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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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