admin管理员组

文章数量:1277901

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);
    }
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);
    }
Share Improve this question edited Jun 2, 2021 at 16:53 Viktor Borítás 3042 silver badges11 bronze badges asked May 22, 2015 at 20:22 RoyRoy 311 gold badge1 silver badge2 bronze badges 1
  • What is $query? What is the context? I am unsure what you are doing. – s_ha_dum Commented May 22, 2015 at 21:52
Add a comment  | 

4 Answers 4

Reset to default 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

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;

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.

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;

本文标签: pluginsGet Current user email as a return string value