Using custom variables with default ones in a conditional statement.

jeff.sydor's Avatar

jeff.sydor

17 Mar, 2020 05:53 PM

Hi, I'm trying to incorporate some custom variables with definitions defined in ACF fields. For one instance, i'd like to change {{link_url}} to an alternate location such as a file url or external one.

If i'm understanding the documenation correctly, I should be using the new_rs_slides_renderer_helper filter for this. Here's what I have so far for the custom variable {{customURL}}, I'm just not sure how to call the default link_url in my function.

function custom_link_acf_var($m, $data, $options) {
    
  $m->addHelper('customURL', function() use ($data) {
    
    if ( get_field( "slider_alt_link_extUrl", $data->ID ) ) : return get_field( "slider_alt_link_extUrl", $data->ID );
    elseif ( get_field( "slider_alt_link_file", $data->ID ) ) : return get_field( "slider_alt_link_file", $data->ID );
    else : return link_url;
    endif;

  } );

}
add_filter('new_rs_slides_renderer_helper','custom_link_acf_var', 10, 4);

Thank you very much!

  1. Support Staff 1 Posted by Dmitry Semenov on 17 Mar, 2020 07:01 PM

    Dmitry Semenov's Avatar

    Hello,

    Not sure if I fully understand what do you expect link_url to be, but if you want to retrieve URL of the corresponding post by ID, you can do just:

    return esc_url(get_permalink($data->ID));
    
  2. 2 Posted by jeff.sydor on 17 Mar, 2020 07:39 PM

    jeff.sydor's Avatar

    Thanks. link_url is the default variable in the slider for the hyperlink.

    I tried adding the new function, but I'm getting an error on the line

    $m->addHelper('customURL', function() use ($data)
    

    Uncaught Error: Call to a member function addHelper() on null in /home directory/wp-content/themes/theme/functions.php:19

    My other function above it works just fine. Is there some kind of conflict that can occur when two similar functions are running?

    function custom_button_acf_var($m, $data, $options) {
      $m->addHelper('customBTN', function() use ($data) {
        if ( get_field( "slider_alt_button_text", $data->ID ) ) : return get_field( "slider_alt_button_text", $data->ID );
        else : return "Read More";
        endif;
      } );
    }
    add_filter('new_rs_slides_renderer_helper','custom_button_acf_var', 10, 4);
    

    thank you

  3. 3 Posted by jeff.sydor on 17 Mar, 2020 08:05 PM

    jeff.sydor's Avatar

    Scratch that, the error one isn't the issue. I didn't know that I could add multiple cases of $m inside the same function.

    Your original response with return esc_url(get_permalink($data->ID)); Works just fine!.

    Thank you so much!

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac