WordPress.org

Ready to get started?Download WordPress

Ideas

Do "customize_save" action hook after the settings are saved

  1. lemmonaid
    Member

    Hi guys,

    Just now I've stumbled uppon a certain "issue". I'm hooking a custom function onto "customize_save" hook that's being fired on saving customization options in Theme Customizer. This function of mine is basically regenerating CSS file with new settings.

    The issue here is that settings are not being applied immediately, only after secondary Theme Customizer save procedure. This is due to "customize_save" hook being executed BEFORE the customizer options are being actually saved.

    My question is if you could move this hook after the options saving or if you could create another new hook at that place that would be awesome.

    Thank you very much for considering.

    Regards,

    Oliver

    Posted: 1 year ago #
  2. Dovy Paukstys
    Member

    12345

    I'm having a similar issue. I ended up doing an admin-ajax hook and running javascript 2500 ms after they clicked save. Very much a hack. Wish it were different.

    Posted: 1 year ago #
  3. Dovy Paukstys
    Member

    12345

    Mods/Admins:

    Please add the following line to the the next release of WordPress.

    do_action( 'customize_save_after', $this );

    to line: 514 of class-wp-customize-manager.php

    The full function should look as follows:

    public function save() {
    		if ( ! $this->is_preview() )
    			die;
    
    		check_ajax_referer( 'save-customize_' . $this->get_stylesheet(), 'nonce' );
    
    		// Do we have to switch themes?
    		if ( ! $this->is_theme_active() ) {
    			// Temporarily stop previewing the theme to allow switch_themes()
    			// to operate properly.
    			$this->stop_previewing_theme();
    			switch_theme( $this->get_stylesheet() );
    			$this->start_previewing_theme();
    		}
    
    		do_action( 'customize_save', $this );
    
    		foreach ( $this->settings as $setting ) {
    			$setting->save();
    		}
    		do_action( 'customize_save_after', $this );
    		die;

    }

    This will allow us to use a hook for after the settings from the customizer have been saved.

    Thank you!

    Posted: 1 year ago #
  4. Ryan Boren
    WordPress Dev

  5. lemmonaid
    Member

    Thanks Ryan!

    Just to let you know guys, I've manage to come up with a workaround that works perfectly fine for me:

    1. First I create a hidden field in customizer options and place it at the very end of my settings. Let's call it "my-last-setting".
    2. Then create a function that will process your saved customizer options. Call it "my_processing_function" for now.
    3. Finally hook the above function like this: add_action( 'customize_save_my-last-setting', 'my_processing_function' );

    This should work just fine I think. Although, it is kind of a "hack" too ;)

    Posted: 1 year ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    3 Votes
  • Status

    Good idea! We're working on it