Allow custom meta boxes on ad edit screen

We provide the code snippets and content on this page without any guarantee or support. In most cases, these pages are created on requests or based on individual solutions sent to us by other users. After it was published, we are only reviewing the code and content when we are made aware of an issue. Please let us know if you find one.

Please only use the codes if you understand them and know how to add custom code to your WordPress site.

Advanced Ads automatically removes custom meta boxes on the ad edit screen. This is needed, because sometimes, plugin authors forgot to specify that their meta box is only meant for public post types, like posts and pages. Users then might have seen an SEO-related box on the ad pages, which neither worked nor made sense.

If you are a developer and know what you are doing then it might still be needed to have a few custom fields in a meta box on the ad edit page. This page also includes an example that works auto of the box using the Advanced Custom Fields plugin.

Adding your own meta box

With the following code in the functions.php file of your theme, you can whitelist a meta box with the ID my-meta-box.

add_filter( 'advanced-ads-ad-edit-allowed-metaboxes', 'my_advanced_ads_whitelist_meta_box');
function my_advanced_ads_whitelist_meta_box( $meta_boxes ){
    $meta_boxes[] = "my-meta-box"; // ID of the meta box
    return $meta_boxes;
}Code language: PHP (php)

Finding the meta box ID is a tough task here. It is normally given where the meta box was registered. You can also find it in the source code of the displayed meta box when you see it also on another post type.

Adding an ACF meta box manually

The popular Advanced Custom Fields plugin allows you to add custom fields in a meta box on the ad edit page.

The difficulty is to find the meta box ID because it is generated dynamically. On the Edit Field Group page in ACF, just open the Screen Options at the top right and enable Slug. This should display a field like below on the ACF edit page.

Advanced Custom Fields Slug field

Just add the acf- prefix and you have the meta box ID, e.g. group_5c24d33c7da03 becomes acf-group_5c24d33c7da03.

The following code shows how to whitelist this meta box.

add_filter( 'advanced-ads-ad-edit-allowed-metaboxes', 'my_advanced_ads_whitelist_meta_box');
function my_advanced_ads_whitelist_meta_box( $meta_boxes ){
    $meta_boxes[] = "acf-group_5c24d33c7da03";
    return $meta_boxes;
}Code language: PHP (php)

Field groups set up for Ads show up for all ad types. You need custom code to change that behavior.

Subscribe to our newsletter and get 2 add-ons for free!

* indicates required

Adding an Advanced Custom Fields meta box automatically

Advanced Ads can automatically detect when a field group created in Advanced Custom Fields was whitelisted for the ad edit page. You could use this to build an ad template.

Go to Custom Fields > Field Groups and create a new group.

My ad template is going to use five fields:

  • Title
  • Content
  • Call to action
  • Target URL
  • Image

The Image field uses the “Image URL” as the Return Format. Otherwise, you can set up the behavior of the fields to your liking. Just make sure to use the exact Names later in your template.

I am also setting up the meta box to only be visible on the “Ad” post type, which is equal to ads managed in Advanced Ads. If you don’t limit it then the fields will also show up on the post or page edit screens.

Ad Options field group in Advanced Custom Fields

Make sure that your field group is activated and save it.

You can import the field group setup I used. Just download this file.

Using ACF functions in the ad content

When using Advanced Custom Fields functions in the ad content, e.g., the_field() to output a value, you need to consider the following:

  • use the “Plain text and code” ad type
  • enable the “Allow PHP” option
  • wrap PHP functions with <?php ... ?>
  • use conditions like if( function_exists( 'the_field' ) ) { the_field( ... ) } to make sure the ad is not breaking when you disable the ACF plugin
  • Add the ad ID to the function. Otherwise, ACF will try to query the field from the post or page, e.g., the_field( 'title', 123 ) instead of just the_field( 'title' )

To set up the ad template for our example above, go to Advanced Ads > Ads and create a new ad. Choose the “plain text and code” ad type.

Close the Wizard to see all options on the ad edit screen. The “Ad Options” field group from above automatically shows up. Fill in the information for this ad like in the example below.

The Ad Options field group with sample data on the ad edit screen.

I can now write the ad template into the code field. This should be done once by an experienced (frontend) developer and then serve as a template for more ads.

In my simple case, it looks like this:

<h4>
	<?php the_field( 'title', 219 ); ?>
</h4>
<p>
	<img src="<?php the_field( 'image', 219 ); ?>" style="float: left;"/><?php the_field( 'content', 219 ); ?>
</p>
<p><a href="<?php the_field( 'url', 219 ); ?>"><?php the_field( 'call_to_action', 219 ); ?></a></p>Code language: HTML, XML (xml)

Make sure to replace 219 with the ID of the current ad and secure the code using function_exists if you might ever disable the ACF plugin, even just for testing.

Save and publish the ad. When placing the ad in the content using the TwentyNineteen theme without additional CSS, it looks like this.

Ad content created from custom fields and a simple template.

Use the “Duplicate ad” feature in Advanced Ads Pro to copy the ad template, fill in the Ad Options for the new ad, and publish it using the pre-defined template.

Make it better

Increase your ad management skills without spending more time.
Join over 150,000 publishers and AdOpts increasing their ad revenue.