Skip to content

Instantly share code, notes, and snippets.

@holisticnetworking
Last active June 28, 2018 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holisticnetworking/3db89eae05570ba9da9247b209c03208 to your computer and use it in GitHub Desktop.
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.
/**
* 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