PHP - Fatal error: Call to undefined function wp_get_current_user() - php

I have a WP site and I am trying to create a custom page.
I am getting a Fatal error: Call to undefined function wp_get_current_user()
When I run this code:
<?php
$current_user = wp_get_current_user();
echo 'User ID: ' . $current_user->ID . '<br />';
?>
This is the url:
https://[url of my site]/lesson1.php
I know the reason why. A normal worpress page url looks like this:
https://[url of my site]/index.php/lesson1
I think the pages are stored in a database and the index.php is the resource that gets them somehow.
So the question is, if I want to create a PHP file, which is separate from the WP install, how can I access functions, such as wp_get_current_user() ?

Wordpress's wp-load.php file contains all of it functions / connections , you can do it with the following way and include the main wordpress file into your php file to get wordpress functions into your file.
<?php
define('WP_USE_THEMES', false);
require('./wp-load.php');
?>

If you are creating page on the root, you will require wp-load.php file, so include it in your page.
require( 'wp-load.php' );
Hope this will help you.

Related

Including wp-load.php in custom .php file causes page processing to stop. Why ?

I have got a WP website running version 4.8.2.
I have created, in the root folder of my theme, a custom php page that includes Wordpress functions with the following code:
<?php
echo 'ralph';
clearstatcache();
echo file_exists('../../../wp-load.php');
require_once('../../../wp-load.php');
echo 'joe';
?>
The end result should be displaying on the page the string 'ralph1joe' (1 stands for the fact that wp-load.php actually exists).
Problem is: when I launch the custom page only the string 'ralph1' is visualized. Any instruction after the require_once line, is ignored. I cannot see any error both on the page and in the log files. The process of the page is simply interrupted.
Is there anything I can do for having the require_once line to work properly ?
I think the problem is with wp_load.php or maybe you haven't read the wp_load.php file correctly.
Wp_load always requires wp_config.php If it doesn't get it it will die with an error. See the below code in wp_load.php file
$die = __( "There doesn't seem to be a <code>wp-config.php</code> file. I need this before we can get started." ) . '</p>';
With some more identical code like this and error associated with it.
so when you require_once 'wp_load.php' and It doesn't get wp_config.php It will die and stop the execution and you won't get any further print statement
Hope it clarifies you.
I solved the problem. A plugin I had installed caused evidently a conflict.
The plugin is called All 404 Redirect to Homepage
Deactivating it caused my custom page to work properly again.
In my code this error was caused by the required line inside a function. I removed and placed this line at the main code.

wordpress prevent error reporting

i'm making a custom theme and noticed one thing. when i try to go directly to my theme index.php file through url (or anypage) for example:
localhost/wordpress/wp-content/themes/my-theme/index.php
this error will appear.
Fatal error: Call to undefined function get_header() in ... on line...
i've tried to download and activate other themes but the result is the same.
how can i prevent this from happening?
i've tried adding the following lines to the wp-config.php
error_reporting(0);
#ini_set('display_errors', 0);
but with no results.
i've also tested it "live" but with no results.
thank's in advance.
Correct me if I'm wrong but that error occurs when someone tries to directly access the theme's index.php.
For example when a bot tries to access it.
You should never access it directly like this:
<?php get_header(); ?>
But rather try it like this:
<?php
if (function_exists('get_header')) {
get_header();
} else {
// Here goes whatever you want to do when the file is accessed directly.
}
?>

Not able to access class object in php file loaded via ajax

I am running into a little problem with the below, I am trying to learn for some future projects and need a hand.
Here is the setup:
I have a user class in php in a models directory.
I have a home.php file in my root directory.
I have included the model for the user class in the home.php file and am able to access it just fine.
I have a profile.php file that I would like to load via ajax onto the home.php file.
I am able to load the profile.php file just fine, however the problem is that the info on that page which is just being called via <?php echo $user->first_name; ?> is not being displayed when I load the page via ajax. However if I load the page normally without ajax, it displays just fine.
What I have tried so far:
I have tried to include the user class in the profile page, however it is giving me an error that it is already included.
I would rather not place this into a session variable as I would like to be able to access the class from here.
I also need to be able to access the user class on other screens, that is why I have included it in the home.php file.
Any help would be greatly appreciated as I am rather new to working with php and ajax.
Thanks
Here is some of the code that I am working with.
home.php
//Here we include are models that we need access to.
include 'apps/user/model/user.php';
$my_user = new User();
global $user;
$user = $my_user->getUserById($_SESSION['loggedin_user_id']);
profile.php
<input type="text" value="<?php echo $user->username; ?>" class="input-xlarge">
javascript to load the form via ajax
$.ajax({
url: 'apps/user/view/profile.php',
success: function(data){
if(data != null) $("#content").html(data);
}
});
I also get the following error when I view the console.
[01-Jun-2013 10:38:03] PHP Notice: Undefined variable: user in /Users/btackett1377/development/Test/Base/apps/user/view/profile.php on line 23
Have you tried using require_once instead of include? I would try requiring the user class in both the home and profile pages.
For your reference, PHP has four functions for including files:
include - Blindly include the requested file with no regard for whether it's been included already. Warns you if the file is not found.
include_once - Ensure that the file is only included once. Also warns you if the file is not found.
require - Similar to include, but causes fatal error if the requested file could not be found.
require_once - Similar to include_once, but causes fatal error if the file is not found.

Get logged in user info - call to undefined function (2)

I'm reposting this question because the problem wasn't solved.
In a relevant question the answer below may be the solution.
Apparently this is happening because the file /wp-includes/pluggable which contains the function doesn't get loaded until after the plugins are loaded.
Indeed it is. So wrap whichever thing you're doing in a function, and hook it onto the plugins_loaded or init hook. (see the wp-settings.php file)
Where exactly do I write the add_action('plugins_loaded','my_function'); command?
In the "wp-settings file", the "pluggable" or my own php file?
All I want to do is to display the user info in a php file which is loaded in a page in wordpress,but I'm constantly getting the same error "call to undefined function".
I've tried including the relevant php file, still doesn't work.
<?php
global $current_user;
get_currentuserinfo();
echo $current_user->user_login;
?>
This might work for you!!
require (ABSPATH . WPINC . '/pluggable.php');
global $current_user;
get_currentuserinfo();
echo $current_user->user_login;
All you do is include the pluggable.php file in your plugin.

wordpress get current user

I have a directory inside my wordpress directory for some template application
apacheWWW/wordpress/jtpc
In my application i want the wordpress current user id
I am able to do this in one page but in the other I get an error
This is what I am doing to get the user id:
require_once( '/../../wp-load.php' );
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
but I get an error on this line
require_once( '/../../wp-load.php' );
The error says
File does not exist: D:/programming/apacheWWW/wordpress/jtpc/ajaxHandlers/wp-admin, referrer: http://localhost/wordpress/?page_id=23
what dose he wants with the wp-admin ,
and why he is looking for it in the wrong directory ,
It's suppose to be under
D:/programming/apacheWWW/wordpress/
It's working for one page, (I am uploading file and creating a directory for this user id)
but for the other page I send an ajax request to delete files by user request
so I need to id again, but there he throws the error
ok got i working i put
require_once( '/../../wp-load.php' );
at the top of the page
and not inside the function
My issue is that I've set up a standalone ajax controller for my plugin's admin.
In hindsight, I wish I didn't, because now I need $current_user->ID on a particular method.
using the following code gives me undefined function errors:
global $current_user;
get_currentuserinfo();
same when I use this:
$current_user = wp_get_current_user();
I've used GREP to trace the spaghetti function calls, and was able to get stable using the following chain of includes within my ajax_request method of my base controller:
include APP_ROOT.'wp-includes'.DS.'meta.php';
include APP_ROOT.'wp-includes'.DS.'plugin.php';
include APP_ROOT.'wp-includes'.DS.'user.php';
include APP_ROOT.'wp-includes'.DS.'capabilities.php';
include APP_ROOT.'wp-includes'.DS.'load.php';
include APP_ROOT.'wp-includes'.DS.'functions.php';
include APP_ROOT.'wp-includes'.DS.'pluggable.php';
(I have defined my own APP_ROOT, which is the include path to the base of the whole application.)
This eliminates my errors, but I am still getting a user id of 0, and data is NULL, which doesn't solve my problem.
Surely there is a better way to do this. Has anyone deciphered the cookie or session variables for direct access?
Any and all suggestions would be welcome. I know I'm asking a lot for wordpress to work like normal OOP/MVC code...
In addition, I have deployed a CodeIgniter front-end, and I am using WordPress solely for the back-end. I highly recommend this for ultimate flexibility if you are forced to use WordPress. Front-end hooks are a pain, and really kind of "hacky". Here's where I got the idea:
http://jidd.jimisaacs.com/post/wordigniter-wordpress-codeigniter/
So if anyone has any questions about that, I bet I could help.
Best regards, happy coding.
You may want to try one of the following two lines:
require_once STYLESHEETPATH . '/wp-load.php';
or
require_once TEMPLATEPATH . '/wp-load.php';
There is a slight difference between the STYLESHEETPATH constant and the TEMPLATEPATH constant. If you are developing a child theme, STYLESHEETPATH will be the child theme path, while TEMPLATEPATH will be the parent theme path.
This is how you can get current user:
global $current_user;
$current_user = wp_get_current_user();
After that, you can use $current_user->ID where you want.
More Info:
Function Reference/wp get current user

Categories