3
if(strpos($query, '%get_email%') !== false){
        $query = str_replace('%get_email%',  get_the_author_meta( 'user_email' ), $query);
    }

Okay so am trying to make a MySQL query and in the plugin I am trying to add a shortcode %get_email% but anyway its not returning any email in the actual query.

Like select* from tablename where email='%get_email%';

Any idea on how to do that?

Here what is working for id -

    if(strpos($query, '%CURRENT_USER_ID%') !== false){
        $query = str_replace('%CURRENT_USER_ID%', get_current_user_id(), $query);
    }
1
  • What is $query? What is the context? I am unsure what you are doing.
    – s_ha_dum
    May 22, 2015 at 21:52

4 Answers 4

5

I will approach like this:

global $current_user;
get_currentuserinfo();

$email = (string) $current_user->user_email;
var_dump($email);

You just called got the email from the current_user array and cast it into string :) that's it

2
  • You sould use wp_get_current_user not global $current_user.
    – amiad
    Nov 14, 2021 at 10:47
  • If the user is in the process of changing their email address but their new email address hasn't been verified yet, does this return the old or the new email address?
    – Flimm
    Nov 21, 2022 at 8:54
3

Download/Install the PHPCode Snippets WordPress Plugin, Then just create a shortcode with this content (exactly as follows);

<?php
$current_user = wp_get_current_user();
echo $current_user->user_email;
3
  • That won't control where the code gets run on the page, though, will it? You probably want to do something better than echo, e.g. define a shortcode like some of the other answers do.
    – Rup
    Jul 23, 2021 at 12:33
  • I don't understand why you are trying to introduce complications into a rather simple situation. Jul 23, 2021 at 19:03
  • You should never use echo or print in a shortcode. It will be printed out immediately, not where the shortcode is used.
    – fuxia
    Jul 31, 2021 at 10:08
2

If you just need to "Get Current user email as a return string value" then you can simply do this by using following function

// Add this function in your functions.php or in your plugin
function mm_get_current_user_email(){
    global $current_user;
    get_currentuserinfo();

    $email = $current_user->user_email; 

    return $email;  
} 
add_shortcode( 'get_email', 'mm_get_current_user_email');

Then you can get current user email as string where ever you want by simply using [get_email] or <?php echo do_shortcode( '[get_email]' ); ?>

For more on get_currentuserinfo() visit WordPress Codex.

1

Mile response is ok but there is no need to call an extra function. you can just use like this :

global $current_user;

$email = $current_user->user_email;
1
  • a lot changed since 2015 :) LoL ` // Get the current user's info $current_user = wp_get_current_user(); if ( !($current_user instanceof WP_User) ) return; ` Nov 15, 2021 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.