Archived topic
How do I automatically insert page slug into element
14 replies · Started by RJ on November 23, 2018
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
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.
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
Is "Execute Shortcodes" checked in the Hook?
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
You're very welcome! :)
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
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.
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
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.
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
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.
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.
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.
Thanks, David.