1

I'm trying to create a shortcode with attributes which then places the values in meta tags for SEO purpose. I know there a some plugins for this use, but I want a simple and reusable/extendable meta tags.

So far I've this piece of PHP code. The is saved in shortcodes.php and then included in functions.php.

add_shortcode('seo_tags', 'seo_tags');
$atts = array();
function seo_tags($atts){
    $atts = shortcode_atts(
        array(
            'title' => 'IftinTablets - Android tablets for everyone',
            'description' => '',
            'keywords' => ''
        ), $atts );

    //print_r($atts);
};

print_r gives me the desired effect

Array ( [title] => Title Value [description] => Description value. [keywords] => keyword1, keyword2 )

But I don't get any entries in the header with wp_head();

I tried this for wp_head(); but no errors or desired effect are visible.

if(! function_exists('seo_tags')){
    function set_seo(){
        return '
            <meta name="title" content="' . $atts['title']. '" />'
            <meta name="description" content="' . $atts['description']. '" />'
            <meta name="keywords" content="' . $atts['keywords']. '" />';
    }
}

add_action('wp_head', 'set_seo');

What am I doing wrong?? Please someone help with this "easy" issue. Thank you.

  • 3
    Use post meta (custom fields), not a shortcode. Shortcodes are embedded in the post content, and that is loaded when the header has been printed already. – fuxia Sep 17 '14 at 15:17
  • What if I still want to use shortcode for this purpose? Would you please help with this? – Omar Abdirahman Sep 17 '14 at 15:22
  • By the time Wordpress reads your shortcode in the content of the post, the meta tags are already created. Custom fields are the way to go for you. – Douglas.Sesar Sep 17 '14 at 15:49
  • I know that and I've used custom fields for SEO purpose before but it a bit to complicated and one has to follow more steps then using shortcodes. Please help me out, I don't mind the timing. – Omar Abdirahman Sep 17 '14 at 16:29
  • Please don't multipost the same question. – birgire Sep 17 '14 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.