Category: WordPress snippets

Use selective refresh in the customizer

Since WordPress 4.5, we can reload only parts of the customizer preview after a setting has changed. With that, the user has — at least — the feeling of a faster experience. And in most cases, it will be actually faster (and besides that, this feature could eliminate the customizer page and replace it with inline editing, writes Weston Ruter in his post »Selective Refresh in the Customizer«). Here I show you how to implement selective refresh. Continue reading Use selective refresh in the customizer

Conditional displaying and hiding of customizer controls via JavaScript

When using Partial Refresh in the customizer to reload only a part of the preview after changing a setting’s value, the active_callback for these settings is not working anymore. With active_callback you can, for example, show a control only if another control has a particular value. In that case, the conditional displaying has to be done with JavaScript. Continue reading Conditional displaying and hiding of customizer controls via JavaScript

Modifying an existing customizer control

This post shows you how to modify an existing customizer control — helpful for the solution was a Gist by Marcio Duarte.

In my case, I wanted to add an active_callback to the header image control to only show it if the user is on a static front page. To get this done, we only need one line of code:

/**
 * Set active callback for the header image control.
 */
$wp_customize->get_control( 'header_image' )->active_callback = 'slug_is_static_front_page';

You can modify the other control properties accordingly — a few examples can be found in the above-linked Gist. For the sake of completeness here is the callback function:

/**
 * Check if we are on a static front page.
 *
 * @param WP_Customize_Control $control Control object.
 *
 * @return bool
 */
function slug_is_static_front_page( $control ) {
	/**
	 * Return true if is static front page.
	 */
	if ( is_front_page() && is_page() ) {
		return true;
	} else {
		return false;
	}
}

Modifying robots.txt for individual sites of a multisite install

WordPress creates a robots.txt dynamically. To overwrite it in a normal non-multisite installation, you can just upload a static robots.txt to the server. On a multisite install, this would overwrite the robots.txt for all sites, which is not always the wanted behavior. This post explains how you can modify robots.txt for individual sites of a multisite. Continue reading Modifying robots.txt for individual sites of a multisite install