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.
Related
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.
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.
I have the following issue. I want to load the Wordpress environment into an external php file. All my work is currently on a localhost and my php code is as follows:
<?php
require("../../wp-blog-header.php");
$admin = do_shortcode("[su_user field='user_email']");
if ($admin === "admin#email.com") {
// Do stuff here
}
else {
echo "<h1>Forbidden.</h1>";
}
This works perfectly fine in my localhost, so I figured that when I upload the file to the server it must work too, given that the file is stored in the same directory as it is locally, hence ../../wp-blog-header.php. Sadly, this does not work. When I echo $admin; the following error appears:
User: user ID is incorrect
I tried using the native Wordpress functions without using the shortcode like this:
<?php global $user_email;
get_currentuserinfo();
echo $user_email;
?>
This does not work either. Is there a more efficient way to load the Wordpress environment into an external php file? Any suggestions on what is happening here? It does not necessarily have to be an e-mail, it could be the user id as well, which I tried and gives me 0.
I forgot to mention, the php file loads and echoes Forbidden together with the error. This makes me believe that the file require("../../wp-blog-header.php") is actually being loaded because when I change the directory I directly get an error that the file is now working/available.
The shortcode works as I have tested it inside a new Page returning the e-mail address of the logged-in user.
UPDATE
My root is:
/var/www/vhosts/test-website.com/httpdocs/
Which I tested with require("/var/www/vhosts/test-website.com/httpdocs/wp-blog-header.php") as well. Still same result. The weird thing is that if I add wp_head() below this line, the header gets loaded. I reformulate my question:
How can I get the current e-mail address of a logged-in user in an external php file?
If your file is same folder root with Wordpress instance. just use code bellow:
<?php
define( 'ROOT_PATH', dirname(__FILE__) );
require(ROOT_PATH . "/wp-blog-header.php");
//your stuff
very new to this, so I apologize in advance for rookie mistakes.
I am currently working through one of the PHP/MySQL introductory series from lynda.com. I am loving it thus far, and have been having success, but this one's got me confused.
I have created a form for user input, called new_subject.php. The following is called create_subject.php, and it meant to process the form data from new_subject.php. Please keep in mind that I have not yet added any code to do this, I am just testing a redirect function:
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php
if (isset($_POST['submit'])) {
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
?>
<?php
if (isset($connection)) {mysqli_close($connection);}
?>
The point of the redirect_to function is to redirect the user to the form at new_subject.php if they were to type in create_subject.php manually in the browser. Here is what redirect_to function looks like in functions.php:
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
I am getting the following error when trying to get to create_subject.php manually:
Fatal error: Call to undefined function redirect_to()
The tutorial video says that I can either turn on output buffering, which I have tried to do in my php.ini file, but the error remains the same. The video says I can do that, or "fix the white space issue." I have no idea what this means.
I would appreciate any info on what this "white space issue" is, and if there is anything else I can do here. As far as I can see, I should be able to call this function without issues.
Thanks
for the whitespace issue, there must be no output prior to calling location header, in your code.. there must be no whitespace outside the php tags..
just rewrite so that all those are enclose in a single php tag and make sure there is no space outside of that, even a single one.
<?php
require_once("../includes/db_connection.php");
require_once("../includes/functions.php");
if (isset($_POST['submit'])) {
} else {
// This is probably a GET request
redirect_to("new_subject.php");
}
if (isset($connection)) {mysqli_close($connection);}
?>
read here.. How to fix "Headers already sent" error in PHP
however, your main issue is the function is not found..
check your functions.php and make sure redirect_to is define properly. if that is the case, then most likely issue is in your "require_once" must be pointing to a non existent file. can you tell us your folder structure?
your functions.php must be located in a folder called includes located outside the folder where your create_subject.php is located.
if the 'includes' folder is located inside the folder where create_subject.php is, then change your require_once to
require_once("includes/db_connection.php");
require_once("includes/functions.php");
if both are located in the same folder, then change your require_once to
require_once("db_connection.php");
require_once("functions.php");
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