How to get the user id logged in wordpress

3

I'm doing a php script and I need to get the user id, I have this code:

<?php
    include_once 'wp-load.php';
    echo get_current_user_id(); 
?>

But it returns 0.

It's clear that the script I'm doing outside the Wordpress environment, that is, it's an individual php file, that's why I do the:

include_once 'wp-load.php';

I must also clarify that in localhost the script works fine, but when I upload it to my hosting (instance ec2) it returns 0. Does anyone know what I'm doing wrong ?. Thanks!

    
asked by Patricio 04.09.2018 в 02:40
source

4 answers

1

Actually, the file you want to load is wp-load.php

require_once($_SERVER['DOCUMENT_ROOT'] . "/wp-load.php");

Then you probably need to get the current user before you can see your ID:

$current_user = wp_get_current_user();
    
answered by 04.09.2018 в 02:47
0

You can get that information with wp_get_current_user() . As indicated by the Wordpress documentation the WP_User object has several properties that you can get as the username , the slug, the email, etc.

You can use a function that verifies that the user is logged in Wordpress in the following way:

function usuario_registrado() {
  $user = wp_get_current_user();
  return $user->exists();
}

And get the user ID like this:

function usuario_id() {
  $usuario = wp_get_current_user();
  return $usuario->ID;
}

Within the logic of your template you can use the first function to handle the case that the user does not have a session started.

if ( usuario_registrado() ) {
  $id = usuario_id();
}
    
answered by 04.09.2018 в 03:24
0

Through the get_current_user_id() function we can obtain the ID of the user logged directly, without the need to completely manipulate the user Wordpress object.

The problem that does not work ( has been said in the chat ) is because the OP I was running the code outside of the Wordpress environment .

To be inside the Wordpress environment, this would suffice:

echo get_current_user_id();

But, not being in the WP environment, we must include the wp-load file, which will allow us to use the different Wordpress functions.

For example:

define( 'WP_USE_THEMES', false );
require ('wp-load.php');  //poner la ruta correcta donde se encuentra el archivo
echo get_current_user_id();

In the first line, what we are indicating is that it does not load the functions of the templates (which also loads wp-load ). If you also need those features, you can delete that line in the code.

For more details you can check the answers to the question: What is the correct way to use WordPress functions outside WordPress files? in the Wordpress area of StackExchange.

    
answered by 04.09.2018 в 04:41
0

I found the problem ... yesterday I wanted to delete a file by ftp and it would not let me ... it said that I did not have permissions ... then through ssh I changed the permissions of the html folder to 777 ... and that was the problem ... now probe change them to 755 and voila!, it worked again get_current_user_id (). Thank you very much for your help!

    
answered by 05.09.2018 в 21:30