• Hello,

    I have a semi-automated blog witch uses RSS feeds to publish some posts.

    My problem is that when I am authorizing automatic publish I am getting many empty posts published.

    I do not know if the issue is coming from the RSS feed itself or from one of the other plugins I am using in my blog.

    I tried three PHP codes in order to prevent posts with empty content or empty title, my objective is to save those posts as drafts.

    function _myplugin_save_post_check($post_id) {
     
        if ( $parent_id = wp_is_post_revision( $post_id ) ) 
            $post_id = $parent_id;
    
      
        $custom_feilds = get_post_meta( $post_id, 'whatever', true );
    
        if ( empty( $custom_feilds ) ) {
        
            remove_action( 'save_post', '_myplugin_save_post_check' );
    
            wp_update_post( array( 'ID' => $post_id, 'post_status' => 'draft' ) );
    
          
            add_action( 'save_post', '_myplugin_save_post_check' );
        }
    }
    add_action( 'save_post', '_myplugin_save_post_check' );
    add_filter( 'wp_insert_post_data' , 'filter_post_data' , '99', 2 );
    function filter_post_data( $data , $postarr ) {
      
        if( empty($data['post_title']) ) {
          $data['post_status'] = 'draft';
        }
        return $data;
    }
    function minWord($content)
    {
        global $post;
        $content = $post->post_content;
        if (str_word_count($content) < 100 ) //set this to the minimum number of words
        wp_die( __('Error: your post is below the minimum word count. It needs to be longer than 100 words.') );
    }
    add_action('publish_post', 'minWord');

    Apparently, the filter for the title is working but the two filters for the content are absolutely not working.

    I am using Woody ad snippets to add the PHP codes because I am not familiar with PHP.

    I think that the problem is that the posts are maybe modified by another plugin and are saved empty because there is no reason for having this issue after adding the PHP codes.

    Do you have any idea or suggestion about this problem?

    Thank you in advance.

    • This topic was modified 4 years, 5 months ago by zakariarashid.
    • This topic was modified 4 years, 5 months ago by zakariarashid. Reason: One code separated in many fields !
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    ‘wp_insert_post_data’ filter is your best option in all cases because it’s the only filter that fires before posts are saved. Others can be problematic due to a race condition developing. While a large priority argument is a good idea in general, it will not resolve a race condition. You ought to be able to do whatever checks you want from ‘wp_insert_post_data’, not just the title. Don’t rely upon anything in the DB regarding new data since it’s not yet in the DB. New data should be available in $_POST unless it has been previously saved via Ajax, which the block editor will do for some of the data.

    BTW, the priority argument '99' shouldn’t have quotes because it’s an integer argument. It still works because PHP is loosely typed, but to be 100% correct don’t use quotes for this argument.

    Thread Starter zakariarashid

    (@zakariarashid)

    Hello,

    Thank you for your help.

    I am going to try wp_insert_post_data for all codes with priorities and i will be back.

    add_filter( 'wp_insert_post_data' , 'filter_post_data' , 10, 2 );
    function filter_post_data( $data , $postarr ) {
      
        if( empty($data['post_title']) ) {
          $data['post_status'] = 'draft';
        }
        return $data;
    }
    function _myplugin_save_post_check($post_id) {
     
        if ( $parent_id = wp_is_post_revision( $post_id ) ) 
            $post_id = $parent_id;
    
      
        $custom_feilds = get_post_meta( $post_id, 'whatever', true );
    
        if ( empty( $custom_feilds ) ) {
        
            remove_action( 'save_post', '_myplugin_save_post_check' );
    
            wp_update_post( array( 'ID' => $post_id, 'post_status' => 'draft' ) );
    
            add_action( 'save_post', '_myplugin_save_post_check' );
        }
    }
    add_action( 'wp_insert_post_data', '_myplugin_save_post_check', 20, 2 );
    function minWord($content)
    {
        global $post;
        $content = $post->post_content;
        if (str_word_count($content) < 100 ) //set this to the minimum number of words
    	$data['post_status'] = 'draft';
    }
    add_action('wp_insert_post_data', 'minWord', 30, 2);
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Prevent empty posts from publish.’ is closed to new replies.