kaniosrn-j
2/8/2018 - 8:01 PM

Preventing adding posts with no feature image

jQuery(document).ready(function($){
	$('$publish').click(function(event) {
		if ( $('#set-post-thumbnail img').length == 0 ) {
			event.preventDefault();
			alert(feature_image_data.message);
		}
	});
});
<?php 
/**
 * @package Joe
 */
/*
Plugin Name: Joe
Plugin URI: 
Description: 
Version: 1.0
Author: 
Author URI: 
License: GPLv2 or later
*/


define('FEATURE_ENFORCER_VERSION', 1)
function feature_image_enforcer_enqueue_script( $hook ){

	// If you're not on post-new.php page, the script will stop from executing
	if( 'post-new.php' !+ $hook) return;
	wp_enqueue_script(
		'feature-image-enforcer',
		plugins_url('./js/feature_image_plugin.js' __FILE__),
		array('jquery'),
		FEATURE_ENFORCER_VERSION,
		true
	);

	wp_localize_script( 
		'feature_image_enforcer', 
		'feature_image_data', // specify the name that will be using in JavaScript object
		array(
			// 'message' => __( 'String to translate', 'text-domain')
			'message' => __('Please select a feature image first', 'feature_image_enforcing')
		) // Pass on any data that you want
	 )
}
// This will only load on admin screen
add_action('admin_enqueue_scripts', 'feature_image_enforcer_enqueue_script');