[wp-hackers] Reacting to options (theme options) in Javascript
ravi
ravi-lists at g8o.net
Sat Aug 14 02:33:05 UTC 2010
On Aug 11, 2010, at 4:18 PM, Otto wrote:
> Best way to do that is using wp_localize_script.
>
> Example:
>
> wp_enqueue_script('my_script', /path/to/my/script.js', array('jquery'), etc...);
>
> wp_localize_script( 'my_script', 'MySettings', array(
> 'setting' => 123,
> 'foo' => "bar"
> ));
>
Brilliant, this is what I need. Thank you for the responses, fellas. Otto, I was sort of doing the PHP check and include JS script thing, except I was doing it the ugly way without using wp_enqueue_script() and with many small JS script files each to handle one JS dependent option.
—ravi
> Basically, that array is the parameters I want to pass to the script.
> You can form it any way you like, it's just a PHP array. What will
> happen before the script gets added to the head is that an inline
> script containing those settings will be added first. So that the
> script will have access to your settings.
>
> -Otto
>
>
> On Wed, Aug 11, 2010 at 2:40 PM, Eric Mann <eric at eamann.com> wrote:
>> 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
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> 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