使用短代码在 WooCommerce 我的帐户中显示一些用户数据

问题描述 投票:0回答:1

这个想法是创建一个

shortcode
,我可以通过它获得当前的
users ID
Username
Email
First
Last name
,并使用
global $wpdb
进行额外的控制 - 检查是否还有其他与当前登录用户同名的用户。

后来,我想通过添加注册时间和日期来扩展此功能,但这是另一个问题。我以为我做的事情是正确的,但是当使用我的子主题的

functions.php
文件运行代码时,屏幕变白,显示“
There's been a critical issue
”。

这是我试图使其工作的代码:

add_shortcode( 'ud_account_info', 'ud_account_info' );
function ud_account_info() {
    
    // get access to the DB and WC (is that needed?)
    global $wpdb;
    global $woocommerce;
    
    // get current user
    $current_user = wp_get_current_user();
    
    // User ID
    $current_user_id = $current_user->ID;
    
    // user email (can be the same as the User ID in terms of WC login)
    $current_user_email = $current_user->user_email;

    // get user username
    $current_user_login = $current_user->user_login;
    
    // get users first and last name
    $current_user_name = array(
        'first' => $current_user->first_name,
        'last' => $current_user->last_name
    );
    
    // according to the data we just collected, check the DB if there are other users with the same FIRST name as the currently logged in
    $current_user_first_name_control = $wpdb->get_results( " SELECT user_id FROM $wpdb->usermeta WHERE 
    meta_key = 'first_name' AND meta_value = $current_user->first_name " );
    
    // check if there are other users with the same first name as the one currently logged in
    if ( $current_user_first_name_control ) { foreach ( $current_user_first_name_control as $current_user_first_name ) {
    
    } } else { }
    
    // return it ALL and inform the user by hooking into "woocommerce_account_dashboard" later on by using this shortcode
    return 'Your account has the following ' . $current_user_name . ' as its username and is registered using ' . $current_user_email . 
    ' email which is connected to the following ' . $current_user_login . ' username and your account ID is: '$current_user_id . 
    '. There are ' . $current_user_first_name . ' other users with the same first name as you.';
}

挂钩到要显示此信息的“

My account
”仪表板。

add_action( 'woocommerce_account_dashboard', 'user_control_information', 99 );
function user_control_information() { ?>
    
    // turn off PHP to make it easier to style (according to me)
    <div style="margin-top:20px"><?php do_shortcode( ' [ud_account_info] ' ) ?></div>

<?php
}
php mysql wordpress woocommerce shortcode
1个回答
1
投票

您的代码中有一些错误。

我已将 SQL 查询代码移至单独的安全函数中,并进行了一些更改,以便直接对具有相同名字的其他用户进行计数。

我也简化了你的代码。请注意,用户登录名是用户名(因为您在同一文本中使用了两次)。

代码:

function get_other_users_count_from_first_name( $current_user ) {
    global $wpdb;
    return (int) $wpdb->get_var( $wpdb->prepare( "
        SELECT COUNT(user_id) FROM {$wpdb->prefix}usermeta 
        WHERE meta_key = 'first_name' AND meta_value LIKE '%s'
        AND user_id != %d
    ", $current_user->first_name, $current_user->ID ) );
}

add_shortcode( 'ud_account_info', 'ud_account_info' );
function ud_account_info() {
    global $current_user;

    if( ! $current_user ) {
        return;
    }
    // Check DB for other users with the same FIRST name
    $users_count = get_other_users_count_from_first_name( $current_user );

    // Text to be displayed
    $output_html = sprintf( __('Your account is registered using %s as username, %s as email and your account user ID is: %d.'), 
    $current_user->user_login, $current_user->user_email, $current_user->ID );
    
    // If there are other users with the same first name add the following
    if ( $users_count > 0 ) { 
        $output_html .= ' <br>' . sprintf( __('There are other users that have "%s" as first name.'), $current_user->first_name );
    }
    return $output_html;
}

add_action( 'woocommerce_account_dashboard', 'user_control_information', 99 );
function user_control_information() { 
    $style = 'margin-top:20px;';
    printf('<div style="%s">%s</div>', $style, do_shortcode("[ud_account_info]"));
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并工作。

© www.soinside.com 2019 - 2024. All rights reserved.