Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Plugin Customizer, full source at https://github.com/soderlind/plugin-customizer
<?php
$customize_register_priority = 9;
add_action( 'wp_loaded', function() {
add_action( 'customize_register', array( $this, 'customizer_plugin_settings' ) );
}, $customize_register_priority );
<?php
/**
* Parameters sent to the previewer script is:
* url The custom permalink to the default template. The last part, newsletter in the exmple below
* will translate to newsletter.php in the templates folder.
*/
$default_url = array(
'url' => home_url( 'plugin-customizer/templates/newsletter/?' . PLUGIN_CUSTOMIZER_PARAM_NAME . '=' . PLUGIN_CUSTOMIZER_PARAM_VALUE ),
);
/**
* Add a template to a section. key = section name, value = url to template.
*
* The last part, title and content in the exmple below, will translate to title.php
* and content.php in the templates folder.
*/
$section_urls = array(
// 'newsletter_title_section' => home_url( 'plugin-customizer/templates/title/' ),
// 'newsletter_content_section' => home_url( 'plugin-customizer/templates/content/' ),
);
wp_add_inline_script(
$handle,
sprintf( 'PluginCustomizer.init( %s, %s );',
wp_json_encode( $default_url ),
wp_json_encode( $section_urls )
),
'after'
);
<?php
function rewrite_rule( $wp_rewrite ) {
$new_rules = array(
'plugin-customizer/templates/(.*)$' => sprintf( 'index.php?plugin-customizer-template=%s',$wp_rewrite->preg_index( 1 ) ),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
<?php
$customize_register_priority = 9;
add_action( 'wp_loaded', function() {
add_action( 'customize_register', array( $this, 'customizer_plugin_settings' ) );
}, $customize_register_priority );
/**
* Bailout if not called from the plugin menu links.
*/
if ( ! isset( $_GET[ PLUGIN_CUSTOMIZER_PARAM_NAME ] ) || PLUGIN_CUSTOMIZER_PARAM_VALUE !== wp_unslash( $_GET[ PLUGIN_CUSTOMIZER_PARAM_NAME ] ) ) {
return;
}
add_action( 'wp_loaded', function() {
add_action( 'customize_register', array( $this, 'customizer_plugin_sections' ) );
add_action( 'customize_register', array( $this, 'customizer_plugin_controls' ) );
}, $customize_register_priority );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment