0

Not sure what exactly I'm missing here but I'm trying to set a post to draft if one of the words are detected, it will only publish the post if the user removes the word found within the post. At the moment it detects a word but never publishes it if I remove it.

Another problem I'm facing is, when the word hell is being blocked, it picks up as a blocked word when typing hello, how can I get around this?

function jhnppdraft( $post_id, $post ) {

    $prohibited_words = explode( ',', 'word1, word2, word3' );
    $found_words      = array();
    $content          = $post->post_content;
    foreach( $prohibited_words as $word ) {
    if( strpos( $content, $word ) !== false )
      $found_words[] = $word;
  }
    /** check if words are found */
    if ( $prohibited_words ) {

        /** set post status to draft */
        wp_update_post( array(
            'ID'          => $post_id,
            'post_status' => 'draft',
        ) );
        /** show error if words are used */
        wp_die(
    sprintf(
      __(
        'Your post has been set to draft since it contains the following words: ("%s"). Please remove them and try publishing again.',
        'jhnpp'
      ),
      implode( '", "', $found_words )
    )
  );
    }
}

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

1 Answer 1

0

I rewrote the text of the code for you, there are only a few things I have to tell you.

1 You should put the words you want to search in the $string variable.

2 To solve the problem of finding hello and hell at the same time, I used the preg_match function and the problem was solved and now it only finds one word.

preg_match("/\b{$url}\b/", $content)

3 I have used WordPress admin_notices to display the error message, but in order to delete the message after a certain time, I used the set_transient() function. In this function, you can set your time instead of 10 seconds .

Well, the original code is here:


function jhnppdraft($post_id, $post)
{
    /** Set harmful words in this array */
    $string = " hello ,  hell   , pineapple ";

    $prohibited_words = explode(",", $string);
    $content = $post->post_content;

    /** Search for words in the content of the post */
    $searched = [];
    foreach ($prohibited_words as $url) {
        $url = trim($url);
        if (preg_match("/\b{$url}\b/", $content)) {
            $searched[] = $url;
        }
    }
    /** check if words are found */
    if (!empty($searched)) {
        /** set post status to draft */
        wp_update_post(array(
            'ID' => $post_id,
            'post_status' => 'draft',
        ));
        /** show error if words are used */
        $text = sprintf(
            __(
                'Your post has been set to draft since it contains the following words: ("%s"). Please remove them and try publishing again.',
                'jhnpp'
            ),
            implode('", "', $searched)
        );
        /** set_transient Set to show general_admin_notice() */
        set_transient('jhnpp', $text, 10); // You can set the display time of admin_notices to seconds

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

Error message display section:


function general_admin_notice()
{
    // Display the error text here
    $transient = get_transient('jhnpp');
    if(!$transient) return;

    echo '<div class="notice notice-warning is-dismissible">
             <p>' . $transient. ' </p>
         </div>';
}

add_action('admin_notices', 'general_admin_notice');

Here you can see that it recognized the word hello and did not recognize the word hell.

wordpress edit post

3
  • Thanks for this, this works great! I did notice one thing however, it only works with classic editor? Commented Nov 14, 2022 at 15:24
  • You have to use classic for now to validate the post. I don't think you have a hook for Gutenberg editor. Because it's ajax. Commented Nov 14, 2022 at 15:36
  • If I find a solution I will definitely update this answer my friend. Commented Nov 14, 2022 at 16:03

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.