Title: Insert a PHP head code to a WP website
Last modified: March 11, 2018

---

# Insert a PHP head code to a WP website

 *  Resolved [Luuk Faasse](https://wordpress.org/support/users/luukf8/)
 * (@luukf8)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/insert-a-php-head-code-to-a-wp-website/)
 * 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)

 *  [Jacob Peattie](https://wordpress.org/support/users/jakept/)
 * (@jakept)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/insert-a-php-head-code-to-a-wp-website/#post-10062064)
 * 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](https://wordpress.org/support/users/luukf8/)
 * (@luukf8)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/insert-a-php-head-code-to-a-wp-website/#post-10062149)
 * 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](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/insert-a-php-head-code-to-a-wp-website/#post-10062662)
 * 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.

## Tags

 * [code](https://wordpress.org/support/topic-tag/code/)
 * [Coding](https://wordpress.org/support/topic-tag/coding/)
 * [developing](https://wordpress.org/support/topic-tag/developing/)
 * [editing](https://wordpress.org/support/topic-tag/editing/)
 * [head](https://wordpress.org/support/topic-tag/head/)
 * [header](https://wordpress.org/support/topic-tag/header/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/insert-a-php-head-code-to-a-wp-website/#post-10062662)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
