Solved

Add code right after theme header file loads

Posted on 2014-07-29
7
284 Views
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.
0
Comment
Question by:Dzynit
  • 3
  • 3
7 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
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.

Cd&
0
 
LVL 70

Accepted Solution

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

The Genesis themes do have this capability:

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

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

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

Author Comment

by:Dzynit
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.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 14

Author Closing Comment

by:Dzynit
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.
0
 
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?
0
 
LVL 14

Author Comment

by:Dzynit
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.
0
 
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The viewer will learn how to dynamically set the form action using jQuery.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now