Last active
June 2, 2016 19:01
-
-
Save bob-moore/780cfc68a529617d39e24ad00b5e2422 to your computer and use it in GitHub Desktop.
Default Featured Images
This file contains hidden or 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
<?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(); |
This file contains hidden or 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
if( !function_exists( 'mpress_featured_image' ) ) { | |
function mpress_featured_image( $size = 'post-thumbnail', $attr = '' ) { | |
// if post has a thumbnail, use it | |
if( has_post_thumbnail() ) { | |
the_post_thumbnail( $size, false, $attr ); | |
return; | |
} | |
// Else use default | |
$default_id = get_theme_mod( 'mpress_default_featured_image_id', null ); | |
if( $default_id !== null ) { | |
echo wp_get_attachment_image( $default_id, $size, false, $attr ); | |
return; | |
} | |
return false; | |
} | |
} | |
if( !function_exists( 'get_mpress_featured_image' ) ) { | |
function get_mpress_featured_image( $size = 'post-thumbnail', $attr = '' ) { | |
// if post has a thumbnail, use it | |
if( has_post_thumbnail() ) { | |
return get_the_post_thumbnail( $size, false, $attr ); | |
} | |
// Else use default | |
$default_id = get_theme_mod( 'mpress_default_featured_image_id', null ); | |
if( $default_id !== null ) { | |
return wp_get_attachment_image( $default_id, $size, false, $attr ); | |
} | |
return false; | |
} | |
} | |
if( !function_exists( 'mpress_featured_image_url' ) ) { | |
function mpress_featured_image_url() { | |
// if post has a thumbnail, use it | |
if( has_post_thumbnail() ) { | |
echo wp_get_attachment_url( get_post_thumbnail_id() ); | |
return; | |
} | |
// Else use default | |
$default_url = get_theme_mod( 'mpress_default_featured_image', null ); | |
if( $default_id !== null ) { | |
echo $default_url; | |
return; | |
} | |
return false; | |
} | |
} | |
if( !function_exists( 'get_mpress_featured_image_url' ) ) { | |
function get_mpress_featured_image_url() { | |
// if post has a thumbnail, use it | |
if( has_post_thumbnail() ) { | |
return wp_get_attachment_url( get_post_thumbnail_id() ); | |
} | |
// Else use default | |
$default_url = get_theme_mod( 'mpress_default_featured_image', null ); | |
if( $default_id !== null ) { | |
return $default_url; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment