- This topic has 9 replies, 3 voices, and was last updated 4 years, 10 months ago by
Elvin.
-
AuthorPosts
-
August 1, 2021 at 1:20 am #1879031
feisar
I’ve got a Hook Element which should show output from a Reviews plugin via a shortcode. I’m getting the current post id and inserting it into the shortcode like this:
<?php $post = get_post(); $post_id = $post->ID; echo do_shortcode('[WPCR_SHOW POSTID="'.$post_id.'" NUM="5"]'); ?>The post id is correct, however on the front-end of the site (with errors turned on), I get the following error:
Parse error: syntax error, unexpected 'wpcr3_respond_1' (T_STRING), expecting ')' in /wp-content/plugins/gp-premium/elements/class-hooks.php(215) : eval()'d code on line 5If I hard-code the post id into the
do_shortcodePHP it works:<?php echo do_shortcode('[WPCR_SHOW POSTID="1944" NUM="5"]'); ?>What is the correct way of passing the
$post_idvariable to the shortcode in an Element so that GPP can parse it?August 1, 2021 at 6:28 am #1879197David
StaffCustomer SupportHi there,
try using a variable to store your shortcode and then echo the variable:
<?php $post = get_post(); $post_id = $post->ID; $shortcode = '[WPCR_SHOW POSTID="'.$post_id.'" NUM="5"]'; echo do_shortcode($shortcode); ?>August 1, 2021 at 8:41 am #1879536feisar
Hi,
Thanks for the suggestion, but I’m afraid it produces the exact same error message.
I also tried it this way:
<?php $post = get_post(); $post_id = $post->ID; ?> [WPCR_SHOW POSTID="<?php echo $post_id; ?>" NUM="5"]and that outputs the
<div>container for the reviews but no contents, and no error message. Again if I hard-code the post id into the shortcode, it works as expected, but not if the post id is echo’d by PHP. So it appears that whatever code is parsing the contents of the Element doesn’t like the$post_idvariable being output dynamically.Any other ideas?
August 1, 2021 at 7:30 pm #1879899Elvin
StaffCustomer SupportHi there,
Can you try doing a
var_dump($post_id);to check what the variable returns? This may be the issue.Let us know. 😀
August 2, 2021 at 12:26 am #1880106feisar
Hi,
var_dump($post_id)on my test page returnsint(1944)which is the correct id for that page.If I hard code
1944into the shortcode, it works ok.echo do_shortcode('[WPCR_SHOW POSTID="1944" NUM="5"]');– works as expected
echo do_shortcode('[WPCR_SHOW POSTID="'.$post_id.'" NUM="5"]');– shows the eval() parse error from GPP class-hooks.phpThanks.
August 2, 2021 at 2:01 am #1880225Elvin
StaffCustomer SupportCan you try this?
<?php $post = get_post(); $post_id = $post->ID; $shortcode = '[WPCR_SHOW POSTID="'.$post_id.'" NUM="5"]'; $to_string = strval($shortcode); echo do_shortcode($to_string); ?>if it still breaks, can you do a var_dump of
$shortcodeas well?August 2, 2021 at 6:10 am #1880487feisar
Thanks Elvin. I tried that and it didn’t work, and I couldn’t get the var_dump of
$shortcodeto work either. It would show the same error from the GPPclass-hooks.phpfile.However, after a couple of hours experimenting (including creating my own simple shortcode that uses the $post_id – which produced the same error), I tried unchecking the Execute Shortcodes checkbox in the Element Settings, and it worked with the original code!
So, it seems if you’re using the
do_shortcode()function in PHP, you should not have Execute Shortcodes checked as it conflicts with the GPPclass-hooks.phpfiles parsing of the PHP code in the Element.I don’t know if this is something that needs fixing, or just documenting somewhere (perhaps a message alongside the Execute Shortcodes checkbox?) but anyway, it works now.
Thanks!
August 2, 2021 at 7:43 pm #1881403Elvin
StaffCustomer SupportErrors like this are bound to happen with Hook Elements. While the feature allows running of PHP snippet, it’s purpose is mainly for simple presentation. (simple HTML renders, etc)
For things like this, normally, we recommend Code Snippets plugin or child theme’s functions.php.
But this is worth checking out. We’ll see what we can do about this. 😀
August 2, 2021 at 11:56 pm #1881595feisar
Thank you!
August 3, 2021 at 1:48 am #1881742Elvin
StaffCustomer SupportNo problem. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.