douchushao7799 2017-06-12 07: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 08: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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀