Skip to content

Instantly share code, notes, and snippets.

@bob-moore
Last active June 2, 2016 19:01
Show Gist options
  • Save bob-moore/780cfc68a529617d39e24ad00b5e2422 to your computer and use it in GitHub Desktop.
Save bob-moore/780cfc68a529617d39e24ad00b5e2422 to your computer and use it in GitHub Desktop.
Default Featured Images
<?php
/**
* mpress Theme Customizer.
* @package mpress
*/
class Mpress_Theme_Customizer {
private $sections;
private $settings;
private $controls;
public function __construct() {
add_action( 'customize_register', array( $this, 'core_theme_settings' ) );
add_action( 'customize_register', array( $this, 'social_settings' ) );
add_action( 'customize_register', array( $this, 'mpress_register_custom_logo' ) );
add_action( 'customize_update_image_theme_mod', array( $this, 'customize_update_image_theme_mod' ), 10, 2 );
}
public function core_theme_settings( $wp_customize ) {
// define section
$section = array(
'cabability' => 'edit_theme_options',
'title' => __( 'Core Theme Settings', 'mpress' ),
'description' => __( 'Customize Core Theme Settings' ),
'priority' => 10
);
// Register section
$wp_customize->add_section( 'core_theme_settings_section', $section );
// Define Settings
$settings = array(
'mpress_enqueue_jquery' => array(
'default' => 'wordpress',
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'transport' => 'refresh'
),
'mpress_jquery_location' => array(
'default' => false,
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'transport' => 'refresh'
),
'mpress_archive_type' => array(
'default' => 'content',
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'transport' => 'refresh'
),
'mpress_menu_type' => array(
'default' => 'dropdown',
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'transport' => 'refresh'
)
);
// Register Settings
foreach( $settings as $key => $setting ) {
$wp_customize->add_setting( $key, $setting );
}
// Define Controls
$controls = array(
'mpress_enqueue_jquery' => array(
'label' => __( 'Jquery Source', 'mpress' ),
'section' => 'core_theme_settings_section',
'settings' => 'mpress_enqueue_jquery',
'type' => 'radio',
'choices' => array(
'wordpress' => __( 'Wordpress Core', 'mpress' ),
'google' => __( 'Google CDN', 'mpress' )
)
),
'mpress_jquery_location' => array(
'label' => __( 'Load JQuery in footer', 'mpress' ),
'section' => 'core_theme_settings_section',
'settings' => 'mpress_jquery_location',
'type' => 'checkbox'
),
'mpress_archive_type' => array(
'label' => __( 'Archive Page Listing Type', 'mpress' ),
'section' => 'core_theme_settings_section',
'settings' => 'mpress_archive_type',
'type' => 'radio',
'choices' => array(
'content' => __( 'Full Content', 'mpress' ),
'excerpt' => __( 'Excerpt', 'mpress' )
)
),
'mpress_menu_type' => array(
'label' => __( 'Mobile Menu Type', 'mpress' ),
'section' => 'core_theme_settings_section',
'settings' => 'mpress_menu_type',
'type' => 'radio',
'choices' => array(
'dropdown' => __( 'Simple (dropdown) Menu', 'mpress' ),
'offcanvas' => __( 'Off Canvas Menu', 'mpress' )
)
)
);
// Register Controls
foreach( $controls as $key => $control ) {
$wp_customize->add_control( $key, $control );
}
}
public function social_settings( $wp_customize ) {
// Get our list of social networks from the functions.php file
$social_networks = mpress_social_network_settings();
// If social networks isn't an array, or it's empty lets bail
if( !is_array( $social_networks ) || empty( $social_networks ) ) {
return;
}
// Add section
$section = array(
'cabability' => 'edit_theme_options',
'title' => __( 'Social Settings', 'mpress' ),
'description' => __( 'Set Social Network URI\'s' ),
'priority' => 10
);
$wp_customize->add_section( 'social_settings_section', $section );
foreach( $social_networks as $key => $network ) {
// Define Setting
$setting = array(
'default' => $network['default'],
'type' => 'option',
'capability' => 'edit_theme_options',
'transport' => 'refresh',
'sanitize_callback' => 'esc_url_raw'
);
$wp_customize->add_setting( $network['slug'], $setting );
// Define control
$control = array(
'label' => $network['label'],
'section' => 'social_settings_section',
'setting' => $network['slug'],
'type' => 'text',
);
$wp_customize->add_control( $network['slug'], $control );
} // end foreach
}
public function mpress_register_custom_logo( $wp_customize ) {
$settings = array (
'mpress_site_logo' => array(
'default' => null,
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'transport' => 'refresh'
),
'mpress_mobile_logo' => array(
'default' => null,
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'transport' => 'refresh'
),
'mpress_default_featured_image' => array(
'default' => null,
'type' => 'image_theme_mod',
'capability' => 'edit_theme_options',
'transport' => 'refresh'
),
);
// Add our settings
foreach( $settings as $key => $setting ) {
$wp_customize->add_setting( $key, $setting );
}
$controls = array(
'mpress_site_logo' => array (
'label' => __( 'Site Logo', 'mpress' ),
'section' => 'title_tagline',
'settings' => 'mpress_site_logo'
),
'mpress_mobile_logo' => array (
'label' => __( 'Mobile Logo', 'mpress' ),
'section' => 'title_tagline',
'settings' => 'mpress_mobile_logo'
),
'mpress_default_featured_image' => array (
'label' => __( 'Default Featured Image', 'mpress' ),
'section' => 'core_theme_settings_section',
'settings' => 'mpress_default_featured_image'
)
);
// Add controls
foreach( $controls as $key => $control ) {
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, $key, $control ) );
}
}
public function customize_update_image_theme_mod( $value, $WP_Customize_Setting ) {
$image_id = attachment_url_to_postid ( esc_url_raw( $value ) );
if( $image_id ) {
set_theme_mod( sprintf( '%s_id', $WP_Customize_Setting->id ) , $image_id );
}
set_theme_mod( $WP_Customize_Setting->id, $value );
}
} // end class
$mpress_theme_customizer = new mpress_Theme_Customizer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment