Skip to content
Christopher Davis edited this page Jun 4, 2012 · 5 revisions

To make SEO Auto linker more extensible there are plenty of filters sprinkled throughout the plugin. This page gives a brief overview of each.

Changing Link Targets

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.

Modifying Whether a Post Should Contain Links

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.

Modifying Links for a Given Post

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.

seoal_pre_get_meta

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);

seoal_link_keywords

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;
});

seoal_link_url

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;
});

seoal_link_max

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;
});

Other Filters

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.

Clone this wiki locally