Skip to content

Instantly share code, notes, and snippets.

@apmarshall
Created February 1, 2017 15:12
Show Gist options
  • Save apmarshall/954bdbcf83ffea838825989b4feca67e to your computer and use it in GitHub Desktop.
Save apmarshall/954bdbcf83ffea838825989b4feca67e to your computer and use it in GitHub Desktop.
Custom Customizer controls for Showcasing Chosen Category and Pages in WordPress
// Customizer controls
// Custom Control for Category Dropdown
if (class_exists('WP_Customize_Control')) {
class WP_Customize_Category_Control extends WP_Customize_Control {
/**
* Render the control's content.
*
* @since 3.4.0
*/
public function render_content() {
$dropdown = wp_dropdown_categories(
array(
'name' => '_customize-dropdown-categories-' . $this->id,
'echo' => 0,
'show_option_none' => __( '— Select —' ),
'option_none_value' => '0',
'selected' => $this->value(),
)
);
// Hackily add in the data link parameter.
$dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
printf(
'<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
$this->label,
$dropdown
);
}
}
}
function greene_customize_register( $wp_customize ) {
$wp_customize->add_section( 'category_featured_section', array(
'title' => _('Featured Category', 'greene'),
'description' => _('Choose a category to highlight on the front page', 'greene')
) );
$wp_customize->add_section( 'page_featured_section', array(
'title' => _('Featured Page', 'greene'),
'description' => _('Choose pages to highlight on the front page', 'greene')
));
$wp_customize->add_setting( 'category_featured', array(
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'default' => 'volunteer-opportunities'
));
$wp_customize->add_setting( 'category_featured_title', array(
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'sanitize_callback' => 'sanitize_text_field'
));
$wp_customize->add_setting( 'category_featured_button', array(
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'sanitize_callback' => 'sanitize_text_field'
));
for($count = 1; $count <=3; $count++) {
$wp_customize->add_setting( 'page_featured-' . $count, array(
'type' => 'theme_mod',
'capability' => 'edit_theme_options'
));
$wp_customize->add_setting( ' page_featured_title-' . $count, array(
'type' => 'theme_mod',
'capability' => 'edit_theme_option',
'sanitize_callback' => 'sanitize_text_field'
));
$wp_customize->add_setting( 'page_featured_button-' . $count, array(
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'sanitize_callback' => 'sanitize_text_field'
));
}
$wp_customize->add_control(
new WP_Customize_Category_Control(
$wp_customize,
'category_featured',
array(
'label' => _('Featured Category', 'greene'),
'section' => 'category_featured_section'
)
)
);
$wp_customize->add_control('category_featured_title', array(
'label' => _('Display Title', 'greene'),
'type' => 'text',
'section' => 'category_featured_section'
));
$wp_customise->add_control('category_featured_button', array(
'label' => _('Button Text', 'greene'),
'type' => 'text',
'section' => 'category_featured_section'
));
for ($count = 1; $count <= 3; $count++ ) {
$wp_customize->add_control('page_featured-' . $count, array(
'label' => _('Featured Page', 'greene'),
'type' => 'dropdown-pages',
'section' => 'page_featured_section'
));
$wp_customize->add_control('page_featured_title-' . $count, array(
'label' => _('Display Title', 'greene'),
'type' => 'text',
'section' => 'page_featured_section'
));
$wp_customize->add_control('page_featured_button-' . $count, array(
'label' => _('Button Text', 'greene'),
'type' => 'text',
'section' => 'page_featured_section'
));
}
}
add_action( 'customize_register', 'greene_customize_register');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment