set_theme_mod( string $name, mixed $value ): bool
- Since
- 2.1.0, 5.6.0
- Source
wp-includes/theme.php:1105
Compatibility
- WordPress
- since 5.6.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$namestring- Theme modification name.
$valuemixed- Theme modification value.
Return value
bool- True if the value was updated, false otherwise.
Performance profile
How much work a call to set_theme_mod() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 26–27
- Plugin surface
- 1 hook
- Called by
- 16
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 27.
Third-party callbacks on 'pre_set_theme_mod_{$name}' run inside this call, and their cost is not bounded by anything here.
16 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - cacheobject cache
wp_cache_get()one call below set_theme_mod() - serializeserialisation
maybe_unserialize()one call below set_theme_mod()
Further down the call graph this can also reach transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost set_theme_mod() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 26–27 | get_theme_mods(), apply_filters(), get_option(), update_option() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 27 instructions, 26–27 executed per call, 1 branch. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while set_theme_mod() runs, in this order:
- apply_filters( pre_set_theme_mod_{$name} )filterline 1121 (+16 into the body)
Filters the theme modification, or 'theme_mod', value on save.
Uses · 4
- get_theme_mods()Retrieves all theme modifications.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_option()Retrieves an option value based on an option name.
- update_option()Updates the value of an option that was already added.
Used by · 16
- Custom_Background::handle_upload()Handles an Image upload for the background image.
- Custom_Background::take_action()Executes custom background modification.
- Custom_Background::wp_set_background_image()
- Custom_Image_Header::reset_header_image()Resets a header image to the default image for the theme.
- Custom_Image_Header::set_header_image()Chooses a header image, selected from existing uploaded and default headers, or provides an array of uploaded header data (either new, or from media library).
- Custom_Image_Header::take_action()Executes custom header modification.
- WP_Customize_Custom_CSS_Setting::update()Store the CSS setting value in the custom_css custom post type for the stylesheet.
- WP_Customize_Setting::set_root_value()Set the root value for a setting, especially for multidimensional ones.
- WP_REST_Menus_Controller::handle_locations()Updates the menu's locations from a REST request.
- _wp_check_split_nav_menu_terms()If the term being split is a nav_menu, changes associations.
- _wp_menus_changed()Handles menu config after theme change.
- switch_theme()Switches the theme.
Show all 16
- wp_ajax_menu_locations_save()Handles saving menu locations via AJAX.
- wp_delete_nav_menu()Deletes a navigation menu.
- wp_get_custom_css_post()Fetches the `custom_css` post for a given theme.
- wp_update_custom_css_post()Updates the `custom_css` post for a given theme.
Source code
function set_theme_mod( $name, $value ) { $mods = get_theme_mods(); $old_value = $mods[ $name ] ?? false; /** * Filters the theme modification, or 'theme_mod', value on save. * * The dynamic portion of the hook name, `$name`, refers to the key name * of the modification array. For example, 'header_textcolor', 'header_image', * and so on depending on the theme options. * * @since 3.9.0 * * @param mixed $value The new value of the theme modification. * @param mixed $old_value The current value of the theme modification. */ $mods[ $name ] = apply_filters( "pre_set_theme_mod_{$name}", $value, $old_value ); $theme = get_option( 'stylesheet' ); return update_option( "theme_mods_$theme", $mods );}Changelog
Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/theme.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.