0

I'm trying to prevent a post from being published if it contains certain words using the folowing:

function jhnppWord($content)
{
    global $post;
        $content = $post->post_content;
        $words = 'word1';
            if (strpos($content, '$words') !== false ) 
                wp_die( __('Your post contains words that we do not allow. Please remove them and try again.') );
}
add_action('publish_post', 'jhnppWord');

With this, it either blocks any word you type or nothing at all, what am I missing?

How can I add multiple words to $words?

0

2 Answers 2

0

The argument syntax in your strpos() call is such that it's testing the content for the literal string "$words" instead of the content of the $words variable.

publish_post action hooks do not directly receive a post's content as an argument. However if you use the provided arguments as detailed by the documentation, it is not necessary to retrieve the $post global.

To test for multiple words, the easiest solution may be to place the words in an array, then loop through it to test each individually.

If you don't care about conveying which prohibited words were discovered, you can wp_die() within the loop as soon as any word is found. It may be more useful to test them all and tell the end-user which were discovered, however.

All together:

function jhnppWord( $post_id, $post ) {
  $prohibited_words = array(
    'foo',
    'bar',
    'baz',
    'baq',
  );
  $found_words      = array();
  $content          = $post->post_content;

  foreach( $prohibited_words as $word ) {
    if( strpos( $content, $word ) !== false )
      $found_words[] = $word;
  }

  if( count( $found_words ) === 0 )
    return;

  wp_die(
    sprintf(
      __(
        'Your post contains words that we do not allow ("%s"). Please remove them and try again.',
        'jhnpp'
      ),
      implode( '", "', $found_words )
    )
  );
}

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

Instead of hard-terminating the publish request, a more user-friendly alternative may be to revert the transition to publish-status and set a message to be displayed within the dashboard UI, and/or implement the test in JavaScript to provide feedback prior to the user submitting the post.

6
  • Only saw this after I posted the other answer, this one is nice since it displays the blocked word. Thanks! Aug 22, 2022 at 16:57
  • Is there any possible way to list these words in this format? word1,word2,word3,word4,word5 etc. We have some common blocked words and they are all listed like the above, around 500+ words so I guess you see it would be great to list them out like this ^^ Sep 23, 2022 at 11:51
  • If they're stored in an array, implode( '", "', $words_array ) will produce that string
    – bosco
    Sep 23, 2022 at 16:27
  • Just to confirm, currently we have: 'foo', 'bar', 'baz', 'baq', Can these be listed in the following format and it will still work?: 'foo,bar,baz,baq' Sep 23, 2022 at 17:22
  • Oh I see. Sure. You can create the array by exploding the string on the comma: $prohibited_words = explode( ',', $biglongwordsstring ); (where you could also just replace $biglongwordsstring with the actual string 'foo,bar,baz,baq' if desired).
    – bosco
    Sep 23, 2022 at 17:43
0

Thanks to Lana Codes with a slight adjustment. Ref: https://wordpress.stackexchange.com/a/408850/225083

/**
 * Set post status to draft
 * if content contains certain words
 *
 * @param $post_id
 * @param $post
 */
function wp_jhnpp_post_draft_content_contains_certain_words( $post_id, $post ) {

    $is_contains_certain_words = in_array( true, array(
        strpos( $post->post_content, 'word1' ),
        strpos( $post->post_content, 'word2' ),
    ) );

    /** check is contains certain words */
    if ( $is_contains_certain_words ) {

        /** show error if words are used */
        wp_die( __('Your post contains words that we do not allow. Please remove them and try again.') );
    }
}

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

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.