How to display an admin-notice after custom post type edit modification

admin2025-01-07  3

I did a limitation for a custom post type "vereine". It works, but the admin notice is that the custom post is published (and not draft). How can I add a message? Here is the code for the limiting

function post_published_limit() {
    $max_posts = 1; // anzahl der maximalen Vereinseintragungen
    $author = $post->vereinsmanager; // nur fuer vereinsmanager limitierung

    $count = count_user_posts( $author, 'vereine'); 

    if ( $count > $max_posts ) {
        // wenn mehr als 1, dann auf Entwurf setzen und Notiz für vereinsmanager ausgeben
        
        $post = array('post_status' => 'draft');
        wp_update_post( $post );
        
    }
}
add_action( 'publish_vereine', 'post_published_limit' );

thank you for your help, greetings

I did a limitation for a custom post type "vereine". It works, but the admin notice is that the custom post is published (and not draft). How can I add a message? Here is the code for the limiting

function post_published_limit() {
    $max_posts = 1; // anzahl der maximalen Vereinseintragungen
    $author = $post->vereinsmanager; // nur fuer vereinsmanager limitierung

    $count = count_user_posts( $author, 'vereine'); 

    if ( $count > $max_posts ) {
        // wenn mehr als 1, dann auf Entwurf setzen und Notiz für vereinsmanager ausgeben
        
        $post = array('post_status' => 'draft');
        wp_update_post( $post );
        
    }
}
add_action( 'publish_vereine', 'post_published_limit' );

thank you for your help, greetings

Share Improve this question edited Apr 26, 2021 at 6:18 Aurovrata 1,34611 silver badges18 bronze badges asked Apr 23, 2021 at 10:55 StephSteph 1
Add a comment  | 

1 Answer 1

Reset to default 0

You need to store the notice in the database so that on page reload the notifiation can be displayed. There are many ways to store the notice in database (I use the Persist Admin Notices template for my plugins), but for the sake of this demonstration I will use a simple time-limited transient,

 add_action( 'publish_vereine', 'post_published_limit' );
 function post_published_limit() {
    $max_posts = 1; // anzahl der maximalen Vereinseintragungen
    $author = $post->vereinsmanager; // nur fuer vereinsmanager limitierung

    $count = count_user_posts( $author, 'vereine'); 

    if ( $count > $max_posts ) {
        // wenn mehr als 1, dann auf Entwurf setzen und Notiz für vereinsmanager ausgeben
        
        $post = array('post_status' => 'draft');
        wp_update_post( $post );
        //store the transient notice to expire in 5 minutes.
        set_transient('vereine_post_notice', 'You can only publish 1 post at a time', 5 * MINUTE_IN_SECONDS);
    }
}

then hook into the admin notice process,

add_action('admin_notices', 'display_notice');
function display_notice(){
  $notice = get_transient('vereine_post_notice');
  if(!empty($notice)){
    echo '<p>'.$notice.'</p>';
  }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736251952a7.html

最新回复(0)