Home Ask Login Register

Developers Planet

Your answer is one click away!

neven stepar February 2016

PHP include only on homepage URL

I need to include external php file only on homepage of my website.

But, since all the pages on the site use the same page template (homepage template), I cant filter them based on that so I was wondering is there a way to include PHP file ONLY on homepage URL (which is www.domain.com/folder) and not to show it on any other page (for example www.domain.com/folder/lorem).

I tried using this snippet in my header.php file:

   if ($_SERVER['PHP_SELF'] = '/')

and the file gets included on all other pages as well.

I am a PHP newbie so sorry if it is stupid question :)

UPDATE: I did changed it to

   if ($_SERVER['PHP_SELF'] == '/')

and it still isnt showing up.


Soundz February 2016

if-statements always break down to a true or false, one = is an assignment

Your error results in saying $_SERVER['PHP_SELF'] IS '/' and therefore true.

You must use == for comparison or === for typesafe comparison.

luweiqi February 2016

You can use WordPress's is_front_page() function to check.

Thus, your code should be:


    // if code does not work, adding the next line should make it work
    <?php wp_reset_query(); ?>

    if ( is_front_page() ) {


Source: https://codex.wordpress.org/Function_Reference/is_front_page

Alternatively, if the above is not working, you can try:

if ( $_SERVER["REQUEST_URI"] == '/' ) {

As a last resort, try using plugins to insert PHP directly into the pages, one such plugin is https://wordpress.org/plugins/insert-php/.

UPDATE: After the elaboration in comments, I've come up with an alternate method, as shown below.

In your case, this might work. This code would get the URL first, then parse it to get the directory, and assign the directory to $directory. If it is a on the homepage, the $directory will not be set, thus include some-file.php.


   // Get URL
   $link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

   // Get Directory (eg. will return 'folder' in example.com/folder/files)
   $parts = explode('/', $link);
   $directory = $parts[3];

   // If $directory is null, include PHP (eg. example.com, there is no directory)
   if ($directory == ''){


Hope the above methods help, thanks!

Repox February 2016

There's a couple of issues with your code:

   if ($_SERVER['PHP_SELF'] = '/')

As already mentioned your comparison (==) isn't working as you are actually using assignment (=).

Second, the super global variable $_SERVER['PHP_SELF'] will never contain only / as that variable will contain a path and filename to the file that's currently executing, as stated in the documentation.

So you have to single out your file and of course use the correct way of comparison. So the script might look something like the following instead:

   if (basename($_SERVER['PHP_SELF']) == 'index.php')

Of course, this won't work as expected if you have multiple index.php files in separate directories.

Paul Dunlop February 2016

From: http://php.net/manual/en/reserved.variables.server.php

'PATH_INFO' is probably what you want to use:

Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff.

Ram February 2016

For every wordpress page there is an id .So you can write this condition based on id so (1)Please find your home page id

if 2 is your home page id then write the following code in template file after the header

   if (get_the_ID() == 2)

for to know details about get_the_ID() read this https://developer.wordpress.org/reference/functions/get_the_ID/

James Jones February 2016

Use is_front_page() in your conditional. This returns true when you're on the page you nominated as the home page. Don't use is_home(). That returns the blog post archive page.

I know ... confusing right? But that's WordPress for ya.

Nagy Zoltan February 2016

  1. You should change your include to include_once, so the file will be included only one time.
  2. $_SERVER['PHP_SELF'] comparison to '/' makes no sense. Instead of '/' try to use the path of your index file. Another way would be to use the __FILE__ constant, but that will work only on your environment

      if ($_SERVER['PHP_SELF'] == '/index.php'){

Post Status

Asked in February 2016
Viewed 3,361 times
Voted 10
Answered 7 times


Leave an answer

Quote of the day: live life