Problem with showing admin notice
-
In my WordPress website, I have limited down advertiser users to publish only a single advanced_ad. I have customized this process by adding a custom action which is saving the advertiser user’s advance_ads as drafts if he already has one published advanced ad.
Now I want to show a custom admin notice when the user’s advanced_ad is saved as drafts. I have tried different techniques but there seems to be some problem with my code.
I have already limited down advertiser users from adding only a single product and have added an admin notice if the user already has one published product. The same code (with some modifications for advanced_ad) does not work.
Here is my code:
function published_advanced_ads_limit( $ID, $post ) { $max_posts = 1; $advertiser_user = ''; $author = ''; if(is_current_user_advertiser()){ $author = $post->post_author; } else{ return; } $count = count_user_posts( $author, 'advanced_ads'); if ( $count > $max_posts ) { $post->post_status = 'draft'; wp_update_post( $post); $screen = get_current_screen(); $editPostScreen = ($screen->parent_base == 'edit' && $screen->action == 'edit') ? true : false; if($editPostScreen) { ?> <div class="error notice is-dismissible"> <p> <?php _e( 'The Message', 'advanced-ads' ); ?> </p> </div> <?php } } } add_action( 'publish_advanced_ads', 'published_advanced_ads_limit', 99, 2 );
The topic ‘Problem with showing admin notice’ is closed to new replies.