WordPress Function: Check for H1 Custom Field

We work with a lot of different themes from WooThemes to Elegant Themes to my own WordPress Themes. One thing we always want to optimize when we build our sites is the H1 tag. Unfortunately a lot of these themes don’t give a specific option to edit H1’s on posts and pages. So we end up putting the H1 as the WordPress title and move along. This is a little sloppy and makes the page lists annoyingly hard to find specific pages (especially for our clients).

Which one of the following pages is the homepage and which is the about page?


So the solution I have come up with is to load up Advanced Custom Fields and create a simple text field with the Field Name ‘h1’ and use that.


Thankfully most of the templates use ‘the_title’ when they want to display the H1 tag, so we can tap into that and check for this custom field before we display it.

function check_for_h1_title($title, $id)
  if (in_the_loop() AND $id == get_the_ID())
    $h1 = get_post_meta($id, 'h1', true);
    if($h1) return $h1;
  return $title;
add_filter('the_title', 'check_for_h1_title', 10, 2);

Throw this handly little function into your ‘functions.php’ of your child theme and you’re good to go. Team this up with the great SEO for WordPress by Yoast plugin and you’ll be rocking some great SEO.

It will allow you to set the title of the page in WordPress as just ‘About’ let’s say. Then in the WordPress for SEO you can set your Meta Title and in the custom field you set your H1. Your WordPress admin pages will say ‘About’ instead of ‘Best Real Estate Agent in Oklahoma City’ which should be easier for you and your clients to find.