detj
detj

Reputation: 5309

How do I add a meta tag to the page template from a WordPress plugin?

I want to add a meta tag like this one:

<meta name="key" content="value" />

to some of the pages in WordPress. I know, I can add this into my template and it will show up. But the thing is, I am not allowed to even touch the template. It's totally template independent.

So, I have to add the meta tag only by doing something in my plugin code. I have tried wp_head action hook, but it is not working. Is there a workaround or anything to get the meta tag inside the head tags of the pages dynamically?

What I'm doing

What I'm doing is a little different. There are two pages in my blog, the main content page and Summary page. Both of these pages get data through shortcodes. So, the main content page has a shortcode

[mainpage]

And the Summary has this shortcode in it:

[summarypage]

The shortcode was added to main plugin file

add_shortcode( 'mainpage', 'mainPage' );
add_shortcode( 'summarypage', 'summaryPage' );

Now, in my plugin directory, I have two PHP files named mainpage.php and summarypage.php, and they return HTML content.

In mainpage.php

function mainPage() {
    // Code which generates HTML content
    $mainpage .= 'content';
    return $mainpage;
}

Similarly, in summarypage.php

function summaryPage() {
    // Code which generates HTML content
    $summarypage .= 'content';
    return $summarypage;
}

Since, $mainpage and $summarypage contains all that which go inside the page textarea box. I have no idea how to add some meta information to my main or summary pages. Using wp_head inside the function mainPage() and summaryPage() doesn't work and rightly so. So, how can I get a meta tag inside the head section of the page?

Upvotes: 1

Views: 5866

Answers (2)

BoqBoq
BoqBoq

Reputation: 4704

Does this work on something similar to

function addmeta()
{

  echo "\t<meta name='keywords' content='xxxxxxxxxxxxxxxxx' />\n";

}

function shortcodeTemplate($atts) //SHOWS THE SHORTCODE AND RENDERS THE VIEW MODE FOR THE GIVEN VIEW ID
{
 add_action('wp_head', 'addmeta');
echo showTemplate($atts['id']);
}

The shortcode Template is being called to render to shortcode.... that is in a post.

Upvotes: 0

fuxia
fuxia

Reputation: 63556

We could help you better, if you showed us what you have tried already. Here is a working example:

<?php
/*
Plugin Name: No automagic phone numbers
Description: Adds <meta> elements to the <head> to prevent the Skype toolbar and the iPhone from autolinking.
Version: 0.1
Author: Thomas Scholz
Author URI: http://toscho.de
Created: 01.04.2010
*/

if ( ! function_exists('no_automagic_phone_numbers') )
{
    function no_automagic_phone_numbers()
    {
        /* Prevent the Skype plugin and the iPhone from randomly parsing numbers
         * as phone numbers: */
        ?>
<meta name="SKYPE_TOOLBAR" content="SKYPE_TOOLBAR_PARSER_COMPATIBLE">
<meta name="format-detection" content="telephone=no">
        <?php
        if ( ! is_single() and ! is_page() )
        {
            // execute archive stuff
        }
        else
        {
            // single page stuff
        }
    }
    add_action('wp_head', 'no_automagic_phone_numbers');
}

Now I'm wondering why you are allowed to install plugins but not to change the theme … :)

Upvotes: 3

Related Questions