Last active
June 20, 2021 22:38
-
-
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.
This file contains hidden or 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
<?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