Archived topic
Using Plugin Shortcode in Hook Element
7 replies · Started by Benny on February 15, 2021
Hi Team,
I've done my research in this forum and I came across this: https://generatepress.com/forums/topic/dynamic-shortcode-with-custom-field/
but the answer was not very clear to me.
Issue: I'm using a plugin which generates a shortcode like this:
[wp_express_checkout product_id="5486"]
I have created a custom field "myProductID" and assigned values like "1234", "4567" etc.
Using the hook element I was trying to dynamically pull
[wp_express_checkout product_id=myProductID]
which is of course not working.
Could you please point me in the right direction?
Thank you very much
Hi there,
From David's code here - https://generatepress.com/forums/topic/dynamic-shortcode-with-custom-field/#post-1656086 - he assigned the dynamic values coming from a custom field.
$itemid = get_field( "item_id" );
$shortcode = '[the_shortcode id="'.$itemid.'"]';
echo do_shortcode($shortcode);
Let's break this down:
$itemid is a variable holds the value coming from the field item_id. In your case, you can replace this with the custom field name/slug (myProductID).
The $shortcode basically holds the shortcode. In your case, it's value should be '[wp_express_checkout product_id="'.$itemid.'"]';
The echo do_shortcode($shortcode); basically means to display and execute the shortcode.
So in your specific case, the code may look something like this:
$itemid = get_field( "myProductID" );
$shortcode = '[wp_express_checkout product_id="'.$itemid.'"]';
echo do_shortcode($shortcode);
Now the tricky part is how or where you're going to use this.
You can't use this as it is on a shortcode blocks within the content as this is a PHP snippet. That said, you may have to wrap it with another PHP snippet to turn it to a shortcode.
Example:
add_shortcode('dynamic_wp_express_checkout',function(){
$itemid = get_field( "myProductID" );
$shortcode = '[wp_express_checkout product_id="'.$itemid.'"]';
echo do_shortcode($shortcode);
});
With this, you can use [dynamic_wp_express_checkout] shortcode on shortcode blocks.
But if you're using it on Hook Elements, then adding the PHP delimiters should be enough.
<?php
$itemid = get_field( "myProductID" );
$shortcode = '[wp_express_checkout product_id="'.$itemid.'"]';
echo do_shortcode($shortcode);
?>
Just make sure Execute PHP is checked. :)
Hey Elvin,
thanks very much for your support.
Unfortunately I can't get it running.
Using [dynamic_wp_express_checkout] shortcode on shortcode block throws a fatal error.
I'm using the Code Snippet plugin
Using PHP in Hook Element didn't help either.
"Execute PHP" never runs, so I searched and found David's code on how to enable "Execute PHP"
add_filter( 'generate_hooks_execute_php', '__return_true' ); using the Code Snippets plugin.
But that also didn't work.
Not even a simple "echo"-command seems to work using "Execute PHP" in Hook Elements.
Can you remove echo and just keep do_shortcode($shortcode);?
Example:
<?php
$itemid = get_field( "myProductID" );
$shortcode = '[wp_express_checkout product_id="'.$itemid.'"]';
do_shortcode($shortcode);
?>
Hey Elvin,
first, I want to thank you and the whole GP Team.
You are so awesome and very helpful. I think GP should start charging a bit more or you should create at least an extra donation site /pot, so we "grateful customers" can donate an extra EURO or two.
You pointed me in the right direction.
I used the following code to solve the issue (using a Hook Element)
I dont know much about PHP, so feel free to polish my code, in case some other noobies are facing the same issue:
<?
$key_name = get_post_custom_values($key = 'name of the custom field goes here');
$shortcode = '[wp_express_checkout product_id="'.$key_name[0].'"]';
echo do_shortcode($shortcode);
?>
Thanks very much, Elvin. You made my day.
Greetings from Germany.
We have a donate page found here: https://generatepress.com/ongoing-development/
We're glad you got it sorted. No problem. :)
Done :)
We sincerely appreciate that you find the help we provide useful. Thanks. :D
Let us know if you need further help. :)