[wp-hackers] Reacting to options (theme options) in Javascript

Eric Mann eric at eamann.com
Wed Aug 11 19:40:00 UTC 2010


I try to keep my PHP and JS as separate as possible.  When there needs to be any
integration, I set up a PHP function that echos the JS options I need access to
and hook it to wp_head.
 
So something like:
 
function returnJS() {
echo '<script type="text/javascript>';
echo "var myWordPressOptions = { 'option1' : 'value', 'option2' : 'value' };";
echo '</script>';
}
add_action('wp_head', 'returnJS');
 
Then you have access to all of your server-side WordPress goodness and can parse
the options as necessary before pushing them to the browser.  One of the options
you set can be a flag to your JS to activate your jQuery code ... or it could
even be a singleton function to do the same thing, it's up to you.
 
But this keeps your PHP files almost JS-free, and you can still enqueue your JS
libraries as usual.
 
On August 11, 2010 at 7:02 PM "[ ravi ]" <ravi-lists at g8o.net> wrote:

>
> Howdy all,
>
> I have a preference setting in my theme options. If it is checked on, then
> some jQuery stuff needs to be activated. The jQuery stuff sits in a .js file
> in my theme. Thus far, I have been throwing this sort of Javascript code, that
> has to be executed on the basis of a back-end option, in a separate PHP file
> with wrapper PHP code that checks for the relevant option(s).
>
> I am wondering if anyone has a cleverer way of achieving the same thing?
>
> Cheers,
>
>         —ravi
>
>
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list