How to throw error to user when saving post

You can use admin_notices hook
http://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices

For example:

function ravs_admin_notice() {
    ?>
    <div class="error">
        <p><?php _e( 'Article with this title is not found!', 'my-text-domain' ); ?></p>
    </div>
    <?php
}

on publish_{your_custom_post_type} hook
http://codex.wordpress.org/Post_Status_Transitions

function on_post_publish( $ID, $post ) {
    // A function to perform actions when a {your_custom_post_type} is published.
    $post_exists = $wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name="" . $_POST["article_name'] . "' AND post_type="post"", 'ARRAY_A');
    if($post_exists)
        update_post_meta($id, 'article_name', strip_tags($_POST['article_name']));
    else
       add_action( 'admin_notices', 'ravs_admin_notice' );
}
add_action(  'publish_{your_custom_post_type}',  'on_post_publish', 10, 2 );

Leave a Comment