Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access WordPress functions in a different php file?

Tags:

php

wordpress

How am I able to call the built-in WordPress functions (specifically wp_get_user()) in a different file (say, x.php)?

Here's my situation. If I call wp_get_current_user() on the index.php file within my theme directory, it works just perfectly.

However, if I have x.php and called that, I got an error. I googled around, and am now including wp-load.php (at it's correct path). Now it's just giving me an associated array with no values.

The associated array is:

WP_User Object ( [data] => [ID] => 0 [id] => 0 [caps] => Array ( ) [cap_key] => [roles] => Array ( ) [allcaps] => Array ( ) [first_name] => [last_name] => [filter] => )

I tried including index.php in this file, and it still spits out that stuf above, instead of the correct information (which is shown by a print_r on index.php).

Thank you!

like image 751
Connor Avatar asked Dec 22 '22 10:12

Connor


2 Answers

try also including the header <?php require('{correct_path}/wp-blog-header.php'); and if that doesn't work also try re-declaring the variables your using to global $var in your wordpress setting php.

like image 171
Chamilyan Avatar answered Dec 24 '22 01:12

Chamilyan


See http://codex.wordpress.org/Integrating_WordPress_with_Your_Website

First, use either

<?php  define('WP_USE_THEMES', false); require('./wp-blog-header.php'); ?>

or

<?php require('/the/path/to/your/wp-blog-header.php');?>

and then call your WP functions.

like image 22
markratledge Avatar answered Dec 24 '22 02:12

markratledge