簡體   English   中英

將元素添加到注冊表單的頂部-wordpress,php,functions.php

[英]Add elements to top of signup form - wordpress, php, functions.php

我正在使用以下代碼在Wordpress的注冊頁面中添加名字和姓氏框。 它已添加到我的主題function.php文件中,並且可以正常工作,但是我想將它們添加到現有表單的頂部,而不是底部。 誰能幫我解決這個問題。

/*------- add first name to signup form -----------*/

//0. Style the new form elements...
add_action( 'wp_head', 'full_name_signup_stylesheet' );
function full_name_signup_stylesheet() {
?>
<style type="text/css">
.mu_register #first_name,
.mu_register #last_name { width:47%; font-size: 24px; margin:5px 5px 0px 0px; display:inline-block; float:left; position:relative;}
</style>

<?php
}

//1. Add a new form element...
add_action('signup_extra_fields','full_name_register_form');
function full_name_register_form ($errors)
{
// first_name
$first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
    echo '<label for="first_name">' . __('First Name') . '</label>';
if ( $errmsg = $errors->get_error_message('first_name') ) {
    echo '<p class="error">'.$errmsg.'</p>';
}
echo '<input name="first_name" type="text" id="first_name" value="'. esc_attr($first_name) .'" maxlength="60" />';

// last_name
$last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: '';
    echo '<label for="last_name">' . __('Last Name') . '</label>';
if ( $errmsg = $errors->get_error_message('last_name') ) {
    echo '<p class="error">'.$errmsg.'</p>';
}
echo '<input name="last_name" type="text" id="last_name" value="'. esc_attr($last_name) .'" maxlength="60" />';
}

//2. Add validation. In this case, we make sure first_name is required.
add_filter('signup_user_init', 'full_name_signup_user_init', 10, 1);
function full_name_signup_user_init($arguments)
{
if ( isset( $_POST['signup_for'] ) ) {

// first_name
$errors = $arguments['errors'];
if ( empty( $_POST['first_name'] ) ) {
$errors->add( 'first_name', __('Please enter your first name.','') );
}
}

return $arguments;
}

//3. Finally, save our extra registration user meta.
add_action('add_signup_meta', 'full_name_add_signup_meta');
function full_name_add_signup_meta ($meta)
{
// first_name
if ( isset( $_POST['first_name'] ) ) {
$meta['first_name'] = $_POST['first_name'];
}

// last_name
if ( isset( $_POST['last_name'] ) ) {
$meta['last_name'] = $_POST['last_name'];
}

return $meta;
}

如果使用鈎子signup_hidden_fields * ,它們將顯示在頂部,但$errors不可用:

add_action( 'signup_hidden_fields', 'full_name_register_form' );

function full_name_register_form ()
{
    // first_name
    $first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
        echo '<label for="first_name">' . __('First Name') . '</label>';

    echo '<input name="first_name" type="text" id="first_name" value="'. esc_attr($first_name) .'" maxlength="60" />';

    // last_name
    $last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: '';
        echo '<label for="last_name">' . __('Last Name') . '</label>';

    echo '<input name="last_name" type="text" id="last_name" value="'. esc_attr($last_name) .'" maxlength="60" />';
}

* 檢查該文件是否有其他do_action ,還有其他一些,但這全都取決於您的設置。

也檢查一下

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM