Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改链接';使用钩子创建url和文本_Javascript_Php_Wordpress - Fatal编程技术网

Javascript 更改链接';使用钩子创建url和文本

Javascript 更改链接';使用钩子创建url和文本,javascript,php,wordpress,Javascript,Php,Wordpress,我有一个Wordpress网站,并希望根据一些条件更改按钮的文本和url。我是Wordpress和php新手,不确定是否应该使用钩子(如下所示)或Javascript(如window.onload…)来实现这一点。我该怎么做 我有以下按钮: <div class="wp-block-button"> <a class="start-button" href="[add_to_cart_url id='1045']&quo

我有一个Wordpress网站,并希望根据一些条件更改按钮的文本和url。我是Wordpress和php新手,不确定是否应该使用钩子(如下所示)或Javascript(如
window.onload…
)来实现这一点。我该怎么做

我有以下按钮:

<div class="wp-block-button">
    <a class="start-button" href="[add_to_cart_url id='1045']" style="border-radius:14px">BUY NOW</a>
</div>

更新:在WordPress页面上,我在
函数中添加了短代码
[start button id=“1045”]
。但是,当试图用WordPress中的快捷码保存页面时,会产生500错误。指向下面的第6行,debug info声明:
PHP致命错误:未捕获错误:调用bool上的成员函数add_to_cart_url()

add_shortcode('start-button', 'button_switch');

function button_switch($id){

    $product = wc_get_product($id);
    $default_url = $product -> add_to_cart_url();

    if ( ! is_user_logged_in() ) {
        $button_text="BUY NOW";
        $button_url=$default_url;
    
    } else {
        $current_user = wp_get_current_user();
    
        // User already bought product
        if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $id ) ) {
            $button_text="GO TO PRODUCT";
            $button_url="/product/1045/";
    
        // User not yet bought product
        } else {
            $button_text="BUY NOW";
            $button_url=$default_url;
        }
    }
    
    $out = "
            <div class='aligncenter'>
                <div class='wp-block-button'>
                    <a class='wp-block-button__link' href='$button_url' style='border-radius:6px'>$button_text</a>
                </div>
            </div>
    ";
    return $out;
}

因此,由于要在块编辑器中添加结束代码,过滤器实际上不是要使用的合适工具,因为不能在块编辑器中运行PHP

我建议您编写自己的短代码,例如

<div class="wp-block-button">
    <a class="start-button" href="[custom_code id='1045']" style="border-radius:14px">BUY NOW</a>
</div>


添加快捷码(“启动按钮”、“按钮开关”);
功能按钮开关($atts){
$product=wc_get_product($atts['id']);
$default_url=$product->add_to_cart_url();
如果(!用户是否已登录()){
$button_text=“立即购买”;
$button\u url=$default\u url;
}否则{
$current_user=wp_get_current_user();
//用户已购买产品
如果(wc_客户_购买了_产品($current_user->user_email,$current_user->ID,$ID)){
$button\u text=“转到产品”;
$button_url=“/product/1045/”;
//用户尚未购买产品
}否则{
$button_text=“立即购买”;
$button\u url=$default\u url;
}
}
$out=”
";
退回$out;

我没有测试这个,但应该可以让你开始了。我还替换了按钮开关函数中的短代码:它缺少一个回音(do_shortcode())我不确定在另一个短代码中调用短代码会是什么行为。

看起来您包含的第一个代码块来自WP块编辑器?不,我只使用WP块编辑器的一个类,但它位于自定义HTML块中。自定义HTML块从何处加载?PHP文件?谢谢@PeterBre嗯,我不确定我是否理解你的问题。在WordPress中,我的操作如下:页面>创建新页面>添加类型为“自定义HTML”的Gutenberg块到该页面>添加OP中显示的块中的内容。这是否回答了您的问题?谢谢@PeterBreen,我尝试实现此功能,但出现了500个错误。我已将实现添加为OP的更新。是否打开调试模式?这可能会给您提供更多有用的信息。我已添加调试信息。行
$default\u url=$pro风管->添加到购物车url();
似乎不正确,但不确定原因。我想这是它上面的一行。我更正了参数如何传递到shortcode函数。请看是的,谢谢!将其更改为
$product=wc\u get\u product($atts['id');
解决了它!
PHP Fatal error:  Uncaught Error: Call to a member function add_to_cart_url() on bool in /home/xxx/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:9
Stack trace:
#0 /home/xxx/public_html/wp-includes/shortcodes.php(343): button_switch()
#1 [internal function]: do_shortcode_tag()
#2 /home/xxx/public_html/wp-includes/shortcodes.php(218): preg_replace_callback()
#3 /home/xxx/public_html/wp-includes/class-wp-hook.php(287): do_shortcode()
#4 /home/xxx/public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters()
#5 /home/xxx/public_html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(1722): apply_filters()
#6 /home/xxx/public_html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(836): WP_REST_Posts_Controller->prepare_item_for_response() in /home/xxx/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code on line 9
<div class="wp-block-button">
    <a class="start-button" href="[custom_code id='1045']" style="border-radius:14px">BUY NOW</a>
</div>
add_shortcode('start-button', 'button_switch');

function button_switch($atts){

$product = wc_get_product($atts['id']);
$default_url = $product -> add_to_cart_url();

if ( ! is_user_logged_in() ) {
  $button_text="BUY NOW";
  $button_url=$default_url;

} else {
  $current_user = wp_get_current_user();

  // User already bought product
  if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $id ) ) {
      $button_text="GO TO PRODUCT";
      $button_url="/product/1045/";

  // User not yet bought product
  } else {
      $button_text="BUY NOW";
      $button_url=$default_url;
  }
}

$out = "
      <div class='aligncenter'>
          <div class='wp-block-button'>
              <a class='wp-block-button__link' href='$button_url' style='border-radius:6px'>$button_text</a>
          </div>
      </div>
";
return $out;