Change a link's url and text using a hook

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

4 thoughts on “Change a link's url and text using a hook”

  1. 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('custom_code', '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;
    
          // 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= $default_url;
    
              // User not yet bought product
              } else {
                  $button_text="BUY NOW";
                  $button_url= $default_url;
              }
          }
    
          $link->url = $button_url;
          $link->text = $button_text;
    
          return $link;
      }
    

    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.

    Reply

Leave a Comment