dqunzip3183 2019-03-29 01:48
浏览 84

Wordpress注册表获取用户名字段的值

I am trying to populate the referral_code field with REF-{{USERNAME}} which is the value entered into the default wordpress username registration field:

Wordpress Default Username Field:

<p>
    <label for="user_login">Username<br>
    <input type="text" name="user_login" id="user_login" class="input" value="" size="20" autocapitalize="off"></label>
</p>

My Custom Function To Add Extra Fields

<?php 
function gw_registration_form() {
    $full_name = ( ! empty( $_POST['full_name'] ) ) ? sanitize_text_field( $_POST['full_name'] ) : '';
    $company_name = ( ! empty( $_POST['company_name'] ) ) ? sanitize_text_field( $_POST['company_name'] ) : '';
    $telephone = ( ! empty( $_POST['telephone'] ) ) ? sanitize_text_field( $_POST['telephone'] ) : '';
    $referral_code = ( ! empty( $_POST['referral_code'] ) ) ? sanitize_text_field( $_POST['referral_code'] ) : '';?>
    <p>
        <label for="full_name"><?php _e( 'Full Name', 'mydomain' ) ?><br />
        <input type="text" name="full_name" id="full_name" class="input" value="<?php echo esc_attr(  $full_name  ); ?>" size="25" /></label>
    </p>
    <p>
        <label for="company_name"><?php _e( 'Company Name', 'mydomain' ) ?><br />
        <input type="text" name="company_name" id="company_name" class="input" value="<?php echo esc_attr(  $company_name  ); ?>" size="25" /></label>
    </p>
    <p>
        <label for="telephone"><?php _e( 'Telephone Number', 'mydomain' ) ?><br />
        <input type="text" name="telephone" id="telephone" class="input" value="<?php echo esc_attr(  $telephone  ); ?>" size="25" /></label>
    </p>
    <p>
        <label for="referral_code"><?php _e( 'Your Referral Code', 'mydomain' ) ?><br />
        <input type="text" name="referral_code" id="referral_code" class="input" value="REF-<?php echo esc_attr(  $user_login  ); ?>" size="25" readonly/></label>
    </p>
    <?php
};

I've since just removed the referral code out and made it set the value on registering/saving, however, I do believe someone providing an answer to how said would be achieved in this question would be beneficial to other users.

  • 写回答

1条回答 默认 最新

  • douyue2313 2019-03-29 02:32
    关注

    There is a WP function for that: https://codex.wordpress.org/Function_Reference/wp_get_current_user

    Used like this you can get the user_login. From the Docs:

    <?php
      $current_user = wp_get_current_user();
    
      echo $current_user->user_login; /* here is your user_login */
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢