Last active
August 22, 2016 08:24
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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