douchushao7799 2017-06-11 23:43
浏览 44
已采纳

PHP - 在不同的函数中使用函数的返回值

So I have two functions. Once returns some information and I am trying to get this to be accessible in my second function but this is currently empty/ Here is my first function code:

function get_current_user_role_custom() {
    global $wp_roles;

    $current_user = wp_get_current_user();
    $roles = $current_user->roles;
    $role = array_shift( $roles );

    return isset( $wp_roles->role_names[ $role ] ) ? translate_user_role( $wp_roles->role_names[ $role ] ) : FALSE;
}
$user_role = get_current_user_role_custom();

And the second function (which I am trying to use the $user_role variable in:

function new_customer_registered_send_email_admin() {

    //variables
    global $user_role;
    global $current_user;
    $current_user = wp_get_current_user();


    ob_start();
    do_action('woocommerce_email_header', 'New customer registered');
    $email_header = ob_get_clean();
    ob_start();
    do_action('woocommerce_email_footer');
    $email_footer = ob_get_clean();

    woocommerce_mail(
    get_bloginfo('admin_email'),
    get_bloginfo('name').' - New customer registered',
    $email_header.'<p>User Role: ' . $user_role . '</p>'.$email_footer
    );
}
add_action('new_customer_registered', 'new_customer_registered_send_email_admin');

展开全部

  • 写回答

3条回答 默认 最新

  • duanpao4172 2017-06-12 00:04
    关注

    Unless you have a really good reason why you must use global variables (which is almost never the case), do not use global variables. Instead use a parameter / an argument to hand over variables and thus their values to functions.

    Try something like this:

    // ### Define Functions ###
    function get_current_user_role_custom($wp_roles) {
        $current_user = wp_get_current_user();
        $roles = $current_user->roles;
        $role = array_shift( $roles );
    
        return isset( $wp_roles->role_names[ $role ] ) ? translate_user_role( $wp_roles->role_names[ $role ] ) : FALSE;
    }
    
    function new_customer_registered_send_email_admin($user_role) {
    
        //variables
        $current_user = wp_get_current_user();
    
        ob_start();
        do_action('woocommerce_email_header', 'New customer registered');
        $email_header = ob_get_clean();
        ob_start();
        do_action('woocommerce_email_footer');
        $email_footer = ob_get_clean();
    
        woocommerce_mail(
        get_bloginfo('admin_email'),
        get_bloginfo('name').' - New customer registered',
        $email_header.'<p>User Role: ' . $user_role . '</p>'.$email_footer
        );
    }
    
    // ### Call Functions ###
    // as I cannot see where this variable comes from, you need to modify the follwoing line yourself appropriately ;)
    $wp_roles = get_wp_roles_somehow();
    $user_role = get_current_user_role_custom($wp_roles);
    if ($user_role !== FALSE) {
        new_customer_registered_send_email_admin($user_role);
        add_action('new_customer_registered', 'new_customer_registered_send_email_admin');    
    } else {
        // handle error somehow
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部