Add code right after theme header file loads

Posted on 2014-07-29
Medium Priority
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 2000 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: http://www.fastactionfishing.com/ 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.
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
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: mywebsite.com/wp-login.php : Go t…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

762 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