get_theme_mod()
Retrieve theme modification value for the current theme.
If the modification name does not exist and $default is a string, then the default will be passed through the sprintf() PHP function with the template directory URI as the first value and the stylesheet directory URI as the second value.
Uses: get_theme_mods()
Used By: get_custom_logo(), get_header_image(), get_nav_menu_locations(), get_header_textcolor(), get_header_video_url(), has_custom_logo()
1 time — 0.00212 sec (very slow) | 50000 times — 0.54 sec (very fast) | PHP 7.4.33, WP 6.1.1
Hooks from the function
Return
Mixed
. Theme modification value.
Usage
get_theme_mod( $name, $default );
- $name(string) (required)
- Theme modification name.
- $default(mixed)
- Theme modification default value.
Default: false
Examples
#1 Background color from themes settings
This example shows how to add a top border for the footer block with the same color as the background color set in the settings.
You can insert the code at the beginning of header.php
<style> .footer { border-top: solid 1px #<?= get_theme_mod('background_color') ?>; } </style>
#2 Background color with a default value
Sometimes you need to set default value for avoid any bad situation. This example could be used to add the custom background color for the .footer block:
.footer { background: #<?= get_theme_mod( 'background_color', '#fff' ) ?>; }
#3 Recommended to escape value with proper escaping function
.search-bar { background-color: <?= esc_html( get_theme_mod( 'talash_background_color', '#000' ) ) ?>; }
<a href="<?= esc_url( get_theme_mod( 'talash_url' ) ); ?>">
Changelog
Since 2.1.0 | Introduced. |