Support » Plugins » Accessing Page Content in wp_head()

  • I have some links and styles to output in the head.

    However, I want to avoid outputing them unless the page content requires them (big, fat Google Maps scripts, etc.).

    How can I search the page content from wp_head(), so I can see whether or not the links/styles/scripts are necessary?

    If I need to RTFM, then please point me to the appropriate FM. I could shore do with some example code.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Head elements are relatively small and get included by your chosen theme header.php (plus plugins) , needed or not. I don’t think you can “search” the page/page at that point but when you get to the main section, you can exclude. It’s all in the codex/documentention at

    It’s not debatable issue, like it or not that’s how WP works. The “loop” and the themes. One can get clever but only if one understands the basic intent.

    That’s pretty much what I thought.

    Here’s why I ask (Actually, this is very much a debatable issue, and should be debated):

    I have a mondo heavy-duty plugin that displays some real bitchin’ Google Maps stuff.

    Kewl, Knarly, etc. <wave hang-ten fingers/>

    The problem is that the GM API is REAL BIG, and introduces SERIOUS lag time to the page. I just found out that the GM API sticks <style> tags in their include, so I can’t reference it from the content.

    This means that EVERYONE, whether or not they will ever use the GM stuff, has to pay for it.

    UPDATE: I’ll probably write some real, bloody, dripping, machete-style hack that will look for a “page_id” or a “p” in the argument list, and will read the content from the DB (bad thing to do, as it bypasses all kinds of nice stuff). I’ll peek in the content to see if my trigger comments are there. If so, I’ll include the files. If not, I’ll just skip them.

    Just an update. I was able to hack this without TOO much nose-holding, thusly:

    In my wp_head handler:

    if ( $_GET['page_id'] )
    		$page = get_page ( $_GET['page_id'] );
    	elseif ( $_GET['p'] )
    		$page = get_post ( $_GET['p'] );
    	if ( $page )
    		$page = $page->post_content;
    	if ( preg_match ( "/<!-- MY_TRIGGER_COMMENT -->/", $page ) )
    		echo '<script src=";v=2&amp;key='.$gkey.'" type="text/javascript"></script>'."\n";

    That does it for now. I still have a lot of testing to go, so we’ll see how it holds up (I need to test the plugin as a post, as opposed to a page. It may need work).

    I have to take some issue with the philosophy that any aspect of a project is “not debatable.” In my world, the User Experience is always king, and we should be doing everything we can to always optimize the UE.

    The “that’s just the way it works” outlook is one that is very attractive to (and typical of) engineers and “purists.” We become enamored with the beauty of the design or philosophy, and blind to the UE.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Accessing Page Content in wp_head()’ is closed to new replies.