error77
asked on
Wordpress get current user data from external php page
I have a new php page and I'm using wordpress for my website.
From within my new php file I've included wp_load..
include ('wp-load.php');
which I believe is required in order to retrieve current user details.
I've then tried to echo the current loginname:
$current_user->user_login
but it's coming out empty and I know it's not..
I'm I missing anything?
From within my new php file I've included wp_load..
include ('wp-load.php');
which I believe is required in order to retrieve current user details.
I've then tried to echo the current loginname:
$current_user->user_login
but it's coming out empty and I know it's not..
I'm I missing anything?
ASKER
I've tried all that and it still returns 0 or NULL so it's not catch the current user info
I'm trying this:
I'm trying this:
require_once($_SERVER['DOCUMENT_ROOT'] .'/wp-load.php');
global $current_user;
$current_user = wp_get_current_user();
var_dump($current_user);
$user_id = get_current_user_id();
echo $user_id;
Is the main site powered by WordPress? Or are you trying to just get the user info loaded into a non WP website?
ASKER
Yes, the main site is powered by wordpress and my php file from which I'm trying this code in sctually in my wordpress theme directory.
I'm sctually logged into my wordpress site ... This is a separate php page inside the wordpress website.. I just need to be able to get the wordpress current user details from this page.
I'm sctually logged into my wordpress site ... This is a separate php page inside the wordpress website.. I just need to be able to get the wordpress current user details from this page.
Here is a link to all available User Tags:
http://www.dbswebsite.com/design/wordpress-reference/V3/author.php
Let me know if you are still unable to get the info.
http://www.dbswebsite.com/design/wordpress-reference/V3/author.php
Let me know if you are still unable to get the info.
ASKER
Hi jeremyjared74,
I know the tags but my php page is not currently part of wordpress. I could include the header and the problem would be solved but I don't want to include all the things that come with the header...I just need to current user details from wordpress.
When creating a wordpress template you normally get by including wp-load.php .. but on a standalone php it doesn't seem to work...
I know the tags but my php page is not currently part of wordpress. I could include the header and the problem would be solved but I don't want to include all the things that come with the header...I just need to current user details from wordpress.
When creating a wordpress template you normally get by including wp-load.php .. but on a standalone php it doesn't seem to work...
I see what you're needing now. I've not tried this, but maybe the article from WP-Candy will help. It seems to be the same as what you're doing with the exception that they are defining the path to wp-load:
http://wpengineer.com/1038/embed-wordpress-functions-outside-wordpress/
http://wpengineer.com/1038/embed-wordpress-functions-outside-wordpress/
ASKER
Sorry, that didn't work for me.
Other people comented the same problem too.
Other people comented the same problem too.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Hi jeremyjared74,
I know for a fact that your code above will work as a template because I am using some template which I've created already and it works.
The problem is that this is a standalone page which I only need for moving some uploaded images from one directory to another.
Here is the full code of this page:
<?php
$targetFolder = '/uploads/'; // Relative to the root
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_n ame'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
////$targetFile = rtrim($targetPath,'/') . $_FILES['Filedata']['name' ];
$targetFile = $targetPath . $_FILES['Filedata']['name' ];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif',' png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata ']['name'] );
if (in_array($fileParts['exte nsion'],$f ileTypes)) {
move_uploaded_file($tempFi le,$target File);
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
I need to change the targetFolder to:
..so
$targetFolder = '/uploads/'.$current_user- >user_logi n); ?>.'/images/';
The above is what I need to do BUT for this I need to know the value of the user login name and this is why I need to get this information.
At the moment it is not displaying.
Hope this helps
Thanks
I know for a fact that your code above will work as a template because I am using some template which I've created already and it works.
The problem is that this is a standalone page which I only need for moving some uploaded images from one directory to another.
Here is the full code of this page:
<?php
$targetFolder = '/uploads/'; // Relative to the root
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_n
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
////$targetFile = rtrim($targetPath,'/') . $_FILES['Filedata']['name'
$targetFile = $targetPath . $_FILES['Filedata']['name'
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','
$fileParts = pathinfo($_FILES['Filedata
if (in_array($fileParts['exte
move_uploaded_file($tempFi
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
I need to change the targetFolder to:
..so
$targetFolder = '/uploads/'.$current_user-
The above is what I need to do BUT for this I need to know the value of the user login name and this is why I need to get this information.
At the moment it is not displaying.
Hope this helps
Thanks
ASKER
As for your question answers:
Is WordPress in the root directory of your server? - YES
Does your Database use the default prefix wp_ for the table names? - YES
Have you tried using WordPress custom page feature to load the page? But I would need to add it as a template to an actual page and display it in order to use it?
Is WordPress in the root directory of your server? - YES
Does your Database use the default prefix wp_ for the table names? - YES
Have you tried using WordPress custom page feature to load the page? But I would need to add it as a template to an actual page and display it in order to use it?
ASKER
I'm thinking...
This is the way I'm reaching the file I'm on about ... it's via javascript...
<script type="text/javascript">
$(document).ready(function () {
$('#file_upload').uploadif y({
'uploader' : '<?php bloginfo('template_directo ry'); ?>/uploadify.php',
The line above calls the file I'm on about, which is called uploadify.php ...
Would it work to do something like this:
'uploader' : '<?php bloginfo('template_directo ry'); ?>/uploadify.php?MyVariabl eHere', ???
This is the way I'm reaching the file I'm on about ... it's via javascript...
<script type="text/javascript">
$(document).ready(function
$('#file_upload').uploadif
'uploader' : '<?php bloginfo('template_directo
The line above calls the file I'm on about, which is called uploadify.php ...
Would it work to do something like this:
'uploader' : '<?php bloginfo('template_directo
You can use this to see what variables are available to your script:
http://php.net/manual/en/function.get-defined-vars.php
You might also want to use this:
var_dump($current_user);
http://php.net/manual/en/function.var-dump.php