Skip to content

Commit

Permalink
Add option to Auto-Set Featured Image as Post Cover
Browse files Browse the repository at this point in the history
- Adds a new option to the Theme Customizer under General Options
- This option can be disabled on a per-post basis by checking "Disable post cover" in the Featured Image meta box
- See Issue #86
  • Loading branch information
raamdev committed Apr 23, 2014
1 parent 0f77576 commit f37b01c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 25 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ The following colors can be changed via the Colors section:

### General Options

![screen shot 2014-04-09 at 7 24 01 pm](https://cloud.githubusercontent.com/assets/53005/2662472/1aac03a2-c03e-11e3-8093-258870e5f1fc.png)
![screen shot 2014-04-23 at 4 30 50 pm](https://cloud.githubusercontent.com/assets/53005/2782883/40146740-cb26-11e3-9c54-80301a57e3ac.png)

- **Show Widgets on Single Posts**. Disabled by default. When this option is enabled, sidebar widgets will also be shown on Single Post pages.
- **Show Post Date in Entry Meta**. Disabled by default. When this option is enabled, the post date will be shown in the entry meta on Blog, Archive, and Search pages. It uses the date format specified in *Dashboard → Settings → General → Date Format*.
- **Show Post Word Count in Entry Meta**. Disabled by default. Shows the post word count in the entry meta on Blog, Archive, and Search pages. Only shows post word count for posts with the Standard Post Format.
- **Show Nav Menu on Single Posts**. Disabled by default. When this option is enabled, the primary navigation menu will also be shown on Single Post pages.
- **Show Updated Date on Single Posts**. Disabled by default. When this option is enabled, the post's last modified date will be shown underneath the published date. If you enable this, you can disable this on a per-post basis by adding a Custom Field to a post with the name `independent_publisher_hide_updated_date` and any value (`yes` or `true` will do).
- **Auto-Set Featured Image as Post Cover**. Disabled by default. When this option is enabled, any Featured Image set for a post will automatically be used as a Post Cover (see [Post Covers (Full-Width Featured Images)](https://github.com/raamdev/independent-publisher#post-covers-full-width-featured-images)).
- **Show Page Load Progress Bar**. Disabled by default. When enabled, a progress bar will appear across the top of the page as the page is loading. The color of the progress bar is determined by the Link Color setting in *Appearance → Customizer → Colors*.
- **Enable Multi-Author Mode**. Disabled by default. Enabling Multi Author Mode changes the behavior of the site to better support multiple authors. The author name is mentioned in the entry meta and the authors name always links to the author page instead of the home page. The Header Image (*Dashboard → Appearance → Customize → Header Image*) is treated as the site logo and placed as a small icon in top left of the single pages to provide a way of getting back to the home page.
- **Comments Call to Action**. "Write a Comment" by default. This allows you to change the label that shows up on the 'Write a Comment' button and also changes the title of the comment form itself.

Expand Down
101 changes: 83 additions & 18 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,17 +548,37 @@ function independent_publisher_comments_call_to_action_text() {
}
}

/**
* Returns true if the the current post has Full Width Featured Image enabled
/*
* Return true if Auto-Set Featured Image as Post Cover is enabled and it hasn't
* been disabled for this post.
*
* Returns true if the current post has Full Width Featured Image enabled.
*
* Returns false if not a Single post type or there is no Featured Image selected
* or none of the above conditions are true.
*/
function independent_publisher_has_full_width_featured_image() {
$full_width_featured_image = get_post_meta( get_the_ID(), 'full_width_featured_image' );

// If this isn't a Single post type or we don't have a Featured Image set
if ( ! is_single() || ! has_post_thumbnail() ) {
return false;
}

$full_width_featured_image = get_post_meta( get_the_ID(), 'full_width_featured_image' );
$full_width_featured_image_disabled = get_post_meta( get_the_ID(), 'full_width_featured_image_disabled' );
$independent_publisher_general_options = get_option( 'independent_publisher_general_options' );

// If Auto-Set Featured Image as Post Cover is enabled and it hasn't been disabled for this post, return true.
if ( isset( $independent_publisher_general_options['auto_featured_image_post_cover'] ) && $independent_publisher_general_options['auto_featured_image_post_cover'] && ! $full_width_featured_image_disabled ) {
return true;
}

// If Use featured image as Post Cover has been checked in the Featured Image meta box, return true.
if ( $full_width_featured_image ) {
return true;
} else {
return false;
}

return false; // Default
}

/**
Expand All @@ -585,7 +605,6 @@ function independent_publisher_show_nav_on_single() {
}
}


/**
* Returns true if Show Updated Date on Single Posts option is enabled
*/
Expand All @@ -598,11 +617,23 @@ function independent_publisher_show_updated_date_on_single() {
}
}

/**
* Returns true if Auto-Set Featured Image as Post Cover option is enabled
*/
function independent_publisher_auto_featured_image_post_cover() {
$independent_publisher_general_options = get_option( 'independent_publisher_general_options' );
if ( isset( $independent_publisher_general_options['auto_featured_image_post_cover'] ) && $independent_publisher_general_options['auto_featured_image_post_cover'] ) {
return true;
} else {
return false;
}
}

/**
* Add full-width-featured-image to body class when displaying a post with Full Width Featured Image enabled
*/
function independent_publisher_full_width_featured_image_body_class( $classes ) {
if ( is_single() && has_post_thumbnail() && get_post_meta( get_the_ID(), 'full_width_featured_image', true ) ) {
if ( independent_publisher_has_full_width_featured_image() ) {
$classes[] = 'full-width-featured-image';
}

Expand Down Expand Up @@ -732,25 +763,42 @@ function independent_publisher_first_sentence_excerpt( $text = '' ) {

add_filter( 'the_excerpt', 'independent_publisher_first_sentence_excerpt' );

/**
* Add a checkbox to the featured image metabox
/*
* Add a checkbox for Post Covers to the featured image metabox
*/
function independent_publisher_featured_image_meta( $content ) {

// If we don't have a featured image, nothing to do.
if ( ! has_post_thumbnail() ) {
return $content;
}

global $post;

// Meta key
$meta_key = 'full_width_featured_image';

// Text for checkbox
$text = __( "Use as post cover (full-width)", 'independent-publisher' );

// Meta value ID
$id = 'full_width_featured_image';
$value = esc_attr( get_post_meta( $post->ID, $id, true ) );
// Option type (for use when saving post data in independent_publisher_save_featured_image_meta()
$option_type = "enable";

/* If Auto-Set Featured Image as Post Cover enabled, this checkbox's functionality should reverse and
* allow for disabling Post Covers on a post-by-post basis.
*/
if ( independent_publisher_auto_featured_image_post_cover() ) {
$meta_key = 'full_width_featured_image_disabled';
$text = __( "Disable post cover (full-width)", 'independent-publisher' );
$option_type = "disable";
}

// Get the current setting
$value = esc_attr( get_post_meta( $post->ID, $meta_key, true ) );

// Output the checkbox HTML
$label = '<label for="' . $id . '" class="selectit"><input name="' . $id . '" type="checkbox" id="' . $id . '" value="1" ' . checked( $value, 1, false ) . '> ' . $text . '</label>';
$label = '<label for="' . $meta_key . '" class="selectit"><input name="' . $meta_key . '" type="checkbox" id="' . $meta_key . '" value="1" ' . checked( $value, 1, false ) . '> ' . $text . '</label>';
$label .= '<input type="hidden" name="full_width_featured_image_enable_disable" value="' . $option_type . '">';

$label = wp_nonce_field( basename( __FILE__ ), 'independent_publisher_full_width_featured_image_meta_nonce' ) . $label;

Expand All @@ -759,8 +807,8 @@ function independent_publisher_featured_image_meta( $content ) {

add_filter( 'admin_post_thumbnail_html', 'independent_publisher_featured_image_meta' );

/**
* Save the meta box's post metadata.
/*
* Save the Featured Image meta box's post metadata for Post Cover options.
*/
function independent_publisher_save_featured_image_meta( $post_id, $post ) {

Expand All @@ -778,10 +826,27 @@ function independent_publisher_save_featured_image_meta( $post_id, $post ) {
}

/* Get the posted data and sanitize it for use as an HTML class. */
$new_meta_value = ( isset( $_POST['full_width_featured_image'] ) ? esc_attr( $_POST['full_width_featured_image'] ) : '' );
if ( isset( $_POST['full_width_featured_image'] ) ) {
$new_meta_value = esc_attr( $_POST['full_width_featured_image'] );
$meta_key = 'full_width_featured_image';
} elseif ( isset( $_POST['full_width_featured_image_disabled'] ) ) {
$new_meta_value = esc_attr( $_POST['full_width_featured_image_disabled'] );
$meta_key = 'full_width_featured_image_disabled';
} else {
$new_meta_value = ''; // Empty value means we're unchecking this option
}

// Figure out which option was being unchecked (this routine handles two types)
if ( isset( $_POST['full_width_featured_image_enable_disable'] ) ) {
if ( $_POST['full_width_featured_image_enable_disable'] === 'enable' ) {
$meta_key = 'full_width_featured_image';
} elseif ( $_POST['full_width_featured_image_enable_disable'] === 'disable' ) {
$meta_key = 'full_width_featured_image_disabled';
}
} else {
$meta_key = 'full_width_featured_image'; // Default
}

/* Get the meta key. */
$meta_key = 'full_width_featured_image';

/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );
Expand Down
18 changes: 18 additions & 0 deletions inc/customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ public static function register( $wp_customize ) {
)
);

// Auto-Set Featured Image as Post Cover
$wp_customize->add_setting(
'independent_publisher_general_options[auto_featured_image_post_cover]', array(
'default' => false,
'type' => 'option',
'capability' => 'edit_theme_options',
'sanitize_callback' => 'independent_publisher_sanitize_checkbox',
)
);
$wp_customize->add_control(
'auto_featured_image_post_cover', array(
'settings' => 'independent_publisher_general_options[auto_featured_image_post_cover]',
'label' => __( 'Auto-Set Featured Image as Post Cover', 'independent-publisher' ),
'section' => 'independent_publisher_general_options',
'type' => 'checkbox',
)
);

// Comments Call to Action text
$wp_customize->add_setting(
'comments_call_to_action', array(
Expand Down
10 changes: 4 additions & 6 deletions inc/template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,13 @@ function independent_publisher_get_post_date() {
if ( ! function_exists( 'independent_publisher_full_width_featured_image' ) ):
/**
* Show Full Width Featured Image on single pages if post has full width featured image selected
* or if Auto-Set Featured Image as Post Cover option is enabled
*/
function independent_publisher_full_width_featured_image() {
if ( is_single() && independent_publisher_has_full_width_featured_image() ) {
if ( independent_publisher_has_full_width_featured_image() ) {
while ( have_posts() ) : the_post();
$full_width_featured_image = get_post_meta( get_the_ID(), 'full_width_featured_image' );
if ( $full_width_featured_image ) :
if ( has_post_thumbnail() ) :
the_post_thumbnail( array( 700, 700 ), array( 'class' => 'full-width-featured-image' ) );
endif;
if ( has_post_thumbnail() ) :
the_post_thumbnail( array( 700, 700 ), array( 'class' => 'full-width-featured-image' ) );
endif;
endwhile; // end of the loop.
}
Expand Down

0 comments on commit f37b01c

Please sign in to comment.