Project

General

Profile

Metadata for custom content

Since v2.9.7 Add-Meta-Tags has internal support for the generation of metadata for custom content. This page outlines the mechanism that should be used in order to add support for custom content. The very same mechanism is used to implement support for BuddyPress and bbPress.

Read more about:

Adding support for custom content to Add-Meta-Tags

Adding support for the generation of metadata for custom content consists of two parts:

  • A detection mechanism for the custom content.
  • Metadata generator functions for the various types of the supported metadata (basic, Opengraph, Twitter Cards, Dublin Core, Schema.org microdata, Schema.org JSON+LD).

The following code adds support for the FooBar content. amt_detect_foobar() is the detection function. amt_foobar_METADATA_TYPE is the metadata generator.

// FooBar detection
function amt_detect_foobar( $default, $post, $options ) {
    // First and important check.
    // $default is a boolean variable which indicates if custom content has been
    // detected by any previous filter.
    // Check if custom content has already been detected by another filter.
    // If such content has been detected, just return $default (should be true)
    // and *do not* add any metadata filters.
    // This check is mandatory in order the detection mechanism to work correctly.
    if ( $default ) {
        return $default;
    }
    // Test whether a FooBar page has been accessed.
    if ( ! function_exists('is_foobar') || ! is_foobar() ) {
        return false;
    }
    // Insert metadata for FooBar pages
    // Basic (description/keywords)
    add_filter( 'amt_custom_metadata_basic', 'amt_foobar_basic', 10, 5 );
    // Opengraph
    add_filter( 'amt_custom_metadata_opengraph', 'amt_foobar_opengraph', 10, 5 );
    // Twitter Cards
    add_filter( 'amt_custom_metadata_twitter_cards', 'amt_foobar_twitter_cards', 10, 5 );
    // Dublin Core
    add_filter( 'amt_custom_metadata_dublin_core', 'amt_foobar_dublin_core', 10, 5 );
    // Schema.org
    if ( $options["schemaorg_force_jsonld"] == "0" ) {
        // Microdata
        // Non content pages via 'wp_footer' action
        add_filter( 'amt_custom_metadata_schemaorg_footer', 'amt_foobar_schemaorg_footer', 10, 5 );
        // Content pages via 'the_content' filter
        add_filter( 'amt_custom_metadata_schemaorg_content_filter', 'amt_foobar_schemaorg_content_filter', 10, 5 );
    } else {
        add_filter( 'amt_custom_metadata_jsonld_schemaorg', 'amt_foobar_jsonld_schemaorg', 10, 5 );
    }
    // Finally return true. FooBar detected.
    return true;
}
add_filter( 'amt_is_custom', 'amt_detect_foobar', 10, 3 );

function amt_foobar_basic( $metadata_arr, $post, $options, $attachments, $embedded_media ) {
    $metadata_arr = array();
    // Add full meta tags as array items
    return $metadata_arr;
}

function amt_foobar_opengraph( $metadata_arr, $post, $options, $attachments, $embedded_media ) {
    $metadata_arr = array();
    // Add full meta tags as array items
    return $metadata_arr;
}

function amt_foobar_twitter_cards( $metadata_arr, $post, $options, $attachments, $embedded_media ) {
    $metadata_arr = array();
    // Add full meta tags as array items
    return $metadata_arr;
}

function amt_foobar_dublin_core( $metadata_arr, $post, $options, $attachments, $embedded_media ) {
    $metadata_arr = array();
    // Add full meta tags as array items
    return $metadata_arr;
}

function amt_foobar_schemaorg_footer( $metadata_arr, $post, $options, $attachments, $embedded_media ) {
    $metadata_arr = array();
    // Add full meta tags as array items
    return $metadata_arr;
}

function amt_foobar_schemaorg_content_filter( $metadata_arr, $post, $options, $attachments, $embedded_media ) {
    $metadata_arr = array();
    // Add full meta tags as array items
    return $metadata_arr;
}

function amt_foobar_jsonld_schemaorg( $metadata_arr, $post, $options, $attachments, $embedded_media ) {
    $metadata_arr = array();
    // Add data
    return $metadata_arr;
}

Metadata for products

A similar mechanism as the above is used in order to add support for product metadata. Please visit the product metadata section for more details.

Metadata for BuddyPress

A mechanism like the one outlined above is used to implement support for BuddyPress metadata. You can find the details in the metadata/amt-extend.php file of the source code.

How to customize the BuddyPress metadata

Add-Meta-Tags tries to generate metadata automatically wherever possible. However, this is not always possible. In such cases or in cases you are not satisfied with the automatic metadata, you can filter it and modify or extend it.

Each metadata generator provides a filter hook which can be used to attach a filtering function and easily insert the metadata you need. Each filtering function should accept the following 5 arguments:

$metadata_arr, $post, $options, $attachments, $embedded_media

For instance, in case you want to extend the Basic metadata generator, you could add the following code in the functions.php file of the theme or in a custom plugin:

function my_extra_buddypress_basic_metadata( $metadata_arr, $post, $options, $attachments, $embedded_media ) {
    // Add an extra meta tag in profile pages
    if ( bp_is_user_profile() ) {
        $metadata_arr[] = '<meta name="some_meta_name" content="some meta value" />';
    }
    return $metadata_arr;
}
add_filter('amt_buddypress_basic_extra', 'my_extra_buddypress_basic_metadata', 10, 5);

The available filter hooks in order to extend the various types of BuddyPress metadata are the following:

  • amt_buddypress_basic_extra -- For the customization/addition of regular meta tags, like description and keywords.
  • amt_buddypress_opengraph_extra - For the customization/addition of Opengraph meta tags.
  • amt_buddypress_twitter_cards_extra - For the customization/addition of Twitter Cards meta tags.
  • amt_buddypress_dublin_core_extra - For the customization/addition of Dublin Core meta tags.
  • amt_buddypress_schemaorg_footer_extra - For the customization/addition of Schema.org microdata for non-content pages, such as archives.
  • amt_buddypress_schemaorg_content_filter_extra - For the customization/addition of Schema.org microdata of content pages.
  • amt_buddypress_jsonld_schemaorg_extra - For the customization/addition of Schema.org JSON+LD data.

How to customize the Extended Profiles field map

Add-Meta-Tags (AMT) tries to generate metadata automatically wherever possible. However, it would be impossible for it to automatically understand what profile property each of your Extended Profile fields represents. In order to bridge the gap between the profile properties AMT understands and generates metadata for and the actual fields of your Extended Profiles a field map has been invented.

The general format of each item of the field map is:

profile_property_amt_understands => array_of_actual_fields_which_are_queried_for_the_relevant_data

The default field map is the following:

    $xprofile_field_map = array(
        'description'       => array('excerpt', 'summary', 'description', 'bio', 'about'),
        'keywords'          => array('keywords', 'skills', 'interests'),    // TODO: Future: add group names?
        'first_name'        => array('first name', 'given name'),
        'last_name'         => array('last name', 'family name', 'surname'),
        'additional_name'   => array('additional name', 'middle name'),
        'nickname'          => array('nickname', 'alias', 'alternate name'),
        'honorific_prefix'  => array('honorific prefix'),
        'honorific_suffix'  => array('honorific suffix'),
        'gender'            => array('gender', 'sex'),
        'nationality'       => array('nationality', 'country'),
        'telephone'         => array('telephone', 'phone', 'tel'),
        'fax'               => array('fax number', 'fax'),
        'email'             => array('email', 'email address'),
        'website'           => array('website', 'web site', 'url', 'homepage', 'blog', 'personal page', 'alternative profile'),
        'job_title'         => array('job', 'job title'),
        'works_for'         => array('company', 'company name', 'employer', 'works for'),
        'works_for_url'     => array('company url', 'employer url'),
        'work_latitude'          => array('work latitude'),
        'work_longitude'         => array('work longitude'),
        'home_latitude'          => array('home latitude'),
        'home_longitude'         => array('home longitude'),
    );

In order to associate the profile properties AMT understands with the fields you have actually deployed in the BuddyPress Extended Profiles you have to attach a filtering function to the amt_buddypress_xprofile_field_map hook and return an array with your associations. For example:

function amt_my_custom_field_map( $default_map ) {
    return array_merge($default_map, array(
        'description'       => array('short description'),
        'keywords'          => array('expertise'),
    ));
}
add_filter('amt_buddypress_xprofile_field_map', 'amt_my_custom_field_map');

The above code instructs AMT to generate a description, og:description, twitter:description, etc, meta tag with the contents of the short description field. In the same manner, the contents of the expertise field of the Extended Profile is used in order to generate the keywords and keywords-like meta tags wherever applicable.

Prevent profile metadata from being added to the profile page

Sometimes, for example in case some profiles have to remain private, it is necessary to prevent Add-Meta-Tags from adding any profile metadata to your pages so as to avoid exposing private information. Although there is no BuddyPress specific way to do this, it can easily be done by attaching a filtering function that returns an empty array to the amt_metadata_head and amt_metadata_footer hooks. For instance:

function amt_check_profile_visibility( $metatags ) {
    if ( bp_is_user_profile() && check_if_profile_is_private_function() ) {
        return array(); // do not add any metadata for this profile
    }
    return $metatags;
}
add_filter('amt_metadata_head', 'amt_check_profile_visibility');
add_filter('amt_metadata_footer', 'amt_check_profile_visibility');

Metadata for bbPress

How to customize