Project

General

Profile

Filtering and customizing the generated metadata

All the metadata (meta tags) generated by Add-Meta-Tags can be filtered and customized according to the special needs of each web site. For this reason several filter hooks are provided.

How to use the filter hooks

One or more functions, which process the generated metadata in a custom way, can be attached to the available filter hooks.

In the following example, my_filtering_function is attached to the amt_metadata_head filter hook:

function my_filtering_function( $metatags ) {
    // ... process the provided meta tags included in the @$metatags@ array here
    // Finally return the processed meta tags.
    return $metatags;
}
add_filter( 'amt_metadata_head', 'my_filtering_function' );

How to process the provided meta tags

The meta tags that are generated by the plugin and passed to the filtering functions are in the form of an array with the format:

array(
    ...
    12 => '<meta name="robots" content="index,follow" />',
    ...
    'basic:description' => '<meta name="description" content="This is a description." />',
    ...
);

Some array items have a key with a name after the name of the meta tag they contain. Please note this is not the case for all meta tags yet. There is an ongoing effort to get there at some point in the future.

You can select/isolate the specific meta tags you want to process in the following ways.

Using the key of the array item

Selecting the meta tag you want to process using the key of the relevant array item is easy:

function my_filtering_function( $metatags ) {
    //var_dump($metatags);
    $metatags['basic:description'] = 'Adding my custom description.';
    return $metatags;
}
add_filter( 'amt_metadata_head', 'my_filtering_function' );

Please note that currently there is no table with the available key names. You will have to use var_dump() to check the contents of the array.

Using the strpos() function

Instead of using the array item key to isolate a specific meta tag, you can search the array for a specific meta tag name using the strpos() PHP function.

function my_filtering_function( $metatags ) {
    //var_dump($metatags);
    // New array to hold the modified meta tags
    $metatags_new = array();
    foreach ( $metatags as $metatag ) {
        if ( strpos($metatag, 'og:title') !== false ) {
            // We have an 'og:title' meta tag
            $metatags_new[] = '<meta property="og:title" content="MY CUSTOM OG TITLE" />';
        }
    }
    return $metatags_new;
}
add_filter( 'amt_metadata_head', 'my_filtering_function' );

Using the preg_match() function

Alternatively the preg_match() PHP function can be used to isolate meta tags in more complex scenarios. In the following example we exclude any meta tags that contain either 'copyrightYear' or 'og:video':

function my_filtering_function( $metatags ) {
    //var_dump($metatags);
    // New array to hold the modified meta tags
    $metatags_new = array();
    foreach ( $metatags as $metatag ) {
        // Exclude any meta tags that contain 'copyrightYear' or 'og:video'
        if ( ! preg_match('#(copyrightYear|og:video)#', $metatag) ) {
            $metatags_new[] = $metatag;
        }
    }
    return $metatags_new;
}
add_filter( 'amt_metadata_head', 'my_filtering_function' );