Archived topic
Mixing shortcodes and PHP in a hook element
3 replies · Started by Ian on March 16, 2022
I have the following snippet in a hook element, and the Execute Shortcodes/PHP are both checked. But the PHP does not appear to be executed before the shortcode. Is there anything I should consider?
[pdfjs-viewer url="<?php the_field( 'pdf' ); ?>" attachment_id="<?php echo attachment_url_to_postid( get_field( 'pdf' ) ); ?>" viewer_width=100% viewer_height=800px fullscreen=true download=true print=true]
Hi Ian,
I'm not sure unfortunately - the hook element shouldn't be the issue here as it simply display whatever you are adding and doesn't have the ability to make something work or not work.
Does the code work in a normal page editor?
For whatever reason, it seems that the PHP is not being executed inside the shortcode. I was able to resolve this using the Wordpress PHP function: <?php echo do_shortcode("[shortcode]"); ?> resulting in something like this, which does work:
<?php $pdf = get_field( 'pdf' ); ?>
<li>URL: <?php echo esc_url( $pdf['url'] ) ?></li>
<li>ID: <?php echo esc_html( $pdf['id'] ) ?></li>
<?php $pdfshort="[pdfjs-viewer url=\"" . esc_url( $pdf['url']) . "\" attachment_id=\"" . esc_html( $pdf['id']) . "\" viewer_width=100% viewer_height=800px fullscreen=true download=true print=true]" ?>
<li>PDF: <?php echo $pdfshort ?></li>
Embed:
<?php echo do_shortcode("$pdfshort"); ?>
Note the use of \" to escape the quotes in the shortcode; using " did not work.
Glad to hear.