Site logo

[Resolved] How do I automatically insert page slug into element

Home Forums Support [Resolved] How do I automatically insert page slug into element

Home Forums Support How do I automatically insert page slug into element

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #735682
    RJ

    Hi there – I have created an element hook for one of my banners. Is there a way to dynamically insert the page slug into the &utm_term=page-slug parameter?

    TIA, Robin

    #736315
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You can try creating a shortcode:

    add_shortcode( 'get_current_slug', function() {
        global $post;
        
        return $post->post_name;
    } );

    Then use [get_current_slug] in your Element.

    #736357
    RJ

    Hi Tom – Thanks for replying. I must be doing something wrong as that didn’t appear to work.

    I inserted the code you wrote into the Snippet plugin.

    https://www.screencast.com/t/DkJYnoTRYQ

    And inserted the shortcode into the Element, see

    https://www.screencast.com/t/q9cj82qI9w

    TIA

    Robin

    #736362
    Tom
    Lead Developer
    Lead Developer

    Is “Execute Shortcodes” checked in the Hook?

    #736367
    RJ

    You and your team are such rockstars Tom!

    Cough (embarrassingly). Fiddle with fingers. Look the other way… err, no I hadn’t checked the ES box.

    I have now, and it works a treat… thanks so much.

    ATB, Robin

    #736666
    Tom
    Lead Developer
    Lead Developer

    You’re very welcome! 🙂

    #1371898
    RJ

    I have just revisited this and am having a bit of trouble.

    I have an Element at the bottom of every page with a hyperlinked button that takes people to: https://www.eatweeds.co.uk/foraging-in-summer/?source=xxx

    I am trying to insert the page slug and have tried this:
    https://www.eatweeds.co.uk/foraging-in-summer/?source=[get_current_slug]

    However, it doesn’t appear to work.

    I have the following PHP snippet installed and have “Execute Shortcodes” checked in the Hook, as Tom suggested above:

    add_shortcode( 'get_current_slug', function() {
        global $post;
        
        return $post->post_name;
    } );

    Any ideas? TIA

    #1372003
    David
    Staff
    Customer Support

    Hi there,

    you could add the button HTML and full link inside your shortcode like so:

    add_shortcode( 'get_current_slug', function() {
      global $post;
      $content = '<a class="button cta-link" href="https://www.eatweeds.co.uk/foraging-in-summer/?source=' . $post->post_name .'">Button Label</a>';
      
      return $content;
    } );

    Then you just need to add your shortcode to the hook.
    I added the GP button class and a custom cta-link class for specific styling.

    #1372045
    RJ

    Hi David,

    I have added what you suggested and it doesn’t appear to pass the slug through.

    If you scroll to the bottom of this page:
    https://www.eatweeds.co.uk/black-mustard-soup

    And look for the Foraging in Summer book, then click the button, you’ll see what I mean.

    Any ideas what I might be doing wrong? TIA

    #1372075
    David
    Staff
    Customer Support

    I am not seeing the new code.
    Did you disable/delete the shortcode that Tom provided?
    And make sure you only add [get_current_slug] to your hook. No HTML before it as my code already contains it.

    #1372389
    RJ

    I’m at a bit of a loss, David. I deleted Tom’s shortcode and replaced it with yours.

    Here’s a 60-second screen recording of what I have done:
    https://www.loom.com/share/74b7400a54be496a95e1cdce387a3a47

    TIA

    #1372696
    Tom
    Lead Developer
    Lead Developer

    From the video it looks like “Execute PHP” is checked, but “Execute Shortcodes” is not. You’ll want to check that shortcodes option.

    Then you can use my original function with the content you have in that Element as David’s will output the entire link.

    #1372728
    RJ

    That’s great. Thanks, Tom. I didn’t realise I needed two snippets, one for your code and the other for David’s.

    Many thanks to you both.

    #1373015
    David
    Staff
    Customer Support

    Hi Robin – it should work now with just Toms snippet. As the issue before was the Execute Shortcodes was missing from the Hook element.

    Either way glad to hear its working.

    #1373056
    RJ

    Thanks, David.

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.