Skip to content

Instantly share code, notes, and snippets.

@mostafasoufi
Last active August 22, 2016 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mostafasoufi/1fece8a9a910111957790d07ecf07a25 to your computer and use it in GitHub Desktop.
Save mostafasoufi/1fece8a9a910111957790d07ecf07a25 to your computer and use it in GitHub Desktop.
Wordpress post field validation (Change post status to draft if post is not complete)
<?php
/**
* Change post status to draft if post is not complete
* @param integer $post_id Post ID
* @param object $post Post Object
* @author Mostafa Soufi <mostafa.soufi@hotmail.com>
*/
function validate_post_field($post_id, $post) {
// Check post status
if( $post->post_status != 'publish' )
return;
// Check post title
if( !$post->post_title ) {
// Update post status to draft
draft_post($post_id);
}
// Get category
$category = get_the_category($post_id);
// Check category
if( count($category) >= 1 and $category[0]->term_id == 1) {
// Update post status to draft
$this->draft_post($post_id);
}
// Get tags
$tags = get_the_tags($post_id);
// Check tags
if( !$tags ) {
// Update post status to draft
$this->draft_post($post_id);
}
}
/**
* Change post status to draft
* @param integer $post_id Post ID
*/
function draft_post($post_id) {
// Update post
$post = array(
'ID' => $post_id,
'post_status' => 'draft',
);
// Update the post into the database
wp_update_post( $post );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment