3

I'm writing a standalone script for Wordpress so I can display things like the username on a page outside of Wordpress.

My method so far is to try to include or require wp-load.php in my script directory. I've tried

chdir ( ".." );
include "wp-load.php";

as well as absolute paths to the web root where Wordpress is located for both the chdir and the include. I don't get any errors but wp_get_current_user() always returns empty.

If I move my script up a level to the same folder with Wordpress it works fine. How can I include wp-load.php and keep all of the files in my subfolder?

EDIT: file structure

  • public_html (wordpress is installed here so wp-load.php is also here)
    • myScript (directory)
      • script.php
      • This is where I try to use chdir ( ".." ) or include ( "../wp-load.php" ) without success
3
  • and please tell me the where you exactly include the wp-load.php
    – user147874
    Commented Dec 18, 2018 at 6:21
  • did you got the result @scottinthebooth ? Commented Dec 18, 2018 at 11:35
  • I was not able to get this to work. I ended up just converting the script to a full plugin which solved the problem (if I'd realized how simple that was I would have started that way) Commented Dec 19, 2018 at 23:10

2 Answers 2

1

try this i hope this work.

$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );    
require_once( $parse_uri[0] . 'wp-load.php' );
4
  • This did not work. My script is in a folder "next to" wordpress, not being served by wordpress itself so I'm not inside the wp-content folder Commented Dec 18, 2018 at 6:47
  • please put here you folder hierarchy
    – user147874
    Commented Dec 18, 2018 at 6:53
  • I edited the original post to include it Commented Dec 18, 2018 at 6:55
  • ottopress.com/2010/dont-include-wp-load-please please read it carefully. thanks
    – user147874
    Commented Dec 18, 2018 at 7:16
1

Use this to include

<?php include '../wp-load.php'; ?>

that will give you the wp-load.php file ../ is usually use for get path of last folder.

hope this helps you and also let me know the result.

1
  • I was not able to get this to work. I ended up just converting the script to a full plugin which solved the problem (if I'd realized how simple that was I would have started that way) Commented Dec 19, 2018 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.