Capture in WordPress Theme Customizer save action

I ran into the following problem:

I used the whole style on the theme options page. When the user clicked the save button, I had a script backend that generated a css file with the changes so that they were not displayed inline on every page. This has many advantages, including caching.

I switched to Theme Customizer, and everything is fine, except that I can’t find a way to connect to the "Save" button. I would like to call a function that updates the contents of the css file when this button is clicked on the backend.

Is it possible?

Thanks!

+4
source share
4 answers

With WordPress 3.6.0, you can now call customize_save_after .

 <?php function emailAdmin(){ mail(' your@email ', 'Woza!', 'You won\'t believe this but someone has updated the theme customizations!!'); } add_action( 'customize_save_after', 'emailAdmin' ); ?> 

Additional information: http://developer.wordpress.org/reference/hooks/customize_save_after/

+6
source

I faced the same situation. Customize_save works before the parameters are saved, so it will exit. I emailed Otto (ottodestruct.com) about this.

The solution that I have right now is as follows:

 add_action('customize_save', 'regenCSS', 100); function regenCSS( $wp_customize ) { checkCSSRegen(); // Checks if I need to regen and does so set_theme_mod('regen-css', time()+3); // Waits 3 seconds until everything is saved } function checkCSSRegen() { if (get_theme_mod('regen-css') != "" && get_theme_mod('regen-css') < time()) { makecss(); remove_theme_mod('regen-css'); } } 

I also add an extra checkCSSRegen (); to my customize_controls_init function.

Again, this is a little hack. Unfortunately, this is the best I can find to do at that time.

Another option is to use the ajax response, which is just a php file ping. It looks more like a hack than that.

Another quick hack would be to make a javascript action, which, when you press the save button, causes the timer to delay the call to the PHP file that starts compilation. This is VERY shitty for me.

The only drawback of the above is that if the user does not reboot or another value is not saved, you may not get all the values ​​you need.

Anyone else have an idea?

** Update ** Just added the following query to the Wordpress team. Hope we get it there.

http://wordpress.org/ideas/topic/do-customize_save-action-hook-after-the-settings-are-saved?replies=3#post-24853

* Update 2 * It looks like it will be in version 3.6 as customize_save_after. Guess a few tweets and sample code that can happen even with the Wordpress team .;)

+5
source

As described in @Dovy, you can already bind customize_save_after to this:

 do_action('customize_save_after', 'savesettings', 99); 

If savesettings save the settings to a file, it will be bad practice to do this using the native functions of the php file (for example, file_put_contents() ), as described here: http://ottopress.com/2011/tutorial-using-the-wp_filesystem/ @ otto.

The solution for saving files will be to use wp_filesystem. To use wp_filesystem, you need the credentials of the user (ftp) file.

customize_save_after will be called in an AJAX request and the result will not be visible. The reason for the AJAX descriptor is that you cannot ask the user for credentials of a file that require a form to be submitted.

The solution can be found by saving the credentials of the file in wp-config.php and adding them (temporary) to the database. Doing this savesettings can read credentials from the database and use them to save the file using credentials. (this solution is described in more detail here: https://wordpress.stackexchange.com/a/126631/31759 )

+4
source

Not tested, but there is a customize_save action in /wp-includes/class-wp-customize-manager.php .

Inside the save() function:

 /** * Switch the theme and trigger the save action of each setting. * * @since 3.4.0 */ 

There are some more interesting action hooks ( do_action ) in this file that can be checked.

0
source

All Articles