weeblrAMP actions and filters sample
weeblrAMP offers many "hooks", actions and filters, that you can use to alter its operation. This lets you customize many parts of either internal data used, or generated output. These hooks (there are many!) are listed and documentated on this page.
To get you started, here are a few examples our users have found useful.
You should not put these filters in your theme functions.php, but instead in an AMP-specific functions.php file. Please see this page to read about adding your filters and actions to be run when an AMP page is rendered.
Change the document type based on the request
This filter changes the AMP document type (set in the page structured data) to Recipe for posts that have received a specific tag:
/**
* 1. Change the document type based on the request.
* This filter is triggered BEFORE reading user-defined data from the content
* (ie: wbamp-meta shortcodes)
*/
add_filter(
'weeblramp_json_manifest',
function ( $jsonld, $pageData ) {
// set document type based on post
$post = WeeblrampHelper_Content::getPostFromPageData( $pageData );
if ( ! empty( $post ) && 'single' == wbArrayGet( $pageData, 'request_type' ) ) {
$isRecipe = has_tag( 'Recipe' );
$postCategories = wp_get_post_categories( $post->ID, array( 'fields' => 'ids' ) );
// replace with your own categories ids here
$recipeCategories = array( 12, 34, 56 );
// set document type to Recipe IF:
// post had a Recipe tag
// OR
// it belongs to one of the listed categories
if ( $isRecipe || array_intersect( $postCategories, $recipeCategories ) ) {
$jsonld['@type'] = 'Recipe';
}
}
return $jsonld;
},
10,
2
);
Filter json-ld data to add support for recipes
This filter will let you add custom structured data on AMP pages for items that are recognized as Recipe (using the filter above, most likely):
/**
* 2. Filter json-ld data to add support for recipes.
* This filter is triggered AFTER reading user-defined data from the content
* (ie: wbamp-meta shortcodes)
*/
add_filter(
'weeblramp_get_jsonld_data',
function ( $jsonld, $pageData ) {
$docType = wbArrayGet( $jsonld, '@type' );
if ( 'Recipe' == $docType ) {
// this is a Recipe, let's add cooking time and such
$jsonld['cookTime'] = 'PT1H';
$jsonld['prepTime'] = 'PT30M';
$jsonld['recipeIngredient'] = array(
'Cheese',
'Sugar',
'Lemon'
);
}
return $jsonld;
},
10,
2
);
Filter automatically inserted ads, based on category or tag
This filter lets you decide to auto-insert ads in your content only if the content belonds to some categories you choose:
/**
* 3. Filter automatically inserted ads, based on category or tag
*/
add_filter(
'weeblramp_autoinsert_ads_on_post',
function ( $shouldInsert, $post ) {
if ( ! empty( $post ) ) {
$postCategories = wp_get_post_categories( $post->ID, array( 'fields' => 'ids' ) );
$isRecipe = has_tag( 'recipe' );
$recipeCategories = array( 12, 34, 56 );
// filter based on category
if ( array_intersect( $postCategories, $recipeCategories ) ) {
$shouldInsert = false;
}
// filter based on tag
if ( $isRecipe ) {
$shouldInsert = false;
}
}
return $shouldInsert;
},
10,
2
);