Project

General

Profile

Common Metadata Customization Recipes

These code snippets customize the generated metadata.

Read more about:

Edit the 'og:site_name' meta tag

You want to replace the autogenerated og:site_name Opengraph metatag with a custom one.

This can easily be done by hooking a custom function to the amt_opengraph_metadata_head filter:

function customize_og_sitename_metatag( $metatags ) {
    // ... replace 'og:site_name' here
    return $metatags;
}
add_filter( 'amt_opengraph_metadata_head', 'customize_og_sitename_metatag', 10, 1 );

Programmatically add the 'fb:admins' and 'fb:app_id' meta tags

Add the fb:admins and the fb:app_id OpenGraph meta tags.

This can easily be done by hooking a custom function to the amt_opengraph_metadata_head filter:

function amt_extend_og_metatags( $metatags ) {
    $metatags[] = '<meta property="fb:admins" content="ENTER_USER_ID_HERE" />';
    $metatags[] = '<meta property="fb:app_id" content="ENTER_APPID_HERE" />';
    return $metatags;
}
add_filter( 'amt_opengraph_metadata_head', 'amt_extend_og_metatags', 10, 1 );

Further customization of the custom title

Programmatically customize the custom title as it has been entered in the post editing panel.

This can easily be done by hooking custom functions to the amt_custom_title filter. Please not that this filter is processed only if a custom title has been added to the post in the Metadata box.

function amt_custom_title_modified( $title ) {
    return $title . ' | ';
}
add_filter( 'amt_custom_title', 'amt_custom_title_modified', 10, 1 );

Metadata exclusion

There are two main ways to prevent the generation of metadata for specific parts of the web site. The following example demonstrates how to exclude metadata from a page with slug contact.

First method is by using the filter based switches for metadata exclusion:

function amt_check_exclude_metadata( $default ) {
    // Must return true/false
    if ( is_page('contact') ) {
        return true;
    }
    return $default;
}
add_filter( 'amt_exclude_basic_metadata', 'amt_check_exclude_metadata' );
add_filter( 'amt_exclude_dublin_core_metadata', 'amt_check_exclude_metadata' );
add_filter( 'amt_exclude_opengraph_metadata', 'amt_check_exclude_metadata' );
add_filter( 'amt_exclude_schemaorg_metadata', 'amt_check_exclude_metadata' );
add_filter( 'amt_exclude_twitter_cards_metadata', 'amt_check_exclude_metadata' );

The second method involves the filtering of the array that contains the generated metadata for the head section and the body of the HTML page. To exclude the metadata all you have to do is return an empty array.

function amt_check_exclude_metadata_alternative( $metatags ) {
    // Must return array
    if ( is_page('contact') ) {
        return array();
    }
    return $metatags;
}
add_filter( 'amt_metadata_head', 'amt_check_exclude_metadata_alternative' );
add_filter( 'amt_metadata_footer', 'amt_check_exclude_metadata_alternative' );

Extend the Organization properties

It would be impossible for Add-Meta-Tags to provide a web based interface for
users to fill in even the most common Organization properties. Alternatively, it
provides the amt_schemaorg_publisher_extra filter hook, which can be used in order
to extend the default Organization properties (for the Person object use amt_schemaorg_author_extra).

In the following example, a filtering function (amt_schemaorg_publisher_extra_tags) is
attached to the amt_schemaorg_publisher_extra hook and adds:

  • Extra social profile URLs (Youtube, LinkedIn).
  • Adds a postal address object to the Organization object.
  • Adds a 'sales' and a 'technical support' contact points to the Organization object.

IMPORTANT NOTICE: The following code only adds the metadata for your Organization.
Make sure this information is also visible to your visitors in the current page.

function amt_schemaorg_publisher_extra_tags( $metatags ) {
    // Social profiles for LinkedIn, Youtube
    $metatags[] = '<meta itemprop="sameAs" content="https://www.youtube.com/channel/abcdef" />';
    $metatags[] = '<meta itemprop="sameAs" content="https://www.linkedin.com/in/abcdef" />';

    // Organization Postal Address
    $metatags[] = '<!-- Scope BEGIN: Organization Postal Address -->';
    $metatags[] = '<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';
    $metatags[] = '<meta itemprop="streetAddress" content="WordPress Str. 123" />';
    $metatags[] = '<meta itemprop="postalCode" content="12345" />';
    $metatags[] = '<meta itemprop="addressLocality" content="City, Country" />';
    $metatags[] = '</span> <!-- Scope END: Organization Postal Address -->';

    // Sales
    $metatags[] = '<!-- Scope BEGIN: ContactPoint - Sales -->';
    $metatags[] = '<span itemprop="contactPoint" itemscope itemtype="http://schema.org/ContactPoint">';
    $metatags[] = '<meta itemprop="contactType" content="sales" />';
    $metatags[] = '<meta itemprop="telephone" content="+1-800-555-1212" />';
    $metatags[] = '<meta itemprop="faxNumber" content="+1-800-555-1213" />';
    $metatags[] = '<meta itemprop="email" content="sales(at)example.org" />';
    $metatags[] = '<!-- Scope BEGIN: OpeningHoursSpecification -->';
    $metatags[] = '<span itemprop="hoursAvailable" itemscope itemtype="http://schema.org/OpeningHoursSpecification">';
    $metatags[] = '<meta itemprop="opens" content="09:00" />';
    $metatags[] = '<meta itemprop="closes" content="21:00" />';
    $metatags[] = '<meta itemprop="dayOfWeek" content="Monday,Tuesday,Wednesday,Thursday,Friday" />';
    $metatags[] = '</span> <!-- Scope END: OpeningHoursSpecification -->';
    $metatags[] = '</span> <!-- Scope END: ContactPoint - Sales -->';

    // Technical Support
    $metatags[] = '<!-- Scope BEGIN: ContactPoint - Technical Support -->';
    $metatags[] = '<span itemprop="contactPoint" itemscope itemtype="http://schema.org/ContactPoint">';
    $metatags[] = '<meta itemprop="contactType" content="technical support" />';
    $metatags[] = '<meta itemprop="telephone" content="+1-800-555-1214" />';
    $metatags[] = '<meta itemprop="faxNumber" content="+1-800-555-1215" />';
    $metatags[] = '<meta itemprop="email" content="support(at)example.org" />';
    $metatags[] = '</span> <!-- Scope END: ContactPoint - Sales -->';

    return $metatags;
}
add_filter( 'amt_schemaorg_publisher_extra', 'amt_schemaorg_publisher_extra_tags' );

Add extra social profile URLs for authors

The following snippet adds extra contact methods in the WordPress user profile for Youtube and LinkedIn profiles, which are then used in the metadata.

Keep in mind that all these URLs are used in the metadata that is generated by Add-Meta-Tags. It is still the responsibility of the frontend developer/designer to make this information visible to visitors.

// Extra contact methods for the WordPress user profile
function amt_add_custom_extra_contactmethods( $contactmethods ) {
    // Youtube
    if ( ! isset( $contactmethods['amt_youtube_author_profile_url'] ) ) {
        $contactmethods['amt_youtube_author_profile_url'] = __('Youtube author profile URL', 'add-meta-tags') . ' (AMT)';
    }
    // LinkedIn
    if ( ! isset( $contactmethods['amt_linkedin_author_profile_url'] ) ) {
        $contactmethods['amt_linkedin_author_profile_url'] = __('LinkedIn author profile URL', 'add-meta-tags') . ' (AMT)';
    }
    return $contactmethods;
}
add_filter( 'user_contactmethods', 'amt_add_custom_extra_contactmethods' );

// Schema.org JSON+LD
function amt_jsonld_schemaorg_extra_author_social_profiles( $metadata_arr, $author_id ) {
    // Youtube
    $youtube_author_url = get_the_author_meta('amt_youtube_author_profile_url', $author_id);
    if ( ! empty($youtube_author_url) ) {
        $metadata_arr['sameAs'][] = esc_url( $youtube_author_url, array('http', 'https') );
    }
    // LinkedIn
    $linkedin_author_url = get_the_author_meta('amt_linkedin_author_profile_url', $author_id);
    if ( ! empty($linkedin_author_url) ) {
        $metadata_arr['sameAs'][] = esc_url( $linkedin_author_url, array('http', 'https') );
    }
    return $metadata_arr;
}
add_filter( 'amt_jsonld_schemaorg_author_extra', 'amt_jsonld_schemaorg_extra_author_social_profiles', 10, 2 );

// Schema.org Microdata
function amt_schemaorg_extra_author_social_profiles( $metadata_arr, $author_id ) {
    // Youtube
    $youtube_author_url = get_the_author_meta('amt_youtube_author_profile_url', $author_id);
    if ( ! empty($youtube_author_url) ) {
        $metadata_arr[] = '<meta itemprop="sameAs" content="' . esc_url( $youtube_author_url, array('http', 'https') ) . '" />';
    }
    // LinkedIn
    $linkedin_author_url = get_the_author_meta('amt_linkedin_author_profile_url', $author_id);
    if ( ! empty($linkedin_author_url) ) {
        $metadata_arr[] = '<meta itemprop="sameAs" content="' . esc_url( $linkedin_author_url, array('http', 'https') ) . '" />';
    }
    return $metadata_arr;
}
add_filter( 'amt_schemaorg_author_extra', 'amt_schemaorg_extra_author_social_profiles', 10, 2 );

Customize archive metadata

Many times it is needed to customize the metadata of archives. The following snippet demonstrates how to customize the description, the image and the 'full meta tags' that appear on WooCommerce's main shop page, which is actually an archive of the custom post type product.


// Shop description
function amt_wc_shop_custom_description( $default ) {
    return 'My custom shop description!';
}
// Dynamic hook name: amt_generic_description_posttype_POSTTYPESLUG_archive
add_filter('amt_generic_description_posttype_product_archive', 'amt_wc_shop_custom_description');

// Shop image
function amt_wc_shop_custom_image_url( $default ) {
    // Return:
    // - URL
    // - URL,WIDTHxHEIGHT notation
    // - ID of an image attachment
    return 'http://example.com/images/shop-logo.png,1200x800';
}
// Dynamic hook name: amt_posttype_image_url_POSTTYPESLUG
add_filter('amt_posttype_image_url_product', 'amt_wc_shop_custom_image_url');

// Shop full meta tags
function amt_wc_shop_custom_full_meta_tags( $default ) {
    if ( is_post_type_archive('product') ) {
        return '
<meta name="robots" content="noimageindex,noarchive" />
<link rel="alternate" hreflang="de" href="http://example.com/de/shop/" />
<link rel="alternate" hreflang="es" href="http://example.com/es/shop/" />
<meta property="myproperty1" content="value1" />
<meta property="myproperty2" content="value2" />';
    }
    return $default;
}
add_filter('amt_full_metatags_site', 'amt_wc_shop_custom_full_meta_tags');