Last active
August 29, 2015 14:22
-
-
Save eriku/0de2b27829ee255a9a6c to your computer and use it in GitHub Desktop.
Set Featured Image to required and add an asterix symbol to title in Wordpress
This file contains 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 | |
/** | |
* Change the title of the Featured Image box | |
* | |
* @package emu | |
* @subpackage filters+hooks | |
* | |
*/ | |
function emu_change_featured_image_title() { | |
// Replace post_type with the post type you want to have a required Featured Image | |
set_transient('thumbnail_post_type', 'post_type'); | |
// Customize the new Featured Image tile | |
$newTitle = 'Featured Image <abbr class="required">*</abbr>'; | |
// Change the title for the post type above that now require the image | |
remove_meta_box( 'postimagediv', get_transient('thumbnail_post_type'), 'side' ); | |
add_meta_box('postimagediv', __($newTitle), 'post_thumbnail_meta_box', get_transient('thumbnail_post_type'), 'side', 'default'); | |
} | |
add_action('do_meta_boxes', 'emu_change_featured_image_title'); | |
/** | |
* Check to see if a Featured Image has been attached | |
* | |
* @package emu | |
* @subpackage filters+hooks | |
* @param int $post_id Post ID | |
* | |
*/ | |
function emu_check_featured_image($post_id) { | |
// If post type doesn't match current post type then just keep going | |
if( get_post_type($post_id) != get_transient('thumbnail_post_type') ) { | |
delete_transient('thumbnail_post_type'); | |
return; | |
} | |
if ( !has_post_thumbnail( $post_id ) && (get_post_status( $post_id ) == 'publish' || get_post_status( $post_id ) == 'auto-draft') ) { | |
// set a transient to show the users an admin message | |
set_transient('has_post_thumbnail', 'no'); | |
// unhook this function so it doesn't loop infinitely | |
remove_action('save_post', 'emu_check_featured_image'); | |
// update the post set it to draft | |
wp_update_post(array('ID' => $post_id, 'post_status' => 'draft')); | |
add_action('save_post', 'emu_check_featured_image'); | |
} else { | |
delete_transient('thumbnail_post_type'); | |
delete_transient('has_post_thumbnail'); | |
} | |
} | |
add_action('save_post', 'emu_check_featured_image'); | |
/** | |
* Check to see if a Featured Image has been attached, if it hasn't then throw | |
* an error message | |
* | |
* @package emu | |
* @subpackage filters+hooks | |
* | |
*/ | |
function emu_featured_image_error() { | |
// check if the transient is set, and display the error message | |
if ( get_transient( 'has_post_thumbnail' ) == 'no' ) { | |
echo '<div id="message" class="error"><p><strong>You must select a Featured Image. Your post has been saved but it not published.</strong></p></div>'; | |
delete_transient( 'has_post_thumbnail' ); | |
} | |
} | |
add_action('admin_notices', 'emu_featured_image_error'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment