diff --git a/README.md b/README.md index 9a9f17c..521c510 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,15 @@ This plugin works within the Genesis Framework, to display your post/page featur ## Description This plugin takes a different approach to how we use and display featured images for posts and pages. Instead of simply reusing an image which already exists in the post/page content, the plugin anticipates that you will want to use lovely large images for your featured images, but to do so intelligently. Depending on what you upload, the plugin will: -* display the image as a backstretch (screen width) image if the image is wider than your site's Large Media Setting. + +* display the image as a _backstretch_ (screen width) image if the image is wider than your site's Large Media Setting. * display the image above your post/page content, centered and up to the width of the content, if your image is larger than your Medium Media Setting, and less than or equal to your Large Media Setting. -* display nothing if your featured image width is less than or equal to your Medium Media Setting. -* display nothing if your featured image is already displayed in your content (the original image, not a resized version). +* display _nothing_ if your featured image width is less than or equal to your Medium Media Setting. +* display _nothing_ if your featured image is already displayed in your content (the original image, not a resized version). + +__New in 1.2.0:__ you can now use the WordPress Customizer to set a _Default Featured Image_ to be used site-wide. This image will be used on any post/page/custom post type which does not have a featured image set. + +_Note: This plugin works with the Genesis Framework and child themes only._ ## Requirements * WordPress 3.8, tested up to 4.0 @@ -44,6 +49,9 @@ Then go to your Plugins screen and click __Activate__. ![Screenshot of a page using the Backstretch Featured Image](https://github.com/robincornett/display-featured-image-genesis/blob/develop/assets/screenshot-1.jpg) __Screenshot of a page using the Backstretch Featured Image.__ +![Screenshot of the WordPress Customizer](https://github.com/robincornett/display-featured-image-genesis/blob/develop/assets/screenshot-2.jpg) +__Use the WordPress Customizer to set a Default Featured Image.__ + ## Frequently Asked Questions ### How do I stop the featured image action from showing on my custom post types? @@ -59,15 +67,18 @@ function rgc_skip_post_types( $post_types ) { return $post_types; } ``` + +It seems that you can also include [conditional tags](http://codex.wordpress.org/Conditional_Tags) in the above, eg `$post_types[] = is_front_page();` to stop the featured image from displaying. This is most helpful if you have set a default featured image in the Customizer. + ### The backstretch image is a little too tall. -If you do not want the height of the backstretch image to be quite the height of the user's window, you can reduce it by just a hair. Go to Settings > Media and change the 'Reduction amount' number from the default of 0. The higher this number is, the shorter your image will be. Feel free to experiment, as no images are harmed by changing this number. +If you do not want the height of the backstretch image to be quite the height of the user's window, you can reduce it by just a hair. Go to Settings > Media and change the 'Height' number from the default of 0. The higher this number is, the shorter your image will be. Feel free to experiment, as no images are harmed by changing this number. -Additionally/alternatively, you could set a max-height for the backstretch image via css: +Additionally/alternatively, you could set a max-height for the backstretch image area via css: ```css -.backstretch { - max-height: 700px; +.big-leader { + max-height: 700px !important; } ``` ## Credits @@ -76,6 +87,10 @@ Additionally/alternatively, you could set a max-height for the backstretch image ## Changelog +###1.2.0 +* new feature: default featured image to display if no image is set +* better method naming/organization + ###1.1.3 * output is now properly managed to show only on single posts/pages and home page, not archives diff --git a/assets/screenshot-2.jpg b/assets/screenshot-2.jpg new file mode 100644 index 0000000..4ba8841 Binary files /dev/null and b/assets/screenshot-2.jpg differ diff --git a/display-featured-image-genesis.php b/display-featured-image-genesis.php index dee1319..216a1d6 100644 --- a/display-featured-image-genesis.php +++ b/display-featured-image-genesis.php @@ -12,7 +12,7 @@ * Plugin Name: Display Featured Image for Genesis * Plugin URI: http://github.com/robincornett/display-featured-image-genesis/ * Description: This plugin requires the Genesis Framework. It varies the display of the post or page featured image, depending on size. - * Version: 1.1.3 + * Version: 1.2.0 * Author: Robin Cornett * Author URI: http://robincornett.com * License: GPL-2.0+ diff --git a/includes/class-displayfeaturedimagegenesis-customizer.php b/includes/class-displayfeaturedimagegenesis-customizer.php new file mode 100644 index 0000000..e5f242b --- /dev/null +++ b/includes/class-displayfeaturedimagegenesis-customizer.php @@ -0,0 +1,57 @@ +statuses = array( '' => __( 'No Image', 'display-featured-image-genesis' ) ); + + parent::__construct( $manager, $id, $args ); + + $this->add_tab( 'upload-new', __( 'Upload New', 'display-featured-image-genesis' ), array( $this, 'tab_upload_new' ) ); + $this->add_tab( 'uploaded', __( 'Uploaded', 'display-featured-image-genesis' ), array( $this, 'tab_uploaded' ) ); + + if ( $this->setting->default ) + $this->add_tab( 'default', __( 'Default', 'display-featured-image-genesis' ), array( $this, 'tab_default_background' ) ); + + // Early priority to occur before $this->manager->prepare_controls(); + add_action( 'customize_controls_init', array( $this, 'prepare_control' ), 5 ); + } + +} + + global $wp_customize; + + $wp_customize->add_section( 'displayfeaturedimage-settings', array( + 'title' => __( 'Default Featured Image', 'display-featured-image-genesis' ), + 'priority' => 105, + ) ); + + $wp_customize->add_setting( 'displayfeaturedimage_default', array( + 'type' => 'option', + ) ); + + $wp_customize->add_control( new Display_Featured_Image_Genesis_Customizer( $wp_customize, 'displayfeaturedimage_default', array( + 'label' => __( 'Display Featured Image for Genesis', 'display-featured-image-genesis' ), + 'description' => __( 'You may set a default image to be used as the backstretch or large featured image if no image is selected for the post/page.', 'display-featured-image-genesis' ), + 'section' => 'displayfeaturedimage-settings', + 'settings' => 'displayfeaturedimage_default', + ) ) ); diff --git a/includes/class-displayfeaturedimagegenesis-output.php b/includes/class-displayfeaturedimagegenesis-output.php index 8a7a9db..1dbecbd 100644 --- a/includes/class-displayfeaturedimagegenesis-output.php +++ b/includes/class-displayfeaturedimagegenesis-output.php @@ -33,13 +33,29 @@ class Display_Featured_Image_Genesis_Output { protected function get_image_variables() { $item = new stdClass(); global $post; - if ( is_home() ) { - $postspage = get_option( 'page_for_posts' ); + $fallback = get_option( 'displayfeaturedimage_default' ); + $postspage = get_option( 'page_for_posts' ); + $postspage_image = get_post_thumbnail_id( $postspage ); + + // Set Featured Image Source + if ( is_home() && !empty( $postspage_image ) ) { $item->original = wp_get_attachment_image_src( get_post_thumbnail_id( $postspage ), 'original' ); } + elseif ( !empty( $fallback ) && !has_post_thumbnail() ) { + $fallback_id = $this->get_image_id( $fallback ); + $item->original = wp_get_attachment_image_src( $fallback_id, 'original' ); + } else { $item->original = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'original' ); } + + // Set Post/Page Title + if ( is_home() ) { + $item->title = get_post( $postspage )->post_title; + } + else { + $item->title = get_the_title(); + } $item->large = get_option( 'large_size_w' ); $item->medium = get_option( 'medium_size_w' ); $item->reduce = get_option( 'displayfeaturedimage_less_header', 0 ); @@ -68,21 +84,26 @@ class Display_Featured_Image_Genesis_Output { public function load_scripts() { $item = $this->get_image_variables(); - if ( ( has_post_thumbnail() && $item->content === false ) || is_home() ) { + if ( ( !empty( $item->original ) && $item->content === false ) || is_home() ) { wp_enqueue_style( 'displayfeaturedimage-style', plugins_url( 'includes/css/display-featured-image-genesis.css', dirname( __FILE__ ) ), array(), 1.0 ); - add_action( 'genesis_before', array( $this, 'do_featured_image' ) ); - - if ( ( $item->original[1] ) > $item->large ) { + if ( ( ( $item->original[1] ) > $item->large ) ) { wp_enqueue_script( 'displayfeaturedimage-backstretch', plugins_url( '/includes/js/backstretch.js', dirname( __FILE__ ) ), array( 'jquery' ), '1.0.0' ); wp_enqueue_script( 'displayfeaturedimage-backstretch-set', plugins_url( '/includes/js/backstretch-set.js', dirname( __FILE__ ) ), array( 'jquery', 'displayfeaturedimage-backstretch' ), '1.0.0' ); wp_localize_script( 'displayfeaturedimage-backstretch-set', 'BackStretchVars', array( - 'src' => $item->original[0], + 'src' => $item->original[0], 'height' => esc_attr( $item->reduce ) )); + add_action( 'genesis_after_header', array( $this, 'do_backstretch_image_title' ) ); + + } + + elseif ( ( $item->original[1] <= $item->large ) && ( $item->original[1] > $item->medium ) ) { + add_action( 'genesis_before_entry', array( $this, 'do_large_image' ) ); // HTML5 + add_action( 'genesis_before_post', array( $this, 'do_large_image' ) ); // XHTML } } } @@ -98,11 +119,11 @@ class Display_Featured_Image_Genesis_Output { $item = $this->get_image_variables(); - if ( $item->content === false ) { - if ( ( has_post_thumbnail() || is_home() ) && $item->original[1] > $item->large ) { + if ( !empty( $item->original ) && $item->content === false ) { + if ( $item->original[1] > $item->large ) { $classes[] = 'has-leader'; } - elseif ( has_post_thumbnail() && ( ( $item->original[1] <= $item->large ) && ( $item->original[1] > $item->medium ) ) ) { + elseif ( ( $item->original[1] <= $item->large ) && ( $item->original[1] > $item->medium ) ) { $classes[] = 'large-featured'; } } @@ -110,35 +131,12 @@ class Display_Featured_Image_Genesis_Output { } /** - * do the featured image + * backstretch image title (for images which are larger than Media Settings > Large ) * @return image * * @since 1.0.0 */ - public function do_featured_image() { - global $post; - - $item = $this->get_image_variables(); - - if ( $item->content === false ) { - if ( $item->original[1] > $item->large ) { - add_action( 'genesis_after_header', array( $this, 'do_backstretch_image' ) ); - } - elseif ( ( $item->original[1] <= $item->large ) && ( $item->original[1] > $item->medium ) ) { - add_action( 'genesis_before_entry', array( $this, 'do_large_image' ) ); // HTML5 - add_action( 'genesis_before_post', array( $this, 'do_large_image' ) ); // XHTML - } - } - - } - - /** - * backstretch image (for images which are larger than Media Settings > Large ) - * @return image - * - * @since 1.0.0 - */ - public function do_backstretch_image() { + public function do_backstretch_image_title() { $item = $this->get_image_variables(); @@ -148,14 +146,7 @@ class Display_Featured_Image_Genesis_Output { } echo '
'; - if ( is_home() ) { - $title = get_post( get_option( 'page_for_posts' ) )->post_title; - echo '

' . $title . '

'; - } - else { - echo '

' . get_the_title() . '

'; - } - + echo '

' . $item->title . '

'; echo '
'; } @@ -170,4 +161,36 @@ class Display_Featured_Image_Genesis_Output { echo get_the_post_thumbnail( $post->ID, 'large', array( 'class' => 'aligncenter featured', 'alt' => the_title_attribute( 'echo=0' ) ) ); } + /** + * Get the ID of each image dynamically. + * + * @since 1.2.0 + * + * @author Philip Newcomer + * @link http://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/ + */ + protected function get_image_id( $attachment_url ) { + global $wpdb; + $attachment_id = false; + + // Get the upload directory paths + $upload_dir_paths = wp_upload_dir(); + + // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image + if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) { + + // If this is the URL of an auto-generated thumbnail, get the URL of the original image + $attachment_url = preg_replace( '(-\d{3,4}x\d{3,4}.)', '.', $attachment_url ); + + // Remove the upload path base directory from the attachment URL + $attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url ); + + // Finally, run a custom database query to get the attachment ID from the modified attachment URL + $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) ); + + } + + return $attachment_id; + } + } diff --git a/includes/class-displayfeaturedimagegenesis-settings.php b/includes/class-displayfeaturedimagegenesis-settings.php index 047aed7..4d589a4 100644 --- a/includes/class-displayfeaturedimagegenesis-settings.php +++ b/includes/class-displayfeaturedimagegenesis-settings.php @@ -32,6 +32,7 @@ class Display_Featured_Image_Genesis_Settings { 'media', 'display_featured_image_section' ); + } /** diff --git a/includes/class-displayfeaturedimagegenesis.php b/includes/class-displayfeaturedimagegenesis.php index fbcb402..e4729bc 100644 --- a/includes/class-displayfeaturedimagegenesis.php +++ b/includes/class-displayfeaturedimagegenesis.php @@ -28,6 +28,7 @@ class Display_Featured_Image_Genesis { } add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); add_action( 'admin_init', array( $this->settings, 'register_settings' ) ); + add_action( 'customize_register', array( $this, 'load_customizer' ) ); add_action( 'load-options-media.php', array( $this->settings, 'help' ) ); add_action( 'get_header', array( $this->output, 'manage_output' ) ); } @@ -78,4 +79,13 @@ class Display_Featured_Image_Genesis { load_plugin_textdomain( 'display-featured-image-genesis', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } + /** + * Set up Customizer options for default featured image + * + * @since 1.2.0 + */ + function load_customizer() { + require plugin_dir_path( __FILE__ ) . 'class-displayfeaturedimagegenesis-customizer.php'; + } + } diff --git a/languages/display-featured-image-genesis.pot b/languages/display-featured-image-genesis.pot index 4bf42e6..1afd657 100644 --- a/languages/display-featured-image-genesis.pot +++ b/languages/display-featured-image-genesis.pot @@ -4,10 +4,9 @@ # msgid "" msgstr "" -"Project-Id-Version: Display Featured Image for Genesis 1.1.3\n" +"Project-Id-Version: Display Featured Image for Genesis 1.2.0\n" "POT-Creation-Date: 2014-09-17 21:11-0500\n" -"PO-Revision-Date: 2014-09-30 17:18-0500\n" -"Last-Translator: Robin Cornett \n" +"PO-Revision-Date: 2014-10-15 15:45-0500\n""Last-Translator: Robin Cornett \n" "Language-Team: Robin Cornett \n" "Language: en_US\n" "MIME-Version: 1.0\n" @@ -25,35 +24,62 @@ msgstr "" "X-Poedit-SearchPath-0: .\n" "X-Textdomain-Support: yes\n" +#: ../includes/class-displayfeaturedimagegenesis-customizer.php:25 +msgid "No Image" +msgstr "" + +#: ../includes/class-displayfeaturedimagegenesis-customizer.php:29 +msgid "Upload New" +msgstr "" + +#: ../includes/class-displayfeaturedimagegenesis-customizer.php:30 +msgid "Uploaded" +msgstr "" + +#: ../includes/class-displayfeaturedimagegenesis-customizer.php:33 +msgid "Default" +msgstr "" + +#: ../includes/class-displayfeaturedimagegenesis-customizer.php:52 +msgid "Default Featured Image" +msgstr "" + +#: ../includes/class-displayfeaturedimagegenesis-customizer.php:62 #: ../includes/class-displayfeaturedimagegenesis-settings.php:23 -#: ../includes/class-displayfeaturedimagegenesis-settings.php:77 +#: ../includes/class-displayfeaturedimagegenesis-settings.php:78 msgid "Display Featured Image for Genesis" msgstr "" +#: ../includes/class-displayfeaturedimagegenesis-customizer.php:63 +msgid "" +"You may set a default image to be used as the backstretch or large featured " +"image if no image is selected for the post/page." +msgstr "" + #: ../includes/class-displayfeaturedimagegenesis-settings.php:30 msgid "Height" msgstr "" -#: ../includes/class-displayfeaturedimagegenesis-settings.php:44 +#: ../includes/class-displayfeaturedimagegenesis-settings.php:45 msgid "" "Change this setting to reduce the maximum height of the backstretch image." msgstr "" -#: ../includes/class-displayfeaturedimagegenesis-settings.php:56 +#: ../includes/class-displayfeaturedimagegenesis-settings.php:57 msgid "Pixels to remove " msgstr "" -#: ../includes/class-displayfeaturedimagegenesis-settings.php:58 +#: ../includes/class-displayfeaturedimagegenesis-settings.php:59 msgid "" "Changing this number will reduce the backstretch image height by this number " "of pixels. Default is zero." msgstr "" -#: ../includes/class-displayfeaturedimagegenesis-settings.php:72 +#: ../includes/class-displayfeaturedimagegenesis-settings.php:73 msgid "Reducto!" msgstr "" -#: ../includes/class-displayfeaturedimagegenesis-settings.php:73 +#: ../includes/class-displayfeaturedimagegenesis-settings.php:74 msgid "" "Depending on how your header/nav are set up, or if you just do not want your " "backstretch image to extend to the bottom of the user screen, you may want " @@ -61,13 +87,13 @@ msgid "" "image, making it shorter." msgstr "" -#: ../includes/class-displayfeaturedimagegenesis.php:58 +#: ../includes/class-displayfeaturedimagegenesis.php:63 msgid "" "Sorry, Display Featured Image for Genesis works only with the Genesis " "Framework. It has been deactivated." msgstr "" -#: ../includes/class-displayfeaturedimagegenesis.php:62 +#: ../includes/class-displayfeaturedimagegenesis.php:67 #, php-format msgid "" "Sorry, Display Featured Image for Genesis works only with the Genesis " diff --git a/readme.txt b/readme.txt index 9bb0ff7..072bc79 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Donate link: https://robincornett.com/donate/ Tags: backstretch, featured image, genesis, studiopress Requires at least: 3.8 Tested up to: 4.0 -Stable tag: 1.1.3 +Stable tag: 1.2.0 License: GPL-2.0+ License URI: http://www.gnu.org/licenses/gpl-2.0.txt @@ -20,6 +20,8 @@ This plugin takes a different approach to how we use and display featured images * display _nothing_ if your featured image width is less than or equal to your Medium Media Setting. * display _nothing_ if your featured image is already displayed in your content (the original image, not a resized version). +__New in 1.2.0:__ you can now use the WordPress Customizer to set a _Default Featured Image_ to be used site-wide. This image will be used on any post/page/custom post type which does not have a featured image set. + _Note: This plugin works with the Genesis Framework and child themes only._ == Installation == @@ -42,27 +44,34 @@ You'll want to add a filter to your theme (functions.php file). Here's an exampl return $post_types; } +It seems that you can also include [conditional tags](http://codex.wordpress.org/Conditional_Tags) in the above, eg `$post_types[] = is_front_page();` to stop the featured image from displaying. This is most helpful if you have set a default featured image in the Customizer. + = The backstretch image is a little too tall. = -If you do not want the height of the backstretch image to be quite the height of the user's window, you can reduce it by just a hair. Go to Settings > Media and change the 'Reduction amount' number from the default of 0. The higher this number is, the shorter your image will be. Feel free to experiment, as no images are harmed by changing this number. +If you do not want the height of the backstretch image to be quite the height of the user's window, you can reduce it by just a hair. Go to Settings > Media and change the 'Height' number from the default of 0. The higher this number is, the shorter your image will be. Feel free to experiment, as no images are harmed by changing this number. -Additionally/alternatively, you could set a max-height for the backstretch image via css: +Additionally/alternatively, you could set a max-height for the backstretch image area via css: - .backstretch { - max-height: 700px; + .big-leader { + max-height: 700px !important; } == Screenshots == 1. Screenshot of a page using the Backstretch Featured Image +2. Use the WordPress Customizer to set a Default Featured Image. == Upgrade Notice == -= 1.1.3 = -proper deactivation, better handling of output += 1.2.0 = +New feature: Set a Default Featured Image == Changelog == += 1.2.0 = +* new feature: default featured image to display if no image is set +* better method naming/organization + = 1.1.3 = * output is now properly managed to show only on single posts/pages and home page, not archives