Wordpress

How to Prevent Publishing a Post if The Content Contains Certain Words

20 AUG 2022

Usually post content in WordPress is generated by registered and trusted users and filtering the content before publishing is not a concern.

But while developing a plugin or a theme, it may be necessary to allow untrusted users to post content.

For example, say, you are taking guest posts in a WordPress site. People will post content that may not be suitable for your site.

In that case you can send all your guest posts to draft mode. Review the posts and release for publication if the content meets your criteria.

But what about holding content that contains some specific keywords and let others get published?

I faced this situation in one of the sites made for client. Let us see how to get it done.

Typically wp_insert_post is used to insert a post dynamically. The syntax of this function is as follows –

wp_insert_post( $args )

There are several parameters accepted by the function. But for this example, only the most important ones will be used.

$title = 'post title content';
$content = 'the post content';

$args = array(
  'post_title'    => wp_strip_all_tags( $title ),
  'post_content'  => $content,
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array( 1, 2 )
);

wp_insert_post( $args )

The parameter most important here is $content which will be checked for some specific words.

The source of $content can be from a form submission, from a file or any other source you want. Usually guest posts are done through a form submission.

Assuming that the content is from a form submission, when you receive the $content, store it in a variable –

$content = $_POST['content'];

Where $_POST[‘content’] is posted from a form with the content form field containing the post content. As stated previously, you can get the content from anywhere you want.

Next, use the function explode to break down the content string to a PHP array.

$content = explode( " ", $content );

Now you have an array of words which can be iterated.

The specific keywords that you want to check the content for can be stored in another array.

// example of specific keywords, can be replaced with value that apply to your case
$banned_words = array(
   'ammunition',
   'fire',
   'fuel',
   'matches'
);

Use the foreach loop to iterate through the array.

$banned_words_found = null;

foreach( $content as $word ) {
     if ( in_array( $word, $banned_words ) ) {
        $banned_words_found[] = $word;
     }
}

Here $banned_words_found is a variable with initial value null.

When the foreach loop is run, this variable becomes an array and stores any specific keyword found.

So, if $banned_words_found is still null after the foreach loop, that means no specific keywords were found, and if it is an array that we can be sure that one or more specific keywords were found and prevent the publication of the post.

Put this code inside a function and call the function just before you insert the post into database.

function has_banned_words( $content ) {

    $content = explode( " ", $content );
    $banned_words = array(
        'ammunition',
        'fire',
        'fuel',
        'matches'
    );

    $banned_words_found = null;

    foreach( $content as $word ) {
         if ( in_array( $word, $banned_words ) ) {
             $banned_words_found[] = $word;
         }
     }

     if ( is_array( $banned_words_found ) ) return true;

     else return false;
    
}

Now you can call this function just before insertion of the post and change the status of the post to draft if it contains specific keywords.

// call to accept the content of submission form or any other source
$has_banned_keywords = has_banned_keywords();

if( $has_banned_keywords ) {
    $status = 'draft';
}

$args = array(
  'post_title'    => wp_strip_all_tags( $title ),
  'post_content'  => $content,
  'post_status'   => $status,
  'post_author'   => 1,
  'post_category' => array( 1, 2 )
);

wp_insert_post( $args ); // or any other mechanism to insert post.

With this checking in place you can convert the status of the post to several other values and get your desired result.

The status values you can choose from are usually – future, pending, draft, trash, private. There are a few other statuses too, but they are infrequently used.

Every business deserves a website
हर एक बिजनेस के लिए एक वेबसाइट होना जरूरी है

GET YOUR'S NOW

Address

411 Kalikapur

Ishani Apartments, Kolkata - 99

Contacts

support@webdesignvista.com
Phone: +91 9903490874
Fax: +91 9903490874