Monday, November 14, 2022
HomeWordPress Developmentphp - Set put up to draft if matching phrase is discovered...

php – Set put up to draft if matching phrase is discovered within the put up


Undecided what precisely I am lacking right here however I am making an attempt to set a put up to draft if one of many phrases are detected, it should solely publish the put up if the consumer removes the phrase discovered throughout the put up. For the time being it detects a phrase however by no means publishes it if I take away it.

One other downside I am dealing with is, when the phrase hell is being blocked, it picks up as a blocked phrase when typing howdy, how can I get round this?

operate jhnppdraft( $post_id, $put up ) {

    $prohibited_words = explode( ',', 'word1, word2, word3' );
    $found_words      = array();
    $content material          = $post->post_content;
    foreach( $prohibited_words as $phrase ) {
    if( strpos( $content material, $phrase ) !== false )
      $found_words[] = $phrase;
  }
    /** verify if phrases are discovered */
    if ( $prohibited_words ) {

        /** set put up standing to draft */
        wp_update_post( array(
            'ID'          => $post_id,
            'post_status' => 'draft',
        ) );
        /** present error if phrases are used */
        wp_die(
    sprintf(
      __(
        'Your put up has been set to draft because it comprises the next phrases: ("%s"). Please take away them and take a look at publishing once more.',
        'jhnpp'
      ),
      implode( '", "', $found_words )
    )
  );
    }
}

add_action( 'publish_post', 'jhnppdraft', 10, 2 );

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments