Skip to content

Instantly share code, notes, and snippets.

@cameronjonesweb
Last active June 1, 2023 16:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronjonesweb/f3158f53ae7351508b561b258943b223 to your computer and use it in GitHub Desktop.
Save cameronjonesweb/f3158f53ae7351508b561b258943b223 to your computer and use it in GitHub Desktop.
Never Fear, The Customizer Is Here! [WordCamp Sunshine Coast] demo code
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'wordcampsc_link_colour', array(
'label' => __( 'Link Colour', 'wordcampsc' ),
'section' => 'colors',
) ) );
$wp_customize->selective_refresh->add_partial( 'wordcampsc_link_colour', array(
'selector' => '#wordcampsc_customize_styles',
'render_callback' => function() {
echo 'a{color:' . get_theme_mod( 'wordcampsc_link_colour', '' ) . '};';
},
) );
$wp_customize->add_setting( 'wordcampsc_link_colour', array(
'type' => 'theme_mod',
'default' => '#337ab7',
'transport' => 'postMessage',
'sanitize_callback' => 'sanitize_hex_color',
) );
add_action( 'customize_register', 'wordcampsc_customize_register' );
function wordcampsc_customize_register( WP_Customize_Manager $wp_customize ) {
// Settings etc go here
}
function wordcampsc_customize_style_output() {
echo 'a{color:' . get_theme_mod( 'wordcampsc_link_colour', '' ) . '};';
}
$wp_customize->selective_refresh->add_partial( 'wordcampsc_link_colour', array(
'selector' => '#wordcampsc_customize_styles',
'render_callback' => function() {
echo 'a{color:' . get_theme_mod( 'wordcampsc_link_colour', '' ) . '};';
},
) );
function wordcampsc_customize_styles() {
echo '<style id="wordcampsc_customize_styles">';
echo 'a{color:' . get_theme_mod( 'wordcampsc_link_colour', '' ) . '};';
echo '</style>';
}
add_action( 'customize_register', 'wordcampsc_customize_register' );
function wordcampsc_customize_register( WP_Customize_Manager $wp_customize ) {
$wp_customize->add_setting( 'wordcampsc_link_colour', array(
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'default' => '#337ab7',
'transport' => 'postMessage',
'sanitize_callback' => 'sanitize_hex_color',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'wordcampsc_link_colour', array(
'label' => __( 'Link Colour', 'wordcampsc' ),
'section' => 'colors',
) ) );
if( isset( $wp_customize->selective_refresh ) ) {
$wp_customize->selective_refresh->add_partial( 'wordcampsc_link_colour', array(
'selector' => '#wordcampsc_customize_styles',
'render_callback' => function() {
wordcampsc_customize_style_output();
},
) );
} else {
wp_enqueue_script( 'wordcampsc_customizer', get_template_directory_uri() . '/customizer.js', array( 'customize-preview' ), NULL, true );
}
}
add_action( 'wp_head', 'wordcampsc_customize_styles' );
function wordcampsc_customize_styles() {
echo '<style id="wordcampsc_customize_styles">';
wordcampsc_customize_style_output();
echo '</style>';
}
function wordcampsc_customize_style_output() {
echo 'a{color:' . get_theme_mod( 'wordcampsc_link_colour', '' ) . '};';
}
if ( isset( $wp_customize->selective_refresh ) ) {
$wp_customize->selective_refresh->add_partial( 'wordcampsc_link_colour', array(
'selector' => '#wordcampsc_customize_styles',
'render_callback' => function() {
echo 'a{color:' . get_theme_mod( 'wordcampsc_link_colour', '' ) . '};';
},
) );
} else {
wp_enqueue_script( 'wordcampsc_customizer', get_template_directory_uri() . '/customizer.js', array(), NULL, true );
}
add_action( 'wp_head', 'wordcampsc_customize_styles' );
function wordcampsc_customize_styles() {
echo '<style id="wordcampsc_customize_styles">';
echo 'a{color:' . get_theme_mod( 'wordcampsc_link_colour', '' ) . '};';
echo '</style>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment