Link to home
Create AccountLog in
Avatar of error77
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?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

This is a data-dependent problem and we cannot see your data, so here are some strategies you might try to see the data.

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
Avatar of error77
error77

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:

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;

Open in new window

Is the main site powered by WordPress? Or are you trying to just get the user info loaded into a non WP website?
Avatar of error77

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.
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.
Avatar of error77

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 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/
Avatar of error77

ASKER

Sorry, that didn't work for me.

Other people comented the same problem too.
ASKER CERTIFIED SOLUTION
Avatar of jeremyjared74
jeremyjared74
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of error77

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_name'];
      $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['extension'],$fileTypes)) {
            move_uploaded_file($tempFile,$targetFile);
            echo '1';
      } else {
            echo 'Invalid file type.';
      }
}
?>

I need to change the targetFolder to:



..so

$targetFolder = '/uploads/'.$current_user->user_login); ?>.'/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
Avatar of error77

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?

Avatar of error77

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').uploadify({
            'uploader' : '<?php bloginfo('template_directory'); ?>/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_directory'); ?>/uploadify.php?MyVariableHere',   ???