Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted
+50

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
{
share|improve this answer
    
Thank you soo much. I can't even believe this. I spent like 5 days on a hackish work around. Soo frustrating. Although I am ecstatic that I know I can do this now. SE will allow me to award the bounty in 14 more hours. I selected your answer though, since it has been solved. Will be back. Thanks again. –  user1632018 Dec 29 '13 at 7:17
    
There seems to be one issue. I can only get the full overview to work. When trying to get individual settings, I get an error saying it is not a function. I have been tinkering with it to get the individual setting value to output into the console, to no avail. –  user1632018 Dec 29 '13 at 7:55
    
It seems that my issue was me trying to write it into the console. Which I could only get to work if I created a function that accessed a theme setting like the one in the OP, and declared a global variable outside of the function, and then define the variable inside the function with the wp.customize._value.blogname(); method. I seem to have alot of .net habits I need to drop in order to be more productive with jquery. Good news is when outputting the method directly, within jquery it works great and that's what I needed. Thanks again. EDIT: 3 more hours and it can be awarded. –  user1632018 Dec 29 '13 at 18:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.