1

I created custom post type Testimonials and made the title required using the code in this link:

Require title for pages

Now I applied the code in order to make the content required, and it works fine.

Just when I click Publish button and editor is empty, an alert message appears at the top of the screen that the content is required.

But here is the problem: when I enter some content and click Publish button, the alert message displayed again.

Here is the used code:

function force_testimonials_post_content_init() {
    wp_enqueue_script('jquery');
}

function force_testimonials_post_content() {
    if ( get_post_type( get_the_ID() ) == 'testimonials' ) {
        echo "<script type='text/javascript'>\n";
        echo "
        jQuery('#publish').click(function() {
            var contentvar = jQuery('[id^=\"postdivrich\"]')
                                .find('.mce-container-body');
            if (contentvar.val().length < 1) {
                jQuery('[id^=\"postdivrich\"]').css('background', '#fff3cd');

                setTimeout(\"jQuery('#ajax-loading').css('visibility', 'hidden');\", 100);

                alert('TESTIMONIAL POST CONTENT IS REQUIRED');

                setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
                    
                return false;
            }
        });
        ";
        echo "</script>\n";
    }
}
add_action('admin_init', 'force_testimonials_post_content_init');
add_action('edit_form_advanced', 'force_testimonials_post_content');
 // Add this row below to get the same functionality for page creations.
add_action('edit_page_form', 'force_testimonials_post_content');

1 Answer 1

0

the content_save_pre filter. It checks the post type of the current post and ensures that the content field is not empty. If the content field is empty, the function calls wp_die() to display an error message and stop the post from being saved.

Note: Make sure to replace "custom_post_type" with the actual name of your custom post type. Also, you can modify the error message to fit your needs.

add_filter( 'content_save_pre', 'validate_custom_post_type_content' );
function validate_custom_post_type_content( $content ) {
    global $post;
    if ( 'custom_post_type' === $post->post_type && empty( $content ) ) {
        wp_die( __( 'The content field is required for this custom post type. Please enter some content and try again.' ) );
    }
    return $content;
}
2
  • useful answer , this code working fine , but there is a warning appear in top of screen says: Warning: Attempt to read property "post_type" on null in E:\xampp\htdocs\repo\wp-content\plugins\consulty-core\custom-posts\testimonials\testimonials.php on line 285 , consulty-core is a plugin I created for this theme includes custom posts and custom widget. @vijay-rathod Feb 6, 2023 at 11:12
  • the warning line appear when open "add new testimonials" screen , then disappear after publish post Feb 6, 2023 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.