Archived topic
Hook element PHP fatal error.
5 replies · Started by boaz on October 17, 2021
Hi, im setting up a hook to run some JS code in one of my pages using the hook element,
using this code i setup from an example you gave in one of your answers in the forum.
<?php if ( is_page( [10100584] ) { ?>
<script>
console.log('this a LP run from a hook');
</script>
<?php } ?>
im getting a PHP Fatal Error: "Uncaught Error: syntax error, unexpected '?>"
hook settings:
wp_head
execute php
run in all pages.
when running just the js code
<script>
console.log('this a LP run from a hook');
</script>
it runs ok
Thanks
Hi there,
its probably the [ square brackets ] you have in your code.
See here for methods of using is_page:
https://developer.wordpress.org/reference/functions/is_page/#comment-370
But you could simply remove the PHP and just output the script and then set the Hook Elements Display Rules for the pages you want it applied to.
Hi David
i tried this
<?php if ( is_page( (10100584) ) { ?>
<script>
console.log('this a LP run from a hook');
</script>
<?php } ?>
same result.
PHP Fatal Error: Uncaught Error: syntax error, unexpected '?>
Hi there,
Any particular reason why 10100584 needs to be wrapped in ()?
If it's a single value, you can do it like this:
<?php if ( is_page( 10100584 ) { ?>
<script>
console.log('this a LP run from a hook');
</script>
<?php } ?>
or like this:
<?php
if ( is_page( 10100584 ) {
echo '<script>
console.log('this a LP run from a hook');
</script>';
}
?>
or do David's recommendation which is basically placing this code:
<script>
console.log('this a LP run from a hook');
</script>
on a Hook element with set display rule location to a specific page, hooked to wp_footer.
Hi Elvin
this worked
<?php
if ( is_page_template('template-landing-page.php')) {
echo "<script> console.log('this a LP run from a hook');</script>";
}
?>
i guess its the echo that made the difference.
Thanks
No problem. Glad you got it sorted. :D