• Resolved hazratnoor

    (@hazratnoor)


    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 );
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Thomas M

    (@webzunft)

    Hi hazratnoor,

    is this different from https://wordpress.org/support/topic/problem-with-showing-admin-notice/ ?

    Thanks,
    Thomas

    Thread Starter hazratnoor

    (@hazratnoor)

    Hi Thomas!

    There is nothing change in both these questions. I am sorry for that but I was having two reasons in my mind while reposting the question:

    1. To get your instant attention.
    2. To provide the code in a bit more managed order (because the code indentation in my first question was not looking good).

    Thanks
    Usman

    Plugin Author Thomas M

    (@webzunft)

    Hi Usman,

    > 1. To get your instant attention.

    This will get you attention by the forum moderators, too. It also decreases the chance of me helping. Please notice that this is a volunteer forum. If you want our attention then please become a client and contact us through customer support.

    > Now I want to show a custom admin notice when the user’s advanced_ad is saved as drafts.

    I am not sure I understand. You want to show a message when that advertiser changes the ad as draft or someone else did? The latter would mean that the message needed to be persistent.

    Thanks,
    Thomas

    Thread Starter hazratnoor

    (@hazratnoor)

    Hi Thomas!

    You got my point. When an advertiser will try to publish a new ad or click on the publish button on an existing ad, then I want to show the admin notice that he cannot publish more than one ad and his/her ad is saved as drafts.

    The code is already done for saving the ad as drafts. Only need assistance in adding the admin notice before or after wp_update_post( $post);.

    Thanks,
    Usman

    Plugin Author Thomas M

    (@webzunft)

    Hi Usman,

    I would first look into the publish_advanced_ads hook. It is not something in Advanced Ads. Is there a publish_ + post type hook in WordPress? If there is then check if it might not run too late or might not support any output.

    Unfortunately, that is my only idea and I am not able to help more with such a niche problem.

    Best regards,
    Thomas

    Thread Starter hazratnoor

    (@hazratnoor)

    Hi Thomas!

    No worries. I tried myself with a few advanced ads filter-hooks like advanced-ads-notices & advanced-ads-ad-notices. Unfortunately, the advanced-ads-notices filter-hook doesn’t do anything. However, advanced-ads-ad-notices filter-hook output all of the notices (7 for meta boxes on the ad edit screen).

    My actual problem is already resolved (not to allow the advertiser users to publish more than one ad), however, I was further looking for notifying him/her (which doesn’t seem to be possible) that the ad is saved as drafts and that he/she cannot publish another ad.

    Thanks for the detailed support you provided again.

    Plugin Author Thomas M

    (@webzunft)

    Hi hazratnoor,

    thanks for mentioning the filters. That actually helped me to remember the problem.

    Advanced Ads disables the WordPress admin notices on plugin pages because a lot of third-party plugins put irrelevant information and notices on our pages.

    Please give advanced-ads-admin-notices a try.

    Best regards,
    Thomas

    Thread Starter hazratnoor

    (@hazratnoor)

    Hi Thomas!

    I sort out the problem using advanced-ads-ad-notices filter_hook. There was a problem with advanced-ads-ad-notices filter_hook which I already pointed out to you in my earlier reply and now I have resolved that using transient.

    Because, approximately 7 admin notices were appearing when I used the above filter_hook, so I removed/deleted all admin notices except one. My code is listed here and is working fine for me:

    function published_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'); // get author post count
        if ( $count > $max_posts ) {
    		$post->post_status = 'draft';
            wp_update_post( $post);
    		
    		$user_id = get_current_user_id();
    		
    		set_transient( get_current_user_id().'adslimiterror', "Your current advertising package does not allow adding more than one ad!" );
        }
    }
    add_action( 'publish_advanced_ads', 'published_ads_limit', 99, 2 );
    
    function limit_published_advanced_ads( $ID, $post ) {
        global $pagenow;
    	if (( $pagenow == 'post.php' ) && 'advanced_ads' == get_post_type( $_GET['post'] ) && isset($_GET['post'])) {
    		if($out = get_transient( get_current_user_id().'adslimiterror' ) ) {
    			delete_transient( get_current_user_id().'adslimiterror' );
    			echo '<div class="error notice is-dismissible" style="background-color: #ffc9c9;"><p>'.$out.'</p></div>';
    		}
    	}
    }
    add_filter( 'advanced-ads-ad-notices', 'limit_published_advanced_ads', 99, 2 );

    I am marking this reply as resolved.

    Plugin Author Thomas M

    (@webzunft)

    Thanks for sharing your solution!

    Best regards,
    Thomas

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Re: Problem with showing admin notice For Advanced Ads’ is closed to new replies.