4

I'm working on a custom theme that uses WordPress Customization API to let the user play with parts of the content of his website, basically the front-page which he can modify by adding/removing custom blocks such as latest posts, page children, etc. Blacks structure is collected and filled in an custom input.

Everything works fine except for saving, the Customizer seems to have some safety that blocks me from saving the input value: in order to save it, I need to manually trigger a Javascript 'keypress' event by selecting the input and pressing a key. If I don't, the submit button stays disabled.

By browsing through /wp-admin/js/customize-controls.js I manage to activate the submit button:

wp.customize.trigger('change')

But even though the saving thing is unlocked, it doesn't have any effect: the input value is not sent to admin_ajax.php and my data is not saved.

Any idea of how I could force the trigger on the Javascript API to save my data? I couldn't find detailed doc on the WP JS API, that could helped too if I missed it somewhere.

2 Answers 2

3

Instead of using jQuery to update the value of the custom input, use the wp.customize object’s set() function (found in customize-base.js):

wp.customize( key, function ( obj ) {
    obj.set( newValue );
} );

Where key is the setting, and newValue is the updated value.

Also, worth noting, in the class that extends WP_Customize_Control, make sure to use $this->get_link() to put the data attribute data-customize-setting-link on the custom input (found in class-wp-customize-control.php).

2
  • Thanks Matt! I ended using a custom setting page to resolve this, but I'll look through your solution, much cleaner. Thanks again!
    – caercam
    Commented Apr 11, 2014 at 6:15
  • 1
    @Askelon, can you please say how you solved it. I am facing same issue
    – shahpranaf
    Commented Sep 1, 2015 at 8:49
0

I think this also would help:

$(this).trigger('input');

// input    for @ui.input, @ui.textarea
// change   for @ui.checkbox, @ui.radio, @ui.select
// click    for @ui.responsiveSwitchers

This worked for me since I'm working on a text field. Now the save button goes green

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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