admin_notices on runtime in function

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:

Read More
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>
        </div>
    <?php
}

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>
        </div>
    <?php
}

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(
        'appid' => 'XXXXXXXXXXXXXXXXXXXXXXX',
        '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');

Related posts

1 comment

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

    function my_function($post_id){
    
        if($error){
               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");
    

Comments are closed.