get_current_user_id () renvoie 0?
-
-
Qu'imprimeglobalement $ current_userWhat does global `$current_user` prints?
- 2
- 2014-10-04
- Sisir
-
Oùest le code que vous utilisezpour appelerget_current_user_id ()?Quandexécutez-vous ce code?Est-ce avant ou après l'actioninit?Where is the code you're using to call get_current_user_id()? When are you running this code? Is it before or after the init action?
- 0
- 2014-10-04
- Otto
-
Il affiche ** WP_User Object ([data]=> [ID]=> 0 [caps]=> Array () [cap_key]=> [roles]=> Array () [allcaps]=> Array () [filter]=>) ** @SisirIt displays **WP_User Object ( [data] => [ID] => 0 [caps] => Array ( ) [cap_key] => [roles] => Array ( ) [allcaps] => Array ( ) [filter] => )** @Sisir
- 0
- 2014-10-06
- PHP Learner
-
Votre questionprête à confusion.Essayez-vous d'obtenir l'utilisateur actuellement connecté queje dans leback-end.Quelleest la relation avec la deuxièmepartie de votre code?Avez-vousbesoin d'afficher l'utilisateur à côté dunom d'utilisateur dans lebackend de l'écran d'administration "Utilisateurs"?Veuillez clarifier dans un [modifier]Your question is confusing. Are you trying to get the current logged in user I'd in the back end. What is the relationship to the second part of your code? Do you need to display the user I'd next to the username in the backend in the "Users" admin screen? Please clarify in an [edit]
- 0
- 2014-10-06
- Pieter Goosen
-
Enfait,j'aiessayé d'obtenir l'ID utilisateur actuellement connecté.La deuxièmepartie du code consiste à ajouter la colonne ** ID ** dans lebackend.Maintenant,j'obtiens l'ID utilisateurpar le code ci-dessus lui-même @PieterGoosen.Merci d'avoirpassétontemps d'or avecmoiActually I tried to get the currently logged in user id.The second part of the code is for adding the **ID** column in back-end.Now I'm getting the user id by the above code itself @PieterGoosen. Thanks for spending your golden time with me
- 0
- 2014-10-06
- PHP Learner
-
J'aifait uneerreuridiote,c'est-à-dire queje neme suispas connecté,alors comment devrait-il renvoyer l'identifiant de l'utilisateur Unefois queje me suis connecté,j'obtiens l'identifiant de l'utilisateur.Mercipour @Justin BellI have made a silly mistake.That is I did not get logged in,then how should it returns the user id.After i have logged in,I'm getting the user id. Thanks for @Justin Bell
- 0
- 2014-10-06
- PHP Learner
-
4 réponses
- votes
-
- 2014-10-04
En utilisant lesinformations wp_get_current_user () dans le Codex,lafonction utilise le
$current_user
et sinécessaire l'initialise avant utilisation. Comme d'autres l'ontindiqué,get_current_user_id()
utilise cettefonction sur lebackend.Pensez à /wp-includes/user.php ,lignes 323-327 (la définition defonctionpour ce code). À lafin,la valeur de retourest
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
— ce code renverra0
si l'utilisateur connectéestindisponible d'unemanière ou d'une autre.Utilisezinit outoute autre action ultérieurepour appeler cettefonction. Appel celaen dehors d'une actionpeutentraîner desproblèmes. Voir # 14024pourplus de détails.
Celaprovient de la documentation de
wp_get_current_user
. Si vous utilisiez ce code dans unmodèle,vouspouviez être assuré queinit
avait déjà été appelé. Cependant,si vous récupérez simplement au hasard cesinformations utilisateur avantinit
l'actionest appelée,vousn'obtiendrezpas un utilisateur actuel. Celaexpliquepourquoi vous avez obtenu l'ID d'utilisateur lors de l'ajout de ces actions dansfunctions.php (car ces actions ont lieu aprèsinit
),alors qu'avec votre code d'origine,cen'estpas clair lorsque vous l'appelez.Veuillez vous reporter à lapage Plugin API pour uneidéegénérale de l'ordre dans lequel ces différents les actions sont appelées.
Going by wp_get_current_user() information in the Codex, the function utilizes the global
$current_user
object and if necessary initializes it before use. As others have stated,get_current_user_id()
uses this function on the backend.Consider /wp-includes/user.php, lines 323-327 (the function definition for this code). At the tail end, the return value is
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
— this code will return0
if the logged-in user is somehow unavailable.Use the init or any subsequent action to call this function. Calling it outside of an action can lead to troubles. See #14024 for details.
This comes from the documentation for
wp_get_current_user
. If you were using this code within a template, you could be assured thatinit
had already been called. However, if you just randomly grab for that user information beforeinit
action is called, you will not get a current user. This explains why you got the User's ID when adding those actions into functions.php (as those actions take place afterinit
), whereas with your original code it's not clear when you invoke that.Please refer to the Plugin API page for a general idea of the order in which these various actions are invoked.
-
Oui,j'aienregistré lefichier ** wp-includes/user.php **,il contient le code de code que vous avezmentionné.Est-ce la raison du renvoi de l'ID utilisateur 0?Yes I have checked in **wp-includes/user.php** file, it has the code code which you have mentioned. Is it be the reason for returning the user id 0?
- 1
- 2014-10-06
- PHP Learner
-
Ilpeut également êtreintéressant dementionner que si l'utilisateurn'estpas dutout connecté,lafonction renvoie également `0` (oufalse)It might also be worth mentioning that if the user is not logged in at all the function also returns `0` (or false)
- 0
- 2015-04-22
- Bysander
-
- 2014-10-24
Utiliseztoujours
get_current_user_id();
sous l'actioninit
.Voici l'exemple:
add_action('init', 'myFunction'); function myFunction(){ $user_ID= get_current_user_id(); echo"User number $user_ID is loggedin"; }
Always use
get_current_user_id();
underinit
action.Following is the example:
add_action('init', 'myFunction'); function myFunction(){ $user_ID= get_current_user_id(); echo"User number $user_ID is loggedin"; }
-
Où * sous * signifie l'action `init`,outoute action appelée aprèselle,non?Where *under* means `init` action, or any action being called after it, right?
- 0
- 2020-02-28
- jgangso
-
- 2015-04-22
Commementionnépar les autres: si vous appelez lafonctiontroptôt,elle renverra la valeur
0
Unbonmoyen de vérifier s'ilest "troptôt" ounonest cetype de vérification:
// Do NOT check for action 'set_current_user', but for 'init'!! if ( ! did_action( 'init' ) ) { _doing_it_wrong( __FUNCTION__, 'get_current_user_id() called before the init hook', null ); } $user_id = get_current_user_id();
La raisonpour laquellenousn'utilisonspas
did_action('set_current_user')
est:Si un autre code/autreplugin appelé
get_current_user_id()
troptôt,il déclenchera l'exécution du hookset_current_user
.Cependant,les données de l'utilisateur actuelne sont pas correctes à ce stade,donc sefier à ce hook d'actionn'estpas unebonneidée - seulement lorsqueinit
estexécuté,nouspouvons être sûrslebon utilisateur!As mentioned by the others: If you call the function too early it will return value
0
A good way to check if it's "too early" or not is this kind of checking:
// Do NOT check for action 'set_current_user', but for 'init'!! if ( ! did_action( 'init' ) ) { _doing_it_wrong( __FUNCTION__, 'get_current_user_id() called before the init hook', null ); } $user_id = get_current_user_id();
The reason why we do not use
did_action('set_current_user')
is:If some other code/other plugin called
get_current_user_id()
too early, it will trigger the hookset_current_user
to run. However, the current-user data is not correct at this point, so relying on that action hook is no good idea - only wheninit
is executed we can be sure to have the correct user! -
- 2016-06-07
Pour les versions wordpress> 3.4:
Utilisez:
$current_user_id= get_current_user_id();
Pour les versions wordpress & lt;3.4:
La documentationindique que si la versionestinférieure à 3.4,utilisez-la avecinit:
add_action('init', 'get_your_current_user_id'); function get_your_current_user_id(){ $your_current_user_id= get_current_user_id(); //do something here with it }
Vouspouvez vérifier la documentation: wp_get_current_user
For wordpress versions > 3.4:
Use:
$current_user_id= get_current_user_id();
For wordpress versions < 3.4:
Documentation says if the version is less than 3.4 use it with init:
add_action('init', 'get_your_current_user_id'); function get_your_current_user_id(){ $your_current_user_id= get_current_user_id(); //do something here with it }
You can check documention: wp_get_current_user
J'essaie d'obtenir l'identifiant de l'utilisateur actuellement connecté dans WordPress.J'ai utilisé le code suivant:
Le code ci-dessus renvoie le résultat vide comme: