-
Notifications
You must be signed in to change notification settings - Fork 15
Filters
To make SEO Auto linker more extensible there are plenty of filters sprinkled throughout the plugin. This page gives a brief overview of each.
The seoal_targets
filter changes the dropdown of available target attributes
on each link. The values returned by this filter also serve as a whitelist for
which targets atts can be used.
<?php
/*
* Fetch the available target attributes
*
* @since 0.7.2
* @return array The list of target atts
*/
protected static function get_targets()
{
$targets = array(
'_blank' => __('_blank', 'seoal'),
'_top' => __('_top', 'seoal'),
'_self' => __('_self', 'seoal')
);
return apply_filters('seoal_targets', $targets);
}
Example:
<?php
add_filter('seoal_targets', function($targets) {
$targets['_mytarget'] = __('A very Special Target')
return $targets;
});
Functions or methods hooked into seoal_targets
should return an associative
array with the target attribute as the key and a label as the value.
Before any links are added to a post, SEO Auto Link checks to see whether or not links are allowed to be added to a give post.
Before that result gets returned, it runs through the seoal_allowed
filter.
protected static function allowed($post)
{
$rv = true;
if(!is_singular() || !in_the_loop()) $rv = false;
self::setup_links($post);
if(!self::$links) $rv = false;
if(in_array(self::$permalink, self::$opts['blacklist'])) $rv = false;
return apply_filters('seoal_allowed', $rv, $post);
}
Example:
add_filter('seoal_allowed', function($bool, $post) {
// don't allow links on post with ID 12
if(12 == $post->ID)
$bool = false;
return false;
}, 10, 2);
Functions hooked into seoal_allowed
should return a boolean.
The seoal_links
filter allows you to hook in and add keywords programmatically
to a given posts. Some use cases might be network-wide links on a multisite
setup or adding links to all your static pages programmatically.
"Links" in SEO Auto Link are just post with a non-public post type. And all the
meta data about links (url, keywords, etc.) are postmeta values. You can hook
in and add other posts with seoal_links
.
<?php
add_filter('seoal_links', function($links) {
$links[] = array(
'not_post' => true,
'keywords' => array('example', 'a keyword'),
'url' => 'http://www.example.com',
'times' => 1
);
return $links;
});
Of course, if you add links dynamically like the above, the postmeta lookups for things like the url and keywords are going to fail. You'll need to hook into one of the following filters to modify things.
The pre_seoal_links
filter fires before SEO Auto Linker makes a query for the
links on a given post. You can use this to completely replace the links (with
the exception of the per-link blacklist).
An example use case: extend SEO auto linker to associate links by category.
A simple example:
<?php
SEOAL_Dynamic::init();
class SEOAL_Dynamic
{
// taxonomy you want to use
const TAX = 'category';
public static function init()
{
if(!class_exists('SEO_Auto_Linker_Base'))
return;
add_action(
'init',
array(__CLASS__, 'register'),
20
);
add_filter(
'seoal_links',
array(__CLASS__, 'add_links'),
10,
2
);
}
public static function register()
{
register_taxonomy_for_object_type(
self::TAX,
SEO_Auto_Linker_Base::POST_TYPE
);
}
public static function add_links($links, $post)
{
$terms = get_the_terms($post->ID, self::TAX);
if(is_wp_error($terms) || !$terms)
return array(); // no links!
$terms = array_map(function($t) {
return $t->term_id;
}, array_values($terms));
$links = get_posts(array(
'post_type' => SEO_Auto_Linker_Base::POST_TYPE,
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => self::TAX,
'field' => 'id',
'terms' => $terms
)
)
));
return $links;
}
} // end SEOAL_Dyanmic
This one allows you to hook in before Auto link looks up post meta.
<?php
add_filter('seoal_pre_get_meta', function($retval, $meta_key, $link) {
if(isset($link[$meta_key])) $retval = $link[$meta_key];
return $retval;
}, 10, 3);
Used to hook in and modify only the keywords for a given link. You might use this to add a keyword to all your links.
<?php
add_filter('seoal_link_keywords, function($kw) {
$kw[] = 'something';
return $kw;
});
Used to modify the URL for a given link. You might use this to dynamically add something like an affiliate tag to amazon links
<?php
add_filter('seoal_link_url', function($url) {
if(preg_match('#$http://(www\.)?amazon\.com', $url))
{
$url = add_query_arg('tag', 'youraff-id', $url);
}
return $url;
});
Used to modify the max number of times a link may be added. You might use this to allow more links on a older posts or something.
<?php
add_filter('seoal_link_max', function($max) {
// if the post is older than 30 days, allow 10 links
if((time() - get_post_time('U')) > 60*60*24*30)
{
$max = 10;
}
return $max;
});
seoal_should_continue
fires at the start of the loop of keywords. In other
words, you could hook in here and encourse a per post link max.
seoal_pre_replace
fires right before things like headers, images and
shortcodes are added back into the post content.
seoal_post_replace
fires right before the content gets returned and after all
the links, headers, shortcodes and other stuff are added back into the content.