WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My plugin needs to know if the currently active theme places the post title in H1 heading tags? I'm looking for suggestions on approaches to create script that could discern this.

UPDATE: The plugin evaluates the SEO profile of the post content to help the user make improvements for search rankings. Second only to the post title, the h1 tag is one of the most important tags for SEO. However, I don't want to ask the user to include their primary keyword in an h1 heading tag in the post content if the theme already does that (as many do).

share|improve this question
Methinks that's too broad a question. A theme can have many files, some of which could have <h1>{$title}</h1> and some of them not. And what is your plugin trying to do? More use-case details would help us help you answer this question. – MikeSchinkel Nov 9 '10 at 7:54
Perhaps you could clarify why it is you feel you need to be able to determine what element a user's theme has surrounding the title call, what does you code aim to do, and why does it need to be aware of the title's parent element? – t31os Nov 9 '10 at 14:56
Hi guys, sorry I left this uncommented! I've updated the question with more detail. – Scott B Nov 12 '10 at 20:51
up vote 6 down vote accepted

Send an HTTP request to the page and search the response body for </h1>. See this Gist for an example.

Store the result in an option and delete this option when the user switches the theme (hook into the action switch_theme).

share|improve this answer
A plugin that making use of this is the CSP WordPress Plugin - people.mozilla.org/~bsterne/content-security-policy/… ; Screenshots/Review: hakre.wordpress.com/2010/06/01/… -- It does an ajax request to figure out similar stuff (image and script sources), but that should work with looking for <h1> tags as well. – hakre Nov 9 '10 at 18:16
Brilliant in its simplicity. – Jan Fabry Nov 9 '10 at 20:52
This is excellent. Thanks! – Scott B Nov 12 '10 at 20:57

You can never be sure, but you can use heuristics to make an educated guess. Are you only talking about single post pages, or also lists? Some widgets also list post titles, I assume you don't care about sidebars?

Also, you do not know whether it is the title that is written in <h1> tags. If you see <h1>{$title}</h1>, is it the post title or the site name, or something completely else?

I would simplify the question to this: does the current main template file (that is included by template-loader.php) contain <h1>[some stuff]title[some stuff]</h1>? This would cover <h1>{$title}</h1>, <h1><?php the_title(); ?></h1> and other common patterns. It would probably get fooled by commented parts, like <!-- <h1>{$title}</h1> -->. It does not cover get_template_part() includes, maybe you can add that yourself (but there is no easy hook for that). Watch out for <h1>Site name</h1> [content] <h2>Post title</h2> [content] <h1>Some other big heading</h1>, you don't want to match that.

The general idea would be to hook (late) into the template_include filter, so you know which template file will be included. Analyze this file, looking for the pattern above. If you find it, set a variable that was null to true. If you can't find it, set it to false. Cache the result, since this will probably be an expensive operation and the template doesn't change that often.

You won't know the result until the template will be included, but I assume you will change the output of the_title() based on the result? In that case, you don't need to know it that early.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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