Add code right after theme header file loads

Posted on 2014-07-29
Last Modified: 2014-08-06
There isn't a wordpress hook to hook directly after the actual header area where the logo and menu is. I'm making a plugin to auto add a tap to call button when in mobile view. Using the_content filter won't work because it won't display before the page title.

Can anyone think of a way to have the plugin know when the header.php file has finished loading, then insert the code I want before the page.php, single.php, etc loads? Or any other suggestions on a good way to get the code in there at the right location without having to have the user edit their theme?

Every theme can be so different, so I just can't think of a common way to do this so it's compatible with if not all themes, most.
Question by:Dzynit
  • 3
  • 3
LVL 53

Expert Comment

ID: 40228047
I'm not sure I understand what you are trying to do.  Loading order is not necessarily the same a listing order. If you need code at a particular place in the page you need to handle it on the server to put the code in the correct place in the listing order, or add a script that generates the container and inserts it as a child element of the body using the DOM methods.

If it is for a plugin, the I assume the plugin will have a script file and you will need to create the container insert it in to the body only AFTER the document is loaded or you will get errors for referencing undefined elements and the code will not execute to conclusion.

LVL 70

Accepted Solution

Jason C. Levine earned 500 total points
ID: 40228423
You might have to write your own Hook.

The Genesis themes do have this capability:

This hook executes immediately before the header (outside the #header div).

By default, this hook outputs the header code, including the title, description, and widget area (if necessary).

This hook executes immediately after the header (outside the #header div).
LVL 14

Author Comment

ID: 40229179
Here's an example to help clarify better: if you make your browser very skinny, you'll see the mobile view take over and see the book a trip button display. I manually added the code for that button right in the theme.

But I have a customer that wants me to make a plugin to automatically do that for two reasons. One, so he doesn't have to keep asking me every time to add the code so he doesn't goof and mess up the theme, Two, so there's no worries of the code getting overwritten when there's a theme upgrade. We don't ALWAYS want to make a child theme for one small edit.

Jason, I know some themes have their own hooks for in the header area, but each are different and then there's tons of themes that do not have one at all. I plan to include a shortcode for users to manually place the button any where they want, but I also wanted to be able to put it between the header and the title of the content area automatically without having to add any code to the theme manually.

I was just hoping maybe someone on here might have a crazy idea to try that wouldn't be flagged as a security vulnerability by the wordpress team.
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

LVL 14

Author Closing Comment

ID: 40244477
I was hoping for a crazy idea on this, but want to close the question. Thanks Jason, I'd planned to include a shortcode for the plugin also.
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40244583
Yeah, sorry.  The correct answer is still to make your own hooks which means code at some level.

Not sure how a shortcode will help here.  By the time a shortcode executes from the page/post editor, the header is rendered?
LVL 14

Author Comment

ID: 40244610
Yeah, I'll provide the do_shortcode php line and the user will have to insert that line in their header.php (or other area) where they want the button to display. That way if they want it in a specific page they can just use the regular shortcode too.
That will make it flexible, but still might create some extra support tickets due to people inserting in the wrong areas and maybe breaking their theme.
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40245035
Ah-ha.  Shortcode-as-function, got it.

Sorry, wish there was a more intelligent/standardized way to do this.

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: : Go t…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question