Access WordPress functions in a different php file

2019-06-26 13:03发布

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!

标签: php wordpress
2条回答
可以哭但决不认输i
2楼-- · 2019-06-26 13:12

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-06-26 13:20

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.

查看更多
登录 后发表回答