Press enter to see results or esc to cancel.

Customizations

Ad Inserter uses WordPress action and filter hooks to insert codes into WordPress pages. Each block has many settings so you can insert it practically anywhere on any page. This will work fine for most websites. However, in some cases you may need additional filters or actions not available in the plugin. For this purpose Ad Inserter offers additional actions, filters and hooks to extend the plugin functionality.

Filter Hooks

Custom block insertion checks

ai_block_insertion_check ($default, $block)

This filter hook is called just before the block is to be inserted – all the settings were already checked. This is a place where you can prevent insertion. $default is the default return value, $block is block number. When the function returns false the block will not be inserted. Example of code:

add_filter ('ai_block_insertion_check', 'insertion_check', 10, 2);

function insertion_check ($default, $block) {
  global $test_counter;
  if (!isset ($test_counter)) $test_counter = 0;
  $test_counter ++;
  return $test_counter == 3;
}

PRO Custom gelolocation services

ai_ip_to_country ($default, $ip, $details)

This filter hook is called before IP address to country lookup. You can use it for custom geolocation services. $default is the default return value, $ip is the IP address of the client, $details is true when country subdivision data is required (ignore it if your database contains only country data). The function should return uppercase two-character country ISO code. When the function returns false the configured Ad Inserter Pro lookup will be used. Code example:

add_filter ('ai_ip_to_country', 'custom_ip_to_country', 10, 3);

function custom_ip_to_country ($default, $ip, $details) {
// $default: default return value (false, means not found and configured Ad Inserter Pro lookup will be used)
// $ip: IP address of the client
// $details: true when country subdivision data is required (ignore it if your database contains only country data)

// DB lookup

  return 'XX'; // Uppercase country ISO code

// for country subdivisions
//   return array ($isoCode, $subdivision, $city); // Complete data 

// when not found
//   return false; // false means not found and configured Ad Inserter Pro lookup will be used 
}

PRO Custom ad blocking detection scripts path

ai_adb_scripts_path ($default)

When ad blocking detection is set to Advanced, Ad Inserter Pro moves scripts for ad blocking detection to a folder with random name. By default the scripts are moved into /wp-content/uploads

This filter hook is called on init action hook just before the scripts used for ad blocking detection are moved. With this filter you can define a custom folder according to your needs. In any case Ad Inserter Pro will either in the default or in the specified folder create subfolders ad-inserter/random-name/ where the scripts will be placed.

The custom folder returned by this filter hook must already exist.

You need to add filter hook before it is used in the init action, for example on action after_setup_theme. Check WP Plugin API Action Reference for the order of WP action hooks.

function adb_scripts_path ($path) {
  return '/wp-content/static';
}

add_filter ('ai_adb_scripts_path', 'adb_scripts_path', 10, 1);

Javascript actions

PRO Manual loading of blocks

ai_load_blocks ([block])

This function loads blocks when they are set to be loaded manually. The block number parameter is optional, if it is not provided all blocks enabled for manual loading (and not loaded yet) will be loaded. Of course, the code for the ads needs to support asynchronous loading (loading after the page is created).


PRO Action when a block is clicked

ai_click_action (block)

When the plugin detects a click on a block with enabled tracking, it calls Javascript function ai_click_action with block number as parameter.


PRO Action for external tracking

ai_external_tracking_event (event_data, category, action, label, non_interaction)

Before the plugin generates event for external tracking it calls Javascript function ai_external_tracking_event with various event data as parameters. category, action and label are defined on the Tracking settings tab, non_interaction is a parameter for the Google analytics events and event_data is defined as object:

var event_data = {'event': event, 'block': block, 'block_name': block_name, 'block_counter': block_counter, 'version': version, 'version_name': version_name};

You can use this function to define custom actions for external tracking, for example, when you are using some unsupported web analytics. When the function ai_external_tracking_event returns 0 no further actions are performed. In other cases tracking event is sent to the Google Analytics or Piwik web analytics.