I am wondering if it is possible to directly interop with the wp.customize api to get a value of a theme customizer setting. The way I am doing it now is using ajax to get a response from a php function. I seem to be having alot of issues with it.

I know that you can bind to theme customizer controls using jquery with the wp.customize api, although I am wondering if I can directly access a setting from jquery?

EDIT: More information

I know that you can interact with controls directly through the API like this:

/** Site title and description. */
wp.customize( 'blogname', function( value ) {
    value.bind( function( to ) {
        $( '.site-title a' ).html( to );
    } );
} );

I am wondering if it is possible using the same API to directly access theme setting values, using jquery alone?

If not I think it wouldn’t be hard and would be beneficial to extend the wp.customize api(and maybe all other wp functions for that matter) to do exactly what I am manually doing with ajax and php, so this way it would be possible for users to only use jquery themselves to interact with wp functions and have wordpress do the dirty work behind the scenes. This may be a good feature for a later version of WP.

1 Answer
1

Not sure what you try to accomplish, but you can get a value by key using the wp.customize object:

wp.customize.value('show_on_front')();
wp.customize.value('blogname')();
....

sorry no jQuery here, just javascript, and yes, the extra () are intentional.

Edit: Full overview of all settings:

wp.customize._value;
console.log(wp.customize._value);

Edit II:

different approach:

a) lookup all available settings by using

console.log(wp.customize._value);

b) one can not access the value directly, so

wp.customize._value.blogname

won’t work. But, if you make it a function call it should work:

wp.customize._value.blogname();

I don’t think it’s intended to be used this way ( if there is an intention of usage at all ) but it works for me.
I’ve just inspected the wp.customize object and didn’t look up how WP constructs this _value object.
It’s a good idea to test if a setting is available and if it is a function,and finally you can use jQuery 😉

if ( jQuery.isFunction(wp.customize._value.blogname) ) {
// do stuff
}

or like our parents would have made it 😉

if(typeof wp.customize._value.blogname === 'function')
    //do stuff
{

Leave a Reply

Your email address will not be published. Required fields are marked *