Adding footers and meta tags to WordPress posts

Adding post footer

There are a lot of plugins for adding footers to WordPress posts and one of them is so called “Add Post URL” plugin written by some Chinese guys. From my perspective, its benefits includes macros support and ability to decide whether or not to display the footer for each post individually. It worked fine for me and I even improved it a little bit. The following code snippet shows how I added “post_id” macro:

$footer_text = $posturl_options['footer_text'];
$footer_text = trim($footer_text);
if (!empty($footer_text))
{
    //remove_filter( 'the_content', 'wpautop' );
    
    $post_id = get_the_ID();
    
    $footer_text = str_replace("%post_id%", $post_id, $footer_text);
    $footer_text = str_replace("%site_url%", site_url('/'), $footer_text);
    $footer_text = str_replace("%site_name%", get_bloginfo('sitename'), $footer_text);
    $footer_text = str_replace("%post_url%", get_permalink(), $footer_text);
    $footer_text = str_replace("%post_title%", the_title('', '', false), $footer_text);
    $footer_text = stripslashes($footer_text);
    $text .= $footer_text;
}

A small Problem with line breaks

The only small problem that I met while using this plugin was that some redundant line breaks (<br />) were added to the footer text. Initially I played a bit with filters and realized that removing “wpautop” filter as shown above solves the problem but affects appearance of some posts. I left the question open, and after a few month a lucky idea surprisingly came to me – the problem could be caused by wrong plugin priority and finally I solved this problem by changing the priority of the plugin filter according to article How to set WordPress plugin filter priority:

### Function: Post URL: Add Post URL for Post
add_action('the_content', 'wp_posturl', 20);
function wp_posturl($text) {
    global $post;
    $posturl_options = get_option('posturl_options');

I chosen 20 because, in my case, no further processing of footer text is needed.

Adding HTML meta tags to the header

Add Post URL” plugin does not allow to add a meta tags, but there are a pretty straightforward way to do it with a few lines of code that register a function to be executed when the wp_head action fires:

add_action('wp_head', 'posturl_add_meta');
function posturl_add_meta()
{
    ?>
    <meta name="testname1" content="test-content">
    <?php
}

Adding theme footer

To add footers not only to posts but to all the pages of the site, wp_footer action could be used:

add_action('wp_footer', 'posturl_add_footer', 20);
function posturl_add_footer()
{
    ?>
<!-- Google Analysis counter code, for example -->
    <?php
}

This code requires wp_footer action to be supported be the theme.

Leave a Reply

Your email address will not be published. Required fields are marked *