Skip to content

Instantly share code, notes, and snippets.

@yratof
Created November 29, 2017 09:24
Show Gist options
  • Select an option

  • Save yratof/6b480fa6f80da84e44fe32269906c235 to your computer and use it in GitHub Desktop.

Select an option

Save yratof/6b480fa6f80da84e44fe32269906c235 to your computer and use it in GitHub Desktop.
Basic wordpress customizer functions to change the content & see the changes as they happen
<?php
add_action( 'customize_register', __CLASS__ . '::custom_content_area' );
function custom_content_area( $wp_customize ) {
$wp_customize->add_section('custom_content_area_section', array(
'title' => __( 'Content area', 'wordpress' ),
'description' => __( 'Globally change the content area', 'wordpress' ),
'priority' => 60,
'transport' => 'postMessage',
));
$wp_customize->add_setting( 'custom_content_area', array(
'default' => '',
'capability' => 'edit_theme_options',
'transport' => 'postMessage'
));
$wp_customize->add_control( 'custom_content_area', array(
'label' => __( 'Content replacement tool', 'wordpress' ),
'description' => __( 'Change the content of the area', 'wordpress' ),
'type' => 'textarea',
'section' => 'custom_content_area_section',
'input_attrs' => [
'placeholder' => __( 'Change your content by typing here', 'wordpress' ),
],
'settings' => 'custom_content_area'
));
$wp_customize->selective_refresh->add_partial( 'custom_content_area', array(
'selector' => '.content--area',
'container_inclusive' => true,
'render_callback' => 'display_the_custom_content',
'fallback_refresh' => false, // Prevents refresh loop when document does not contain .cta-wrap selector. This should be fixed in WP 4.7.
) );
}
function display_the_custom_content() {
$replacement_content = get_theme_mod( 'custom_content_area' );
$default_content = get_bloginfo( 'description' );
echo '<div class="content--area">';
echo ( $replacement_content ) ? $replacement_content : $default_content;
echo '</div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment