Site logo

[Resolved] Dynamically inserting post id into Element via PHP gives Parse error

Home Forums Support [Resolved] Dynamically inserting post id into Element via PHP gives Parse error

Home Forums Support Dynamically inserting post id into Element via PHP gives Parse error

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #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 5

    If I hard-code the post id into the do_shortcode PHP it works:

    <?php 
    echo do_shortcode('[WPCR_SHOW POSTID="1944" NUM="5"]'); 
    ?>

    What is the correct way of passing the $post_id variable to the shortcode in an Element so that GPP can parse it?

    #1879197
    David
    Staff
    Customer Support

    Hi 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);
    ?>
    #1879536
    feisar

    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_id variable being output dynamically.

    Any other ideas?

    #1879899
    Elvin
    Staff
    Customer Support

    Hi there,

    Can you try doing a var_dump($post_id); to check what the variable returns? This may be the issue.

    Let us know. 😀

    #1880106
    feisar

    Hi,

    var_dump($post_id) on my test page returns int(1944) which is the correct id for that page.

    If I hard code 1944 into 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.php

    Thanks.

    #1880225
    Elvin
    Staff
    Customer Support

    Can 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 $shortcode as well?

    #1880487
    feisar

    Thanks Elvin. I tried that and it didn’t work, and I couldn’t get the var_dump of $shortcode to work either. It would show the same error from the GPP class-hooks.php file.

    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 GPP class-hooks.php files 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!

    #1881403
    Elvin
    Staff
    Customer Support

    Errors 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. 😀

    #1881595
    feisar

    Thank you!

    #1881742
    Elvin
    Staff
    Customer Support

    No problem. 😀

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