- This topic has 7 replies, 2 voices, and was last updated 3 years, 2 months ago by Elvin.
-
AuthorPosts
-
February 15, 2021 at 9:08 am #1659288Benny
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
February 15, 2021 at 6:21 pm #1659727ElvinStaffCustomer SupportHi 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 fielditem_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. π
February 16, 2021 at 12:23 am #1659889BennyHey 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 pluginUsing 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.
February 16, 2021 at 12:36 am #1659903ElvinStaffCustomer SupportCan you remove
echo
and just keepdo_shortcode($shortcode);
?Example:
<?php $itemid = get_field( "myProductID" ); $shortcode = '[wp_express_checkout product_id="'.$itemid.'"]'; do_shortcode($shortcode); ?>
February 16, 2021 at 1:31 am #1659953BennyHey 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.February 16, 2021 at 5:11 pm #1661077ElvinStaffCustomer SupportWe have a donate page found here: https://generatepress.com/ongoing-development/
We’re glad you got it sorted. No problem. π
February 16, 2021 at 10:39 pm #1661250BennyDone π
February 17, 2021 at 5:42 pm #1662537ElvinStaffCustomer SupportWe sincerely appreciate that you find the help we provide useful. Thanks. π
Let us know if you need further help. π
-
AuthorPosts
- You must be logged in to reply to this topic.