Skip to content

Instantly share code, notes, and snippets.

@mostafasoufi mostafasoufi/functions.php
Last active Aug 22, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.