questions

Change a link’s url and text using a hook

Rate this post

I have a WordPress site and would like to the a button’s text and url depending on some conditions. I’m new to WordPress and php and am not sure if I should do this with a hook, as I try below, or with Javascript such as window.onload .... How should I do this?

I have the following button:

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

I added the code below to functions.php but am not sure how to make it apply that actual method on the button… How can I make this work?

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

function button_switch($link){
    if ( ! is_user_logged_in() ) {
        $button_text="BUY NOW";
        $button_url="[add_to_cart_url id='1045']";
    
    // User logged in
    } else {
        $current_user = wp_get_current_user();
    
        // User already bought product
        if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, 1045 ) ) {
            $button_text="GO TO PRODUCT";
            $button_url="/product/1045/";
    
        // User not yet bought product
        } else {
            $button_text="BUY NOW";
            $button_url="[add_to_cart_url id='1045']";
        }
    }

    $link->url = $button_url;
    $link->text = $button_text;

    return $link;
}

Update: On the WordPress page I added the short code [start-button id="1045"] with in functions.php the code below. However, when trying to save the page with the shortcode in WordPress it produces a 500 error. Pointing to the 6th line below, debug info states: PHP Fatal error: Uncaught Error: Call to a member function add_to_cart_url() on bool.

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;
}

Full debug info:

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

 

✔️Solution:

So, since you are adding the end code in the Block Editor, filters aren’t actually the appropriate tool to be using because you can’t run PHP from within the block editor.

I would recommend writing your own shortcode, so something like

<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;

I didn’t test this, but it should get you started. I also replaced the shortcode in your button switch function: it was missing an echo(do_shortcode()) and I am not sure what the behavior would be having a shortcode call inside another shortcode.

Leave a Reply

Your email address will not be published.

Back to top button