How to show an error message after publishing a post?

All we need is an easy explanation of the problem, so here it is.

I’m using the transition_post_status hook to perform some operations after publishing a post. In some conditions I would like to show an error message in a red box under “Edit Post” and above “Post published”:

Post pushlished

How can I do that?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I wouldn’t use that hook. Here’s why

Try something like this using admin_notices.

function wpsites_admin_notice() {
$screen = get_current_screen();
if( 'post' == $screen->post_type
&& 'edit' == $screen->base ){
?>
<div class="error">
    <p><?php _e( 'Updated Demo Message!', 'wpsites' ); ?></p>
</div>
<?php
}}
add_action( 'admin_notices', 'wpsites_admin_notice' );

Untested.

Method 2

add_settings_error(
    'myUniqueIdentifyer',
    esc_attr( 'settings_updated' ),
    $message,
    $type
);

Check

http://codex.wordpress.org/Function_Reference/add_settings_error

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply