views:

130

answers:

1

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. Any idea of 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, 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 Page 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??

+2  A: 

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 … :)

toscho
the client has such requirements...can't help it!
detj
Where I output the HTML directly you could check for is_single() or is_page() and echo mainPage();. See http://codex.wordpress.org/Conditional_Tags
toscho
How will chekcking for is_single() or is_page() help me to add the <meta> tag. Please explain!
detj
I extended the sample code. Does that help?
toscho
wow! thanx man! I've implemented the above code, and it works exactly like I wanted.
detj