Skip to content

Instantly share code, notes, and snippets.

@justintadlock
Last active June 20, 2021 22:38
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save justintadlock/9598018c5b085094e021f4d27d31ea77 to your computer and use it in GitHub Desktop.
Save justintadlock/9598018c5b085094e021f4d27d31ea77 to your computer and use it in GitHub Desktop.
Get theme mods that fall back to the stored parent theme mod if child theme is active.
<?php
/**
* The purpose of this code is to show how you can use theme mods that will fall back to
* the already-set mods of the parent theme. So, when a child theme is active, the code
* checks, in the following order: 1) child theme mod, 2) parent theme mod, 3) default.
*/
function jt_get_theme_mod( $name, $default = false ) {
if ( is_child_theme() )
return get_theme_mod( $name, jt_get_parent_theme_mod( $name, $default ) );
return get_theme_mod( $name, $default );
}
function jt_get_parent_theme_mods() {
$slug = get_option( 'template' );
return get_option( "theme_mods_{$slug}", array() );
}
function jt_get_parent_theme_mod( $name, $default = false ) {
$mods = jt_get_parent_theme_mods();
if ( isset( $mods[ $name ] ) )
return $mods[ $name ];
if ( is_string( $default ) )
$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
return $default;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment