0

I have the following issue. I want to load the Wordpress environment into an external php file. All my work is currently on a localhost and my php code is as follows:

<?php
require("../../wp-blog-header.php");

$admin = do_shortcode("[su_user field='user_email']");

if ($admin === "admin@email.com") {

    // Do stuff here

}

else {

    echo "<h1>Forbidden.</h1>";

}

This works perfectly fine in my localhost, so I figured that when I upload the file to the server it must work too, given that the file is stored in the same directory as it is locally, hence ../../wp-blog-header.php. Sadly, this does not work. When I echo $admin; the following error appears:

User: user ID is incorrect

I tried using the native Wordpress functions without using the shortcode like this:

<?php global $user_email;
      get_currentuserinfo();

      echo $user_email;
?>

This does not work either. Is there a more efficient way to load the Wordpress environment into an external php file? Any suggestions on what is happening here? It does not necessarily have to be an e-mail, it could be the user id as well, which I tried and gives me 0.

I forgot to mention, the php file loads and echoes Forbidden together with the error. This makes me believe that the file require("../../wp-blog-header.php") is actually being loaded because when I change the directory I directly get an error that the file is now working/available.

The shortcode works as I have tested it inside a new Page returning the e-mail address of the logged-in user.


UPDATE

My root is:

/var/www/vhosts/test-website.com/httpdocs/

Which I tested with require("/var/www/vhosts/test-website.com/httpdocs/wp-blog-header.php") as well. Still same result. The weird thing is that if I add wp_head() below this line, the header gets loaded. I reformulate my question:

How can I get the current e-mail address of a logged-in user in an external php file?

Ava Barbilla
  • 968
  • 2
  • 18
  • 37
  • When you were running on localhost were you running this in a Virtual Host? Or were you running it as `localhost/folder/...` – RiggsFolly May 11 '16 at 21:51
  • @RiggsFolly as `localhost/folder/...` to be precise I use MAMP. Hope this is of any help. – Ava Barbilla May 11 '16 at 21:54
  • What is `DocumentRoot` set to on your local dev machine. It will not be the same on a LIVE web site – RiggsFolly May 11 '16 at 21:57
  • To load the WP environment externally you need to include wp-load.php – Tim Malone May 11 '16 at 22:00
  • Out of curiosity, did you try using `require(WP_ROOT_PATH . 'wp-load.php');` instead of including just `wp-blog-header`? – jeffjenx May 11 '16 at 22:00
  • @RiggsFolly my `DocumentRoot` is `htdocs/test-website/wp-blog-header.php` **htdocs** being a subfolder from the application MAMP and **test-website** the name of the website (domain), as a live example the url would be: http://127.0.0.1:8888/test-website/wp-blog-header.php – Ava Barbilla May 11 '16 at 22:04
  • @Quantastical if I add your code, the php file stops working. – Ava Barbilla May 11 '16 at 22:07
  • @AvaBarbilla WP_ROOT_PATH needs to be defined/changed to WordPress's root directory. – jeffjenx May 11 '16 at 22:08
  • @Quantastical Thats what I did. Does not work :( – Ava Barbilla May 11 '16 at 22:14
  • But I bet when you put it live you run it like this `xxx.com` or `xxx.com/wp-blog` so `../../wp-blog-header.php` wont be the correct path to this file. Thats the problem with developing out of subfolders of localhost and then running on a live site configuration [See this, its aimed a WAMPServer users but it applies to any Apache config](http://stackoverflow.com/questions/23665064/project-links-do-not-work-on-wamp-server/23990618#23990618) – RiggsFolly May 11 '16 at 22:45
  • @RiggsFolly yes you are right it would be `test-website.com/wp-blog-header.php` – Ava Barbilla May 11 '16 at 22:48
  • Set up a Virtual Host on your local test system then you can mimic the exact LIVE environment. Fix the path issue and then you dont have to mod code when you move it from DEV to LIVE. You can have many Virtual Hosts running on a single Apache so you can develop any number of sites without worrying about path issues ever again – RiggsFolly May 11 '16 at 22:49
  • @RiggsFolly I did as you told me. I am still stuck with this. I updated my question hope you can help me with that perhaps. – Ava Barbilla May 12 '16 at 08:09

1 Answers1

0

If your file is same folder root with Wordpress instance. just use code bellow:

<?php
define( 'ROOT_PATH', dirname(__FILE__) );
require(ROOT_PATH . "/wp-blog-header.php");

//your stuff

tuongpg.jz
  • 104
  • 5