I have a function that is called at a post updated, inside of this function I am doing something and if this went wrong I want to display an admin_notices.

Inside of my function I tried the following:

if($error) {
    add_action( 'admin_notices', 'my_admin_notice' );

my_admin_notice looks like:

function my_admin_notice() {
        <div class="error">
            <p>Error happend</p>

If I call add_action( 'admin_notices', 'my_admin_notice' ); outside of my function it's working, but unfortunately the notice is display always... :/

Update - Context details:

My function is called with add_filter ( 'publish_post', 'my_function' );

Inside of this function I check if the post is published the first time, than I am doing some stuff and if there is something wrong I want to call the add_action( 'admin_notices', 'my_admin_notice' ); to display an error message...

My function is working well and is only getting called if I want, so that's not the problem...

What I think is that the add_action( 'admin_notices', 'my_admin_notice' ); that I call inside of my function is getting ignored!

Update Code for Context:

function my_admin_notice() {
        <div class="error">
            <p>Error happend</p>

function checkEdit()  {

    if( ($_POST['post_type'] == 'post') && ( $_POST['post_status'] == 'publish' ) &&
     ( $_POST['original_post_status'] != 'publish' ) ) {

    $title =  get_the_title();

    $postId = (string) get_the_ID();

    $postUrl = get_site_url() . "?p=" . $postId;
    $wp_category_array = get_the_category(); 

    $categoryArray = array();
    foreach ($wp_category_array as $wp_category => $value) {
        $categoryArray[] = $value->cat_ID;

    $postDate = get_the_date('Y-m-j H:i:s');
    $subTitle = get_the_date();

    $parse = new parseRestClient(array(
        'restkey' => 'XXXXXXXXXXXXXXXXXXXX'

    $params = array(
        'className' => 'News',
        'object' => array(
            'Title' => $title,
            'SubTitle' => $subTitle,
            'array' => $categoryArray,
            'code' => $postId,
            'Date' => $postDate,
            'Url' => $postUrl

    $request = $parse->create($params);

    $responseArray = json_decode($request, true);

    $objectId = $responseArray['objectId'];

    if ($responseArray ['error'] == true) {
        // Called if error (working)
        add_action( 'admin_notices', 'my_admin_notice' );
    }else {
        $sql = "UPDATE wp_posts SET parseId = '$objectId' WHERE ID = '$postId'"; 
        $result = mysql_query($sql);


add_action ( 'post_updated', 'checkEdit');
  • Can you post your code in context?
    – s_ha_dum
    Mar 5 '14 at 20:56
  • @s_ha_dum it's about a little wordpress plugin i road, which is doing some stuff after a post is published... I updated the question with more details. Mar 5 '14 at 21:10
  • Telling me what the code is "about" doesn't put it the code in context. I still have no idea how it is used.
    – s_ha_dum
    Mar 6 '14 at 13:58
  • @s_ha_dum okay sorry I try it again, the plugin is used to send data to a REST-Api and if the REST-Api return an error I want to display a error message for the user. In the function I am doing a php curl which returns succes or error. If error is returned i want to show the admin notice to the user. Hope this infos helps you :) Mar 6 '14 at 14:41
  • Sorry, I don't mean to be rude, but what would help would be actual working (truncated if necessary) code that I read as a whole, and copy and paste into a dev stack if necessary. Are you attempting to apply an action inside an AJAX request and have it honored on the non-AJAX part of the site?
    – s_ha_dum
    Mar 6 '14 at 14:45

I saw this on stackoverflow, may be able to modify it to work in your situation:

function my_function($post_id){

           set_transient( get_current_user_id().'publisherror', "Error Message" );
    return $post_id;
add_filter ( 'publish_post', 'my_function' );

function show_admin_notice() {
    if($out = get_transient( get_current_user_id().'publisherror' ) ) {
        delete_transient( get_current_user_id().'publisherror' );
        echo '<div class="error"><p>'.$out.'</p></div>';

add_action('admin_notices', "show_admin_notice");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.