Selective refresh example plugin
<?php | |
/** | |
* Plugin name: Site Title (and Tagline) Smilies | |
* Description: Demonstration of selective refresh in the Customizer. Selectors are targeting elements in Twenty Fifteen. | |
* Author: Weston Ruter, XWP | |
* Plugin URL: https://gist.github.com/westonruter/a15b99bdd07e6f4aae7a | |
* | |
* @package SiteTitleSmilies | |
*/ | |
namespace SiteTitleSmilies; | |
/** | |
* Convert smilies on blogname option. | |
* | |
* @param mixed $data The requested non-URL site information. | |
* @param string $option_name Type of information requested. | |
* @return string Filtered data. | |
*/ | |
function convert_smilies_in_blogname( $data, $option_name ) { | |
if ( 'name' === $option_name || 'description' === $option_name ) { | |
$data = convert_smilies( $data ); | |
} | |
return $data; | |
} | |
add_filter( 'bloginfo', __NAMESPACE__ . '\convert_smilies_in_blogname', 10, 2 ); | |
/** | |
* Render the site title. | |
* | |
* This function can be used in the template directly instead of calling bloginfo(), | |
* and in this why the duplication of logic can be avoided. | |
* For more complex partials with markup, use get_template_part(). | |
* | |
* This function is called after the associated Customizer setting has already | |
* been previewed, so we can call it just as if we were in the template normally. | |
* | |
* @see \get_template_part() | |
* @return string|void If any text string is returned, it will be used instead of any text echoed and captured via output buffering. | |
*/ | |
function render_blogname_partial() { | |
bloginfo( 'name' ); | |
} | |
/** | |
* Render the site tagline. | |
* | |
* This function can be used in the template directly instead of calling bloginfo(), | |
* and in this why the duplication of logic can be avoided. | |
* For more complex partials with markup, use get_template_part(). | |
* | |
* This function is called after the associated Customizer setting has already | |
* been previewed, so we can call it just as if we were in the template normally. | |
* | |
* @see \get_template_part() | |
* @return string|void If any text string is returned, it will be used instead of any text echoed and captured via output buffering. | |
*/ | |
function render_blogdescription_partial() { | |
bloginfo( 'description' ); | |
} | |
/** | |
* Register selective refresh partial. | |
* | |
* @param \WP_Customize_Manager $wp_customize Manager. | |
*/ | |
function register_selective_refresh_partial( \WP_Customize_Manager $wp_customize ) { | |
if ( ! isset( $wp_customize->selective_refresh ) ) { | |
return; | |
} | |
// Note that this would be changed to $wp_customize->add_partial() once the method is added to WP_Customize_Manager. | |
$wp_customize->selective_refresh->add_partial( 'blogname', array( | |
'selector' => '.site-title a, .site-title:not(:has(a))', | |
'render_callback' => __NAMESPACE__ . '\render_blogname_partial', | |
'container_inclusive' => false, | |
) ); | |
$wp_customize->selective_refresh->add_partial( 'document_title', array( | |
'selector' => 'head > title', | |
'settings' => array( 'blogname' ), | |
'render_callback' => 'wp_get_document_title', | |
'container_inclusive' => false, | |
) ); | |
// Note that this would be changed to $wp_customize->add_partial() once the method is added to WP_Customize_Manager. | |
$wp_customize->selective_refresh->add_partial( 'blogdescription', array( | |
'selector' => '.site-description', | |
'render_callback' => __NAMESPACE__ . '\render_blogdescription_partial', | |
'container_inclusive' => false, | |
) ); | |
$setting = $wp_customize->get_setting( 'blogdescription' ); | |
$setting->transport = 'postMessage'; | |
} | |
add_action( 'customize_register', __NAMESPACE__ . '\register_selective_refresh_partial', 100 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment