• Resolved Luuk Faasse

    (@luukf8)


    Hello,

    I’m building a WordPress website, and I have to insert a PHP code BEFORE the <html> tag on every page. My site is still in maintenance mode using a plugin. But, I have to insert a PHP code before the <html> tag before it will be published. Here’s an example of what I want:

    <?php
    // my code here
    ?>
    <!DOCTYPE html>
    <html>

    Does anyone have a solution for this? I hope this will be fixed, I want to publish my site soon. Thanks in advance.

    Regards,
    Luuk Faasse

Viewing 3 replies - 1 through 3 (of 3 total)
  • There’s a variety of hooks that run before the HTML is output, but which is appropriate depends on what exactly your code is doing. What does your code do?

    Thread Starter Luuk Faasse

    (@luukf8)

    My code is detecting an affiliate string. This is my code:

    <?php
    //DETECT AFFILIATE ID
    if(empty($_SESSION["aid"]))
    {
    $aid = $_GET["aid"];
    if(empty($aid))
    $aid = $_COOKIE["aid"];
    if(empty($aid))
    $aid = 1;
    setcookie("aid", $aid, strtotime("+10 years"), "/");
    $_SESSION["aid"] = $aid;
    }
    ?>

    It’s setting a cookie when an “aid” query string is entered.

    Moderator bcworkz

    (@bcworkz)

    You cannot set cookies with PHP in HTML head sections. Cookies must be set before any output is sent out to the browser. Adding your code to a callback hooked to “init” should be OK.

    Alternately, set the cookie with JavaScript.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Insert a PHP head code to a WP website’ is closed to new replies.