AdsforWP Shortcodes

Below, you’ll find a detailed guide on how to add the Google Adsense & Banner Ads by AdsforWP Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Google Adsense & Banner Ads by AdsforWP Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Google Adsense & Banner Ads by AdsforWP Plugin and the shortcodes it provides:

Plugin Icon
Google Adsense & Banner Ads by AdsforWP

"Google Adsense & Banner Ads by AdsforWP is a robust plugin that easily integrates Google Adsense and custom banner ads into your WordPress site. Boost revenue and enhance user experience effortlessly."

★★★★✩ (42) Active Installs: 5000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [adsforwp]
  • [adsforwp-group]

AdsforWP [adsforwp] Shortcode

The AdsforWP shortcode facilitates manual ad placement. It’s defined by ‘adsforwp’, and the associated function ‘adsforwp_manual_ads’ fetches the ad code based on the provided ID.

Shortcode: [adsforwp]

Parameters

Here is a list of all possible adsforwp shortcode parameters and attributes:

  • id – Unique identifier of the advertisement

Examples and Usage

Basic example – A simple usage of the adsforwp shortcode to display an ad with a specific ID.

[adsforwp id=1 /]

Advanced examples

Using the shortcode to display an ad with a specific ID and type. If the ad with the given ID is not found, the shortcode will not return any output.

[adsforwp id=1 type="AD" /]

Using the shortcode to display an ad with a specific ID and type. The type parameter is optional, and if not provided, the shortcode will default to type “AD”.

[adsforwp id=1 /]

Please note that the ‘id’ attribute is mandatory for the shortcode to work. If the ‘id’ attribute is not provided, the shortcode will not return any output.

PHP Function Code

In case you have difficulties debugging what causing issues with [adsforwp] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('adsforwp', array($this,'adsforwp_manual_ads'));

Shortcode PHP function:

function adsforwp_manual_ads($atts) {	
        
        $post_ad_id =   $atts['id'];  
        
        if($post_ad_id){
                                 
            $ad_code =  $this->adsforwp_get_ad_code($post_ad_id, $type="AD");          
            return $ad_code;     
          
        }
            
    }

Code file location:

ads-for-wp/ads-for-wp/output/functions.php

AdsforWP [adsforwp-group] Shortcode

The ‘adsforwp-group’ shortcode from the Ads-for-WP plugin is used to display ad groups. It checks if the ad conditions are met, then retrieves and displays the ad group. The shortcode works in both AMP and non-AMP environments. It also checks for ad expiry and handles ad alignment and margins for better placement.

Shortcode: [adsforwp-group]

Parameters

Here is a list of all possible adsforwp-group shortcode parameters and attributes:

  • id – Unique identifier for the ad group
  • group_id – Optional, specifies a specific ad group
  • widget – Optional, specifies if ad is a widget
  • all_condition_status – Optional, checks all conditions for ad

Examples and Usage

Basic example – Displaying a group of ads by referencing the group ID.

[adsforwp-group id=1 /]

Advanced examples

Displaying a group of ads by referencing the group ID and setting the widget parameter to ‘widget’. This will display the ads regardless of the ad status.

[adsforwp-group id=1 widget="widget" /]

Displaying a group of ads by referencing the group ID, setting the widget parameter to ‘widget’, and setting the all_condition_status parameter to true. This will display the ads regardless of the ad status and the conditions set in the plugin settings.

[adsforwp-group id=1 widget="widget" all_condition_status="true" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [adsforwp-group] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('adsforwp-group', array($this, 'adsforwp_group_ads'));

Shortcode PHP function:

function adsforwp_group_ads($atts, $group_id = null, $widget=null, $all_condition_status = null) { 
                       
        if ((function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint()) || function_exists( 'is_amp_endpoint' ) && is_amp_endpoint()) {
            $this->is_amp = true;        
        }        
        $post_group_id  =   adsforwp_rmv_warnings($atts, 'id', 'adsforwp_string'); 
        
        if($group_id){
            
            $post_group_id  =   $group_id;     
        
        } 
        
        $service = new adsforwp_output_service();
        $ad_status = $service->adsforwp_is_condition($post_group_id);
        
        if($ad_status || $widget =='widget' || $all_condition_status){
                                                               
        $ad_alignment  = $wheretodisplay = '';                        
        $ad_margin_top = $ad_margin_bottom = $ad_margin_left = $ad_margin_right = 0;
                        
        $post_group_data                = get_post_meta($post_group_id,$key='adsforwp_ads',true);
        $post_group_meta                = get_post_meta($post_group_id,$key='',true);
        $margin_post_meta               = get_post_meta($post_group_id, $key='adsforwp_ad_margin',true);
        
        
        $ad_margin_top                  = adsforwp_rmv_warnings($margin_post_meta, 'ad_margin_top', 'adsforwp_string');                    
        $ad_margin_bottom               = adsforwp_rmv_warnings($margin_post_meta, 'ad_margin_bottom', 'adsforwp_string');                  
        $ad_margin_left                 = adsforwp_rmv_warnings($margin_post_meta, 'ad_margin_left', 'adsforwp_string');                
        $ad_margin_right                = adsforwp_rmv_warnings($margin_post_meta, 'ad_margin_right', 'adsforwp_string');  
        
        $wheretodisplay                 = adsforwp_rmv_warnings($post_group_meta, 'wheretodisplay', 'adsforwp_array');                                
        
        if(isset($post_group_meta['adsforwp_ad_align'])){
            
        $ad_alignment      = $post_group_meta['adsforwp_ad_align'][0];    
        
        }
                       
        $ad_code            ="";  
        $group_ad_code      ="";
        $filter_group_ids   = array();   
        
        if($this->is_amp){            
                            
        if($post_group_data){
            
           foreach ($post_group_data as $group_id=>$value){
            if(get_post_status($group_id) == 'publish'){
              $output_service = new adsforwp_output_service();                    
                $expiry_status = $output_service->adsforwp_check_ad_expiry_date($group_id);
                if( $expiry_status){
                  $filter_group_ids[$group_id] = $value; 
                }
            }
            
         } 
         if($filter_group_ids){
             $ad_code =  $this->adsforwp_get_ad_code(array_rand($filter_group_ids), $type="GROUP");              
         }
        
        
        }  
               
        }else{   
                                
        $post_data = get_post_meta($post_group_id,$key='',true);  
        
        if($post_group_data){
            
        $adsresultset = array();  
        $response = array();    
        
        foreach($post_group_data as $post_ad_id => $post){
        
        $select_ad_type     = '';
        $data_cid           = '';
        $data_crid          = '';
        $ad_detail          = get_post_meta($post_ad_id,$key='',true);                  
        $select_ad_type     = adsforwp_rmv_warnings($ad_detail, 'select_adtype', 'adsforwp_array');                   
        $data_cid           = adsforwp_rmv_warnings($ad_detail, 'data_cid', 'adsforwp_array');                   
        $data_crid          = adsforwp_rmv_warnings($ad_detail, 'data_crid', 'adsforwp_array');                   
        
        if(!empty($ad_detail) && $select_ad_type !='' && get_post_status($post_ad_id) == 'publish'){
          $output_service = new adsforwp_output_service();               
          $expiry_status = $output_service->adsforwp_check_ad_expiry_date($post_ad_id);
          if($expiry_status){
            $adsresultset[] = array(
                'ad_id'                     => $post_ad_id,
                'ad_type'                   => adsforwp_rmv_warnings($ad_detail, 'select_adtype', 'adsforwp_array'),
                'ad_adsense_type'           => adsforwp_rmv_warnings($ad_detail, 'adsense_type', 'adsforwp_array'),
                'ad_custom_code'            => adsforwp_rmv_warnings($ad_detail, 'custom_code', 'adsforwp_array'),
                'ad_data_client_id'         => adsforwp_rmv_warnings($ad_detail, 'data_client_id', 'adsforwp_array'),
                'ad_data_ad_slot'           => adsforwp_rmv_warnings($ad_detail, 'data_ad_slot', 'adsforwp_array'),
                'ad_data_cid'               => $data_cid,
                'ad_data_crid'              => $data_crid,
                'ad_banner_size'            => adsforwp_rmv_warnings($ad_detail, 'banner_size', 'adsforwp_array'),
                'ad_image'                  => adsforwp_rmv_warnings($ad_detail, 'adsforwp_ad_image', 'adsforwp_array'),
                'ad_redirect_url'           => adsforwp_rmv_warnings($ad_detail, 'adsforwp_ad_redirect_url', 'adsforwp_array'),
                'ad_rel_attr'           => adsforwp_rmv_warnings($ad_detail, 'adsforwp_ad_rel_attr', 'adsforwp_array'),
                'ad_img_height'             => adsforwp_rmv_warnings($ad_detail, 'adsforwp_ad_img_height', 'adsforwp_array'),
                'ad_img_width'              => adsforwp_rmv_warnings($ad_detail, 'adsforwp_ad_img_width', 'adsforwp_array'),                
        ) ; 
          }
        
        
        }
        
        }
        $response['afw_group_id'] = $post_group_id;
        
        $response['adsforwp_refresh_type']           = adsforwp_rmv_warnings($post_data, 'adsforwp_refresh_type', 'adsforwp_array');                
        $response['adsforwp_group_ref_interval_sec'] = adsforwp_rmv_warnings($post_data, 'adsforwp_group_ref_interval_sec', 'adsforwp_array');               
        $response['adsforwp_group_type']             = adsforwp_rmv_warnings($post_data, 'adsforwp_group_type', 'adsforwp_array');
        
        $response['ads'] = $adsresultset;  
        if($response['adsforwp_refresh_type'] == 'on_interval'){
                
        $ad_code ='<div class="afw-groups-ads-json" afw-group-id="'.esc_attr($post_group_id).'" data-json="'. esc_attr(json_encode($response)).'">';           
        $ad_code .='</div>';
        $ad_code .='<div style="display:none;" data-id="'.esc_attr($post_group_id).'" class="afw_ad_container_pre"></div><div data-id="'.esc_attr($post_group_id).'" class="afw afw_ad_container"></div>';
        
        
        }else{
                
        $post_group_data = get_post_meta($post_group_id,$key='adsforwp_ads',true);  
        
        foreach ($post_group_data as $group_id=>$value){
            
            if(get_post_status($group_id) == 'publish'){
              $output_service = new adsforwp_output_service();
              $expiry_status = $output_service->adsforwp_check_ad_expiry_date($group_id);   
              if($expiry_status){
                $filter_group_ids[$group_id] = $value; 
              }
              
            }
            
           }       
            if($filter_group_ids){
                $ad_code =  $this->adsforwp_get_ad_code(array_rand($filter_group_ids), $type="GROUP"); 
            }
                              
          }        
         }
                              
       } 
                                      
       $group_ad_code  = '<div data-id="'.esc_attr($post_group_id).'" style="text-align:'.esc_attr($ad_alignment).'; margin-top:'.esc_attr($ad_margin_top).'px; margin-bottom:'.esc_attr($ad_margin_bottom).'px; margin-left:'.esc_attr($ad_margin_left).'px; margin-right:'.esc_attr($ad_margin_right).'px;" class="afw afw_group afw_group afwadgroupid-'.esc_attr($post_group_id).'">';
       $group_ad_code .= $ad_code;
       $group_ad_code .='</div>';       
              
       return $group_ad_code;
       
      }    
               
    }

Code file location:

ads-for-wp/ads-for-wp/output/functions.php

Conclusion

Now that you’ve learned how to embed the Google Adsense & Banner Ads by AdsforWP Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *