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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么