2

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:

<?php
   if ($_SERVER['PHP_SELF'] = '/')
   include('some-file.php');
?>

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

<?php
   if ($_SERVER['PHP_SELF'] == '/')
   include('some-file.php');
?>

and it still isnt showing up.

7 Answers7

5

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

Thus, your code should be:

<?php

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

    if ( is_front_page() ) {
    include('some-file.php');
}

?>

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

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

if ( $_SERVER["REQUEST_URI"] == '/' ) {
    include('some-file.php');
}

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.

<?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 == ''){
      include('some-file.php');
   }

?>

Hope the above methods help, thanks!

Panda
  • 6,955
  • 6
  • 40
  • 55
  • i did but nothing gets displayed (on homepage or anyother pages). if i leave it with only "=" it gets shown everywhere – neven stepar Feb 08 '16 at 09:03
  • I've updated the code again so that it will return true regardless of whether the front page is a static page or a blog posts index. Initially, I've used `is_home()`, but it would only return true on a blog posts index. – Panda Feb 08 '16 at 09:10
  • This would work perfectly, but all my pages are using same page template (page-homepage.php).Plugin that i am using is using that template to display content, so it gets displayed on all pages -.- – neven stepar Feb 08 '16 at 09:24
  • @nevenstepar Did you try both `is_home()` and `is_front_page()`? – Panda Feb 08 '16 at 09:24
  • Yep, i did :( It is this damn plugins fault, everything is shown on the same page (body class - home page page-id-5 page-template page-template-page-homepage) – neven stepar Feb 08 '16 at 09:27
  • @nevenstepar Try `wp_reset_query(); $thispage = $post->ID; if ( $thispage < 1 )` – Panda Feb 08 '16 at 09:29
  • @nevenstepar `if ( $_SERVER["REQUEST_URI"] == '/' ) {include('some-file.php'); }` This might work – Panda Feb 08 '16 at 09:32
  • This last one isnt working, but the one before i dont know how to set up :D should it go inside if statement or... – neven stepar Feb 08 '16 at 09:36
  • @nevemstepar Should be `wp_reset_query(); $thispage = $post->ID; if ( $thispage < 1 ) include('some-file.php');` – Panda Feb 08 '16 at 09:36
  • Damn, no :( I am using this in header.php, could that be the problem... or not... And i really apologize for boring you this much – neven stepar Feb 08 '16 at 09:41
  • @nevenstepar This function can be used in `header.php`, `page.php`, or `footer.php`. Maybe, try last 2 files? – Panda Feb 08 '16 at 09:46
  • @nevenstepar If the code really doesn't work, using a plugin such as https://wordpress.org/plugins/insert-php/ may help :) – Panda Feb 08 '16 at 10:08
  • i know, but if i am right, this only ads it to the page content area, and i need it in header :D – neven stepar Feb 08 '16 at 10:27
  • @nevenstepar Have you tried adding `` before the `is_front_page()` code? – Panda Feb 08 '16 at 10:37
  • I did :( So, there is only one page called Homepage and it is using homepage template.Inside that page i have a shortcode for product catalogue that displays categories and products. Once user clicks on product link, it is shown, but, using the same page template. Plugin, ofcourse, doesnt have any advanced options so i can select the template -.- Page URL is like www.domain.com/folder.But, when product is opened, the page links goes like www.domain.com/folder/product/product-name Is it possible to include it for all pages and then to remove it for all /product pages? Again, php newbie :( – neven stepar Feb 08 '16 at 10:44
  • @nevenstepar Added another method, it would most probably work, thanks :) – Panda Feb 08 '16 at 11:00
2

There's a couple of issues with your code:

<?php
   if ($_SERVER['PHP_SELF'] = '/')
   include('some-file.php');
?>

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:

<?php
   if (basename($_SERVER['PHP_SELF']) == 'index.php')
   include('some-file.php');
?>

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

Repox
  • 15,015
  • 8
  • 54
  • 79
  • I completely ignored the `wordpress` tag. The answer @luweiqi came with would work for your setup. My answer just addresses the issues in your code. – Repox Feb 08 '16 at 09:07
0

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.

Soundz
  • 1,308
  • 10
  • 17
  • i did but nothing gets displayed (on homepage or anyother pages). if i leave it with only "=" it gets shown everywhere – neven stepar Feb 08 '16 at 09:04
  • try looking at what `$_SERVER['PHP_SELF']` actually is (`echo $_SERVER['PHP_SELF'];die;`) – Soundz Feb 08 '16 at 09:05
0

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.

Paul Dunlop
  • 327
  • 1
  • 5
0

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

<?php
   if (get_the_ID() == 2)
   include('some-file.php');
?>

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

Ram
  • 48
  • 4
  • I did tried that but i am using some weird catalogue plugin and all pages are using the same page template (page-homepage.php) with the same ID, so it gets displayed on all pages – neven stepar Feb 08 '16 at 09:20
  • You check all your pages ?they have same id ? – Ram Feb 08 '16 at 09:24
  • yep, same page template and same ID -.- – neven stepar Feb 08 '16 at 09:28
  • could you please attach the screenshort of url with the question , then everyone can easily understand situation . don't forgot to hide your domain name – Ram Feb 08 '16 at 09:29
0

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.

James Jones
  • 3,850
  • 5
  • 25
  • 44
  • I did tried that (first thing) but i am using some weird catalogue plugin and all pages are using the same page template (page-homepage.php) so it gets displayed on all pages – neven stepar Feb 08 '16 at 09:20
  • @nevenstepar That shouldn't matter. In WordPress admin settings you can set which page is the front page. That is what the function checks for. – James Jones Feb 08 '16 at 09:28
0
  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

     <?php
      if ($_SERVER['PHP_SELF'] == '/index.php'){
          include_once('some-file.php');
      }
     ?>