Last active
June 28, 2018 16:53
-
-
Save holisticnetworking/3db89eae05570ba9da9247b209c03208 to your computer and use it in GitHub Desktop.
When working with the WordPress Customizer, you may want to be sure a theme mod gets set on save. Unless a setting gets changed in the Customizer, it's values do not go into the final theme_mods data. This object snippet will do just that for you. The Gist includes both the function to use and the hook to attach it to for checking on save.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* On Customizer save, checks for and sets a few | |
* default settings. | |
* @param \WP_Customize_Manager $wpcustomizer | |
*/ | |
public function set_defaults( \WP_Customize_Manager $wpcustomizer ) { | |
$mod = get_theme_mod( 'my_theme_mod_name' ); | |
if( empty( $mod ) ) { | |
set_theme_mod( 'my_theme_mod_name', 'x' ); | |
} | |
} | |
/** | |
* Customizer constructor. | |
*/ | |
public function __construct() { | |
// Additional code awesomnezz goes here. | |
add_action( 'customize_save', array( &$this, 'set_defaults' ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment