I am trying to use WordPress functions on a custom file but doesn't seems to be working. The file is placed in root directory. Please help me out
In any custom page you can call below three lines to pull up WordPress stack and then use any WordPress functionality
<?
//Imp to include
include('wp-load.php');
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
// check is user is logged - if yes then print its role
if(is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
echo "role is ".$role[0];
}
?>
Just include wp-load.php
file at the top of the custom PHP file
require_once("../../../../wp-load.php");
Note: add slashes and dots
../../../../ as per the custom PHP file exists.
You can access any wordpress inbuilt function in any external file by adding wp-load.php
file. If your file is in the root directory. Then, you can just add following line of code on top.
require_once("wp-load.php");
For more details and example, you can visit How to access Wordpress functions in external file