Wordpress plugin for inserting code before <head>

8 replies
Hi,

I have some code in my header.php before the <head> starts, which defines some session variables.
I'm trying to clean up my site and would like to have no changes to the theme files, and don't want to use a child theme.

Do you know a wordpress plugin that will let me insert code before the <head>?

I've tried to insert it in the beginning of the pages themselves via a header/footer plugin, but that doesn't seem to work (the sessions are not created). Would that be possible if I change the code?

The code looks like:

<?php
session_start();
//creating sessions
?>

As you can guess I'm not a php guru.

Thanks in advance.

Best regards,
Thomas
#<head> #code #inserting #plugin #wordpress
  • Profile picture of the author dwbiz05
    You need to add some code that runs before the headers and html are called. Try adding this to the following file: http://yoursite.com/wp-content/mu-plugins/index.php

    NOTE: you may need to create the mu-plugins directory and the index.php file.

    PHP Code:
    <?php

    add_action
    ('init''dw_set_session'); //runs the function before headers are called.

    function dw_set_session(){
         
    session_start(); //start php session
    }

    ?>
    Hope that helps!

    D.
    Signature

    Tired of fighting with WordPress to build the site you want? You shouldn't have to spend hours tweaking your website to bend to your will. Take the next step in your journey to WordPress mastery with wordpress4anyone.com

    {{ DiscussionBoard.errors[9550989].message }}
  • Profile picture of the author ThomasTe
    Thanks so much for your idea. However, I was looking to avoid making changes to the php files. Do you know a plugin that could do this?

    I have a plugin (Custom headers and footers) that allows me to insert code at the top or bottom of pages and posts and also inside the <head> area.
    I tried to add my code to these areas but it seems to be ignored. Any pointers?

    Thanks.

    Best regards,
    Thomas
    {{ DiscussionBoard.errors[9552125].message }}
    • Profile picture of the author professorrosado
      I haven't tried this, but dwbiz05 did mention to have it call html. Try combining your header/footer plugin with dwbiz05's suggestion, except call an html file which contains the php command line (make all invisible to the eye).

      This will depend on what exactly you're trying to do with the php line, if it will function outside of it's "frame" and even if it is possible for it to operate within the active page or not as an added (embedded) php command.
      {{ DiscussionBoard.errors[9555136].message }}
    • Profile picture of the author dwbiz05
      Using mu-plugins files basically is creating a plugin. More than likely that file isn't even there, you would have to create it, so you are not making changes to any core functions.

      In fact, other than creating an full plugin, this is the correct way to hook into the wordpress process without touching the core files or using the theme's files.

      MU stands for "Must Use." Basically it's a way to make plugin functions that cannot be turned off in the plugins settings. You can't put a session call in the <head> part of html because data has already been sent to the browser. PHP session_start() must be initiated prior to any data being sent to the browser.
      Signature

      Tired of fighting with WordPress to build the site you want? You shouldn't have to spend hours tweaking your website to bend to your will. Take the next step in your journey to WordPress mastery with wordpress4anyone.com

      {{ DiscussionBoard.errors[9558438].message }}
  • Profile picture of the author SteveJohnson
    https://wordpress.org/plugins/simple-session-support/

    Using sessions with WP can be problematic, and there are many reasons not to use them. There is quite a bit of information out in internet land on the subject.

    Keep in mind that any kind of caching plugin will render PHP sessions useless.

    Edit: I haven't looked at the plugin, but you'll need to verify a couple of things: that it starts a session using the 'init' hook, and that it also calls session_destroy() on the wp_login and wp_logout hooks.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[9556595].message }}
  • Profile picture of the author ThomasTe
    Thanks, guys. Guess, I'll just keep it in the header.php manually.

    Cheers,
    Thomas
    {{ DiscussionBoard.errors[9558807].message }}
  • Profile picture of the author newgadget123
    code insert manager plugin which is use to add code after before <head> tag.
    {{ DiscussionBoard.errors[9566918].message }}
  • Profile picture of the author ThomasTe
    Thanks. However, it looks like it cannot insert before <html> or am I missing something?

    BR,
    Thomas
    {{ DiscussionBoard.errors[9567227].message }}

Trending Topics