I want to tie in jquery functions with post meta options, so I have created a PHP file with jQuery code inside the PHP tags.
<?php
echo "
function dynamicAdjust() {
jQuery('#main-home').css('margin-top', jQuery(window).height());
}
";
?>
Maybe there is a better way to create dynamic PHP (let me know if there is) with post meta options, but the jQuery here works fine, I enqueued in in my functions.php as a javascript file, and the jQuery funciton works fine.
The issue and the whole point of having the jQuery inside a PHP file is for users to turn options on/off so for example when I wrap the code up there in an if statement like so.
<?php
if(get_post_meta($post->ID, '_moon_full_static_area', true) == 'on'); {
echo "
function dynamicAdjust() {
jQuery('#main-home').css('margin-top', jQuery(window).height());
}
";
}
?>
This gives me this error Fatal error: Call to undefined function get_post_meta() in my dir..
Ok so according to the message its obvious that it does not see get_post_meta as a function, I know anything that has post_meta enabled is connected to the database, Im just not sure what else I need to do other than enqueue the script?? Any ideas?
PS.. Best Practice for Including JS (with PHP) in WordPress Functions file I came across this, is this what I am looking for?
The standard way to pass PHP values to your Javascript files is through
wp_localize_script
.See also in WordPress Answers.
And then, in
my-file.js
:See: What is the JavaScript equivalent of var_dump or print_r in PHP?