• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Notes to Self

  • Aviation Weather from NOAA Plugin
    • KZZV
    • Bad Shortcode
    • Simple Metar
  • Meetings

Mark Chouinard / December 17, 2014

Require Featured Image

add_action( 'save_post', 'save_post_example' );
add_action( 'admin_notices', 'admin_notices_example' );
function save_post_example( $post_id ) {

// change to any custom post type 
if( get_post_type( $post_id ) != 'post' ) {
return;
}

if ( ! has_post_thumbnail( $post_id ) ) {

// 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', 'wpds_check_thumbnail' );

// update the post set it to draft
wp_update_post( array( 'ID' => $post_id, 'post_status' => 'draft' ) );
add_action( 'save_post', 'wpds_check_thumbnail' );

} else {
delete_transient( 'has_post_thumbnail' );
}

}

function admin_notices_example() {

// 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>You must select a featured image for your post.</p></div>';
delete_transient( 'has_post_thumbnail' );
}

}

 

Filed Under: WordPress Tagged With: Actions

Primary Sidebar

Post Archives

  • June 2024
  • December 2022
  • July 2022
  • December 2021
  • October 2021
  • June 2021
  • February 2021
  • October 2020
  • September 2020
  • August 2020
  • March 2020
  • February 2020
  • December 2019
  • September 2019
  • June 2017
  • May 2016
  • April 2016
  • March 2016
  • September 2015
  • August 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • June 2013
  • May 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • January 2011

Footer

Copyright © 2025 · machouinard · Log in