3

I've got a custom theme I've developed and it's basically a 4 page brochure site for a client and I've managed to do away with a few plugins by building in custom-post-types, gzipping via .htaccess and minifying via gulp etc etc.

I'll be keeping the security plugin on the site, but I'd like to remove Yoast, the only benefit it brings, bearing in mind how optimised the site is, is that it allows me to add the meta tags and snippets for each page for SEO purposes.

Is there a function that I can add add to my functions.php file that allows me to add <meta> tags to different pages via the page id?

When one Googles this subject all you get is plugin articles, or info about general wp meta.

Any help would be awesome.

Paul.

7

The hook you're looking for is specifically wp_head which could look something like this:

function theme_xyz_header_metadata() {

    // Post object if needed
    // global $post;

    // Page conditional if needed
    // if( is_page() ){}

  ?>

    <meta name="abc" content="xyz" />

  <?php

}
add_action( 'wp_head', 'theme_xyz_header_metadata' );

I believe in the long run though, since WordPress is so portable, Yoast SEO is probably the most reliable, flexible bet for SEO than something you would do yourself so I would advise against this personally.

0

DIY SEO:

1: In the post/page editor for a page or post: Add custom field(s) (meta data) with required value(s) for your SEO meta tag(s).

  • e.g. Open say your "Terms &Conditions" page in the page editor and add a custom field "my_noindex" with a value of "y".
  • N.B. if the custom field box is not visible below the editor, click the "display options" drop-down at the top of the page and then the "custom fields" check box that appears.

2: In functions.php (or better still in your own theme independent site_functions plugin): Add your SEO function (to be "called" by wp_head) which take these values for the current page and inserts them in the HTML <head>.

// echo noindex tag if post or page has a "my_noindex" custom field with a value of "y"|"Y"|"yes" ...
function my_meta_tags() {
   $noindex = (get_post_meta( get_queried_object_id(), 'my_noindex', true ));
   if (strtolower(substr($noindex,0,1)) == 'y') { 
     ?><meta name="robots" content="noindex" />
     <?php return; // noindex so no point in doing any other SEO stuff
   }

   // other SEO stuff

}
add_action( 'wp_head', 'my_meta_tags',2);

Jeff Starr's article on rolling your own SEO code (including title and description) will help. It requires a custom/child theme as its code goes in header.php, however much of the code could be modified for the my_meta_tags function above. It may not apply SEO the way you want but that's the beauty of DIY: if you want to use your carefully crafted description in custom field (if present), else your custom excerpt, else first n chars of description; then you can write your code accordingly. I've also been intending to write an article on this subject - if I get round to it I'll add a link.

Omissions from article's code:

  • Prevent duplicate title tags (as some themes insert their own). Modern, properly designed themes should enable you to remove the themes title tag when you add the following to your site functions.php.

    function my_remove_stuff() {
      remove_theme_support( 'title-tag' ); 
    }
    add_action('after_setup_theme', 'my_remove_stuff', 15);
    
  • Canonical Tags. Add the following to the first function above:

    //if post or page has a "my_canon" custom field
    $my_canon = get_post_meta( get_queried_object_id(), 'my_canon', true );
    if ( ! empty($my_canon)) :
       echo '<link rel="canonical" href="' . $my_canon . '" />';
       remove_action('wp_head', 'rel_canonical'); // prevnts Wordpress inserting a canon tag - we don't want two
    endif;
    

An SEO plugin is the right option for many users. However if you are happy writing your own code there are many benefits: avoid bloat, avoid lock-in/dependence on plugin, avoid (in some cases) paying for support, SEO works the way you want it; no SEO conflict issues with SEO on your own dynamic custom pages etc.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.