redirect to a custom page

时间:2018-05-10 作者:bogin

我已经创建了一个插件
插件替换“添加到购物车”按钮,弹出“继续”按钮。对于店主选择的每一种产品
我希望在单击按钮时将用户重定向到我的自定义页面。我需要页面将显示不同产品id的不同内容。我应该将页面放在插件文件夹中的什么位置?

如何重定向到它?

我需要该页面将使用商店主题,例如,我希望该页面将显示产品页面的原样,通过获取url中的产品id…

代码是用id替换按钮的功能。

function replacing_add_to_cart_button( $button, $product  ) {
  $product_id = $product->get_id();
  //
  global $wpdb;

  $shopName = getShopName();
  $existQuary = "SELECT * FROM `wp_onePlusOne` WHERE `id` = \'$product_id\'";
  $existQuaryRes = $wpdb->get_results($existQuary);//simplafy query
  if(!empty($existQuaryRes)){

    $id_onePlusOne = get_object_vars($existQuaryRes[0])[\'id\'];
    $button_text = __("1+1", "woocommerce");
    $button ="<div>".$button."
    <button id=\'myBtn".$id_onePlusOne."\' onclick=\'display".$id_onePlusOne."()\'>1+1</button>
      <div id=\'myModal".$id_onePlusOne."\' class=\'modal\'>
        <div class=\'modal-content".$id_onePlusOne."\'>
          <span id=\'close".$id_onePlusOne."\' class=\'close".$id_onePlusOne."\'  onclick=\'spanDisplay".$id_onePlusOne."()\'>&times;</span>
          <p id=\'".$id_onePlusOne."\'><a href=\'http://linlRedirectTo."/".$id_onePlusOne."\'  onclick=\'unDisplay".$id_onePlusOne."()\' target=\'_blank\'>" . $button_text . \'</a>\'  ."</p>
        </div>
      </div>
      <style>
      .modal {
          display: none;
          position: fixed;
          z-index: 1;
          padding-top: 100px;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
          overflow: auto;
          background-color: rgb(0,0,0);
          background-color: rgba(0,0,0,0.4);
      }
      .modal-content".$id_onePlusOne." {
          background-color: #fefefe;
          margin: auto;
          padding: 20px;
          border: 1px solid #888;
          width: 80%;
      }
      .close".$id_onePlusOne." {
          color: #aaaaaa;
          float: right;
          font-size: 28px;
          font-weight: bold;
      }
      .close".$id_onePlusOne.":hover,
      .close".$id_onePlusOne.":focus {
          color: #000;
          text-decoration: none;
          cursor: pointer;
      }
      </style>
    <script>
    function display".$id_onePlusOne."() {
        document.getElementById(\'myModal".$id_onePlusOne."\').style.display = \'block\';
    }
    function unDisplay".$id_onePlusOne."() {
        document.getElementById(\'myModal".$id_onePlusOne."\').style.display = \'none\';
    }
    function spanDisplay".$id_onePlusOne."() {
        document.getElementById(\'myModal".$id_onePlusOne."\').style.display = \'none\';
    }
    window.onclick = function(event) {
        if (event.target == document.getElementById(\'myModal".$id_onePlusOne."\')) {
            document.getElementById(\'myModal".$id_onePlusOne."\').style.display = \'none\';
        }
    }
    </script></div>" ;
  }

  return $button;

}
谢谢你!!!

1 个回复
SO网友:Benoti

您可以使用template\\u include筛选器来更改重定向链接的常用模板。您还可以将其与template\\u重定向操作混合使用。https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include

结束

相关推荐

运行PHPUnit测试后恢复WordPress默认选项

tl;dr: is there a way to make changes to WordPress options during a PHPUnit test suite execution, and have the changes reverted to the WordPress defaults before the next test suite is executed, without writing a custom teardown function?我正在测试我的插件,它提供了一个功能