Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Add code right after theme header file loads

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
Dzynit
Asked:
Dzynit
  • 3
  • 3
1 Solution
 
COBOLdinosaurCommented:
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
 
Jason C. LevineNo oneCommented:
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
 
DzynitAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DzynitAuthor Commented:
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
 
Jason C. LevineNo oneCommented:
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
 
DzynitAuthor Commented:
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
 
Jason C. LevineNo oneCommented:
Ah-ha.  Shortcode-as-function, got it.

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now