Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Created April 18, 2020 12:48
Show Gist options
  • Select an option

  • Save nfsarmento/066f5535ad54db6c8a2fba2403722ca9 to your computer and use it in GitHub Desktop.

Select an option

Save nfsarmento/066f5535ad54db6c8a2fba2403722ca9 to your computer and use it in GitHub Desktop.
Force WordPress CPT portfolio to Have Excerpts
/**
*
* Force WordPress CPT portfolio to Have Excerpts
* Check if $excerpt is empty and if so set post to draft
*/
if ( ! function_exists( 'envelopedlife_mandatory_excerpt' ) ) {
function envelopedlife_mandatory_excerpt($data) {
//change your_post_type to post, page, or your custom post type slug
if ( 'portfolio' == $data['post_type'] ) {
$excerpt = $data['post_excerpt'];
if (empty($excerpt)) { // If excerpt field is empty
// Check if the data is not drafed and trashed
if ( ( $data['post_status'] != 'draft' ) && ( $data['post_status'] != 'trash' ) ){
$data['post_status'] = 'draft';
add_filter('redirect_post_location', 'envelopedlife_excerpt_error_message_redirect', '99');
}
}
}
return $data;
}
add_filter('wp_insert_post_data', 'envelopedlife_mandatory_excerpt');
}
/**
*
* Force WordPress CPT portfolio to Have Excerpts
* Error direct
*/
if ( ! function_exists( 'envelopedlife_excerpt_error_message_redirect' ) ) {
function envelopedlife_excerpt_error_message_redirect($location) {
$location = str_replace('&message=6', '', $location);
return add_query_arg('excerpt_required', 1, $location);
}
}
/**
*
* Force WordPress CPT portfolio to Have Excerpts
* Admin notice message
*/
if ( ! function_exists( 'envelopedlife_excerpt_admin_notice' ) ) {
function envelopedlife_excerpt_admin_notice() {
if (!isset($_GET['excerpt_required'])) return;
switch (absint($_GET['excerpt_required'])) {
case 1:
$message = 'Envelope text editor field is empty! Envelope text editor is required to publish your envelope post.';
break;
default:
$message = 'Unexpected error';
}
echo '<div id="notice" class="error"><p>' . $message . '</p></div>';
}
add_action('admin_notices', 'envelopedlife_excerpt_admin_notice');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment