简体   繁体   English

将元素添加到注册表单的顶部-wordpress,php,functions.php

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

I am using the following code to add a first name and last name box to my signup page in wordpress. 我正在使用以下代码在Wordpress的注册页面中添加名字和姓氏框。 It is added to my themes function.php file and works fine, HOWEVER I'd like to add them to the top of the existing form - not the bottom. 它已添加到我的主题function.php文件中,并且可以正常工作,但是我想将它们添加到现有表单的顶部,而不是底部。 Can anyone help me with a way to do this please. 谁能帮我解决这个问题。

/*------- 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;
}

If you use the hook signup_hidden_fields * , they show at the top, but $errors is not available: 如果使用钩子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" />';
}

* Inspect that file for other do_action s, there are a few others but it all depends on your setup. * 检查该文件是否有其他do_action ,还有其他一些,但这全都取决于您的设置。

Check this too . 也检查一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM