Need help to add HTML codes by plugin either in header.php or footer.php. For example :


Here if i want to add HTMLcode before by plugin how can i do ??

Final codes


Now think i will create a new plugin and add when the plugin is active then codes will be added but when its off codes will also off


There are hooks that you can use for this purpose.

The wp_head hook allows you to add your content to the header, while wp_footer does the same for your footer.

Use it as follows:

add_action( 'wp_head', 'my_function');
function my_function (){
    echo "Hello World!";

This will print Hello World! in your header.

However, there is an important note. The theme must have wp_head(); and wp_footer(); in its template files. Almost every theme has these files, so you can safely use this in most cases.

To print anything in a template from a plugin, you have to actually hook to some action or filter. If there is none of these in that template file, then you simply can't echo your content there, and must find somewhere else to do so.

|improve this answer|||||
  • Also note that using a Child Theme is recommended - you don't want to change theme files and have your changes overwritten with a theme update. Lots of googles on creating/using Child Themes. Once you get yours set up, just copy the template from the main theme into the Child Theme folder and make your modifications there. – Rick Hellewell Jul 28 '17 at 4:09
  • The question is actually about the plugins. The OP is trying to dynamically add content to header or footer from a plugin, which doesn't require changing any files. @RickHellewell – Jack Johansson Jul 28 '17 at 4:10
  • OK. A plugin will work, just add the code above in a plugin structure. Plugins run on every page, whereas if added to a template, the code would only run on pages that use that template. So, for anyone finding this question in the future, if they decide to use a template, then knowing about Child Theme is useful. As is knowing that you can put any 'add_action' in a plugin. – Rick Hellewell Jul 28 '17 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.