I need to add some html into the header.php but instead of directly changing the header.php of my theme or creating a child theme, how can I do it with just actions and/or hooks?
Leave a Reply
You must be logged in to post a comment.
I need to add some html into the header.php but instead of directly changing the header.php of my theme or creating a child theme, how can I do it with just actions and/or hooks?
You must be logged in to post a comment.
If you are looking to insert code into the
<head></head>
block, then this hook does it:If that’s not the case, then it’s more difficult. I think it would depend on the theme providing a hook at your desired point of insertion.
Another option is to make DOM manipulations with jQuery.
Other quite crazy possibilities can be extracted from this Q&A: Adding onload to body
You have three options:
head
tags, use thewp_head
action.Adding a header action
say in header.php I have a large blue square containing a message, and I want to eb able to insert stuff afterwards via a hook, I can do:
I can now put in
functions.php
something like this:And hello world will be displayed after the big square message
Of course you will need to add the
do_action
calls in the appropriate placesAdding wp_head
This will insert code into the
<head>
tags, and works the same as the above example.Inserting With Javascript
This might work, but will assume you know the ID and classes
How Do I insert header content That Will Work With Any Theme?
You can’t.
There is no standard hook that hooks into the header area. It wouldn’t make sense on a lot of themes as header layouts vary wildly. Some themes don’t have headers.
It all depends upon the theme, what you want to change, and if WordPress and/or the theme provide filters and hooks to accomplish it. A child theme would most likely be your best (and safe) bet.
A simple child theme is very easy to setup and code. Take a look at he CODEX docs here.