How The get_theme_mod() function works in WordPress (2023)

The get_theme_mod function makes it easy to access customizations made by the user in the Customizer. It retrieves these theme options stored in the database, allowing you to change things like colors, fonts, and layout.

Justin Gluska

Updated February 9, 2023

matrix code as digital art pixel picture

matrix code as digital art pixel picture

Reading Time: 4 minutes

The get_theme_mod function in WordPress is a convenient way to retrieve theme customization options set by the user in the Customizer. These options are stored in the database and can be used to customize various aspects of a theme, such as colors, fonts, and layout. In this blog post, we'll cover how to use the get_theme_mod function and provide examples of how it can be used in your theme.

Getting Started

To get started, you first need to add customization options to your theme. This can be done in the functions.php file or in a separate file that you include in your theme. You can add options using the customize_register action and the $wp_customize object. For example:

function my_theme_customize_register( $wp_customize ) {
    $wp_customize->add_setting( 'header_bg_color', array(
        'default' => '#ffffff',
        'transport' => 'refresh',
    ) );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_bg_color', array(
        'label' => __( 'Header Background Color', 'my_theme' ),
        'section' => 'colors',
        'settings' => 'header_bg_color',
    ) ) );
}
add_action( 'customize_register', 'my_theme_customize_register' );

In this example, we are adding a setting for the header background color using the add_setting method. The default argument sets the default value for the option, and the transport argument determines when the Customizer will refresh the preview.

We also add a control for the header background color using the add_control method and the WP_Customize_Color_Control class. The label argument sets the label for the control, the section argument sets the section in the Customizer where the control will appear, and the settings argument sets the setting that the control is linked to.

Using get_theme_mod()

Once you have added customization options to your theme, you can retrieve their values using the get_theme_mod function. For example:

$header_bg_color = get_theme_mod( 'header_bg_color', '#ffffff' );

In this example, the get_theme_mod function retrieves the value of the header_bg_color setting. If the setting has been customized by the user, its value will be returned. If the setting has not been customized, the default value #ffffff will be returned.

You can use the returned value in your theme to customize various aspects of your theme. For example, you can use the header background color to set the background color of your header:

<header style="background-color: <?php echo $header_bg_color; ?>">
    ...
</header>

get_theme_mod() vs get_option()

The get_theme_mod() function and the get_option() function in WordPress have some similarities and differences. Both functions are used to retrieve settings stored in the WordPress database.

However, there is a key difference between the two is that get_theme_mod() is specifically designed for retrieving theme-specific settings, while get_option() can be used to retrieve any type of setting stored in the database.

get_theme_mod() stores theme-specific settings as an array in a single option, keyed to the specific theme name. So, when you use set_theme_mod() to set a theme mod, it actually creates a single options row with the name theme_mods_themename that contains a serialized array with the values you set.

On the other hand, get_option() retrieves the value of a specific option, which can be any type of setting stored in the database. It does not have a specific focus on theme-specific settings like get_theme_mod(). The theme mod functions ensure that each theme on the site uses its own set of settings and are not shared between themes. This makes it simpler, more obvious, and more intuitive.

While get_option() is more versatile, it is recommended to use get_theme_mod() when working with theme-specific settings. The get_theme_mod() function provides a cleaner and simpler solution that separates the functionality of theme settings from the rest of the options stored in the database.

Conclusion

The get_theme_mod() function is like a magic key to unlocking your theme's customizations. It allows you to retrieve all the cool modifications you've made to your theme and display them on your website. You see WordPress theme mods are stored in the database and are specific to each theme. That means, if you have two different themes, each will have its own set of customizations. So, when you want to show off those customizations on your website, you just use the get_theme_mod() function to retrieve them. It's like the icing on the cake! This nifty function makes it easier for you to manage your theme's customizations.

Want to Learn Even More?

If you enjoyed this article, subscribe to our free newsletter where we share tips & tricks on how to use tech & AI to grow and optimize your business, career, and life.


Written by Justin Gluska

Justin is the founder of Gold Penguin, a business technology blog that helps people start, grow, and scale their business using AI. The world is changing and he believes it's best to make use of the new technology that is starting to change the world. If it can help you make more money or save you time, he'll write about it!

Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments