Last active
April 4, 2019 19:00
-
-
Save danielpataki/bae424ed134eae6cbf25 to your computer and use it in GitHub Desktop.
Adding Theme Options With The Customization API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'customize_register' , 'my_theme_options' ); | |
function my_theme_options( $wp_customize ) { | |
// Sections, settings and controls will be added here | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class My_Customizer { | |
function register() { | |
// Sections, settings and controls will be added here | |
} | |
function header_output() { | |
// The live CSS is added here | |
} | |
function live_preview() { | |
// The live preview is enqueued here | |
} | |
} | |
add_action( 'customize_register' , array( 'My_Customizer' , 'register' ) ); | |
add_action( 'wp_head' , array( 'My_Customizer' , 'header_output' ) ); | |
add_action( 'customize_preview_init' , array( 'My_Customizer' , 'live_preview' ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'customize_preview_init' , 'my_customizer_preview' ); | |
function my_customizer_preview() { | |
wp_enqueue_script( | |
'my_theme_customizer', | |
get_template_directory_uri() . '/js/theme-customizer.js', | |
array( 'jquery', 'customize-preview' ), | |
'', | |
true | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp.customize( 'SETTING_NAME', function( value ) { | |
value.bind( function( newval ) { | |
// Change handled here | |
} ); | |
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( function( $ ) { | |
wp.customize( 'footer_bg_color', function( value ) { | |
value.bind( function( newval ) { | |
$('#footer').css( 'background-color', newval ); | |
} ); | |
} ); | |
} )( jQuery ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$wp_customize->add_setting( 'footer_bg_color', | |
array( | |
'default' => 'f1f1f1', | |
'transport' => 'postMessage' | |
) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'wp_head' , 'my_dynamic_css' ); | |
function my_dynamic_css() { | |
?> | |
<style type='text/css'> | |
#site-footer { | |
background-color:#<?php echo get_theme_mod('footer_bg_color') ?> ; | |
} | |
</style> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment