WP Job Manager – 在职位提交表单上添加额外的注册字段

huangapple go评论56阅读模式
英文:

WP Job Manager - Additional signup fields on the job submission form

问题

我正在使用WP Job Manager创建一个网站。用户目前可以在发布新工作时注册,只需输入其电子邮件地址和用户名。一旦工作提交成功,就会创建帐户,并将用户带到预览页面,然后再发布他们的工作。

我想在这个注册表单中添加两个额外的字段:名字和姓氏。当提交时,这些字段应填充用户的元数据 first_namelast_name

我有这个将添加字段的函数:

add_filter( 'wpjm_get_registration_fields', 'add_first_last_name_fields', 999999 );

function add_first_last_name_fields( $reg_fields ){
    if( empty( $reg_fields ) ){
        return [];
    }

    $generate_username_from_email      = job_manager_generate_username_from_email();
    $use_standard_password_setup_email = wpjm_use_standard_password_setup_email();
    $account_required                  = job_manager_user_requires_account();

    $registration_fields = [];
    if ( job_manager_enable_registration() ) {
        $registration_fields['create_account_email'] = [
            'type'        => 'text',
            'label'       => esc_html__( 'Your email', 'wp-job-manager' ),
            'placeholder' => __( 'you@yourdomain.com', 'wp-job-manager' ),
            'required'    => $account_required,
            // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Just used to populate value when validation failed.
            'value'       => isset( $_POST['create_account_email'] ) ? sanitize_text_field( wp_unslash( $_POST['create_account_email'] ) ) : '',
        ];

        if ( ! $generate_username_from_email ) {
            $registration_fields['create_account_username'] = [
                'type'     => 'text',
                'label'    => esc_html__( 'Username', 'wp-job-manager' ),
                'required' => $account_required,
                // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Just used to populate value when validation failed.
                'value'    => isset( $_POST['create_account_username'] ) ? sanitize_text_field( wp_unslash( $_POST['create_account_username'] ) ) : '',
            ];
        }

        $registration_fields['first_name'] = [
            'type'        => 'text',
            'label'       => esc_html__( 'First Name / Organisation', 'wp-job-manager' ),
            'placeholder' => __( 'First Name', 'wp-job-manager' ),
            'required'    => $account_required,
            // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Just used to populate value when validation failed.
            'value'       => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '',
        ];

        $registration_fields['last_name'] = [
            'type'        => 'text',
            'label'       => esc_html__( 'Last Name', 'wp-job-manager' ),
            'placeholder' => __( 'Last Name', 'wp-job-manager' ),
            'required'    => $account_required,
            // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Just used to populate value when validation failed.
            'value'       => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '',
        ];

        if ( ! $use_standard_password_setup_email ) {
            $registration_fields['create_account_password'] = [
                'type'         => 'password',
                'label'        => esc_html__( 'Password', 'wp-job-manager' ),
                'autocomplete' => false,
                'required'     => $account_required,
            ];
            $password_hint                                  = wpjm_get_password_rules_hint();
            if ( $password_hint ) {
                $registration_fields['create_account_password']['description'] = $password_hint;
            }
        }
    }

    return $registration_fields;
}

然后,根据我理解的情况,我需要获取输入字段的值,并将它们分配给用户元数据。

到目前为止,我尝试过以下代码:

add_filter ('job_manager_create_account_data', 'create_additional_registration_fields');
function create_additional_registration_fields($new_user) {
  $new_user = [
    'user_login' => $registration_fields['create_account_username'],
    'user_email' => $registration_fields['create_account_email'],
    'first_name' => $registration_fields['first_name'],
    'last_name' => $registration_fields['last_name'],
  ];

  return $new_user;
}

但这给我报错:Cannot create a user with an empty login name.

我对理解WordPress函数相对较新。任何帮助都将不胜感激。

英文:

I'm creating a site with WP Job Manager. A user can currently sign up at the point of posting a new job by entering their email address and a username. Once the job has been submitted, the account is created and the user is taken to the preview page before publishing their job.

I would like to add two additional fields to this signup form: First Name and Last Name. When submitted, these fields should populate the user's meta first_name and last_name.

I have this function that will add the fields:

add_filter( 'wpjm_get_registration_fields', 'add_first_last_name_fields', 999999 );

function add_first_last_name_fields( $reg_fields ){
if( empty( $reg_fields ) ){
return [];
}
$generate_username_from_email      = job_manager_generate_username_from_email();
$use_standard_password_setup_email = wpjm_use_standard_password_setup_email();
$account_required                  = job_manager_user_requires_account();
$registration_fields = [];
if ( job_manager_enable_registration() ) {
$registration_fields['create_account_email'] = [
'type'        => 'text',
'label'       => esc_html__( 'Your email', 'wp-job-manager' ),
'placeholder' => __( 'you@yourdomain.com', 'wp-job-manager' ),
'required'    => $account_required,
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Just used to populate value when validation failed.
'value'       => isset( $_POST['create_account_email'] ) ? sanitize_text_field( wp_unslash( $_POST['create_account_email'] ) ) : '',
];
if ( ! $generate_username_from_email ) {
$registration_fields['create_account_username'] = [
'type'     => 'text',
'label'    => esc_html__( 'Username', 'wp-job-manager' ),
'required' => $account_required,
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Just used to populate value when validation failed.
'value'    => isset( $_POST['create_account_username'] ) ? sanitize_text_field( wp_unslash( $_POST['create_account_username'] ) ) : '',
];
}
$registration_fields['first_name'] = [
'type'        => 'text',
'label'       => esc_html__( 'First Name / Organisation', 'wp-job-manager' ),
'placeholder' => __( 'First Name', 'wp-job-manager' ),
'required'    => $account_required,
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Just used to populate value when validation failed.
'value'       => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '',
];
$registration_fields['last_name'] = [
'type'        => 'text',
'label'       => esc_html__( 'Last Name', 'wp-job-manager' ),
'placeholder' => __( 'Last Name', 'wp-job-manager' ),
'required'    => $account_required,
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Just used to populate value when validation failed.
'value'       => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '',
];
if ( ! $use_standard_password_setup_email ) {
$registration_fields['create_account_password'] = [
'type'         => 'password',
'label'        => esc_html__( 'Password', 'wp-job-manager' ),
'autocomplete' => false,
'required'     => $account_required,
];
$password_hint                                  = wpjm_get_password_rules_hint();
if ( $password_hint ) {
$registration_fields['create_account_password']['description'] = $password_hint;
}
}
}
return $registration_fields;
}

Then as far as I understand, I need to get the value of the inputted fields, and assign them to the user meta.

So far I have tried:

add_filter ('job_manager_create_account_data', 'create_additional_registration_fields');
function create_additional_registration_fields($new_user) {
$new_user = [
'user_login' => $registration_fields['create_account_username'],
'user_email' => $registration_fields['create_account_email'],
'first_name' => $registration_fields['first_name'],
'last_name' => $registration_fields['last_name'],
];
return $new_user;
}

But this throws me an error: Cannot create a user with an empty login name.

I'm relatively new to understanding WordPress functions. Any help would be appreciated.

答案1

得分: 0

创建一个函数来添加自定义字段的方法是这样的:

add_filter( 'wpjm_get_registration_fields', 'job_manager_add_extra_fields', 10, 1 );

function job_manager_add_extra_fields( $registration_fields ){

	if ( job_manager_enable_registration() ) {

    $registration_fields['first_name'] = [
			'type'        => 'text',
			'label'       => esc_html__( 'First Name', 'wp-job-manager' ),
			'required'    => 1,
			'value'       => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '',
		];

    $registration_fields['last_name'] = [
			'type'        => 'text',
			'label'       => esc_html__( 'Last name', 'wp-job-manager' ),
			'required'    => 1,
			'value'       => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '',
		];
		
	}

	return $registration_fields;
}

接下来,您需要在职位提交时保存它们:

add_filter( 'job_manager_create_account_data', 'job_manager_save_extra_fields', 10, 1 );

function job_manager_save_extra_fields( $new_user ){
  
	$new_user['first_name'] = isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '';
	$new_user['last_name'] = isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '';

	return $new_user;
}
英文:

The way to do this is by creating a function that adds the custom fields:

add_filter( 'wpjm_get_registration_fields', 'job_manager_add_extra_fields', 10, 1 );
function job_manager_add_extra_fields( $registration_fields ){
if ( job_manager_enable_registration() ) {
$registration_fields['first_name'] = [
'type'        => 'text',
'label'       => esc_html__( 'First Name', 'wp-job-manager' ),
'required'    => 1,
'value'       => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '',
];
$registration_fields['last_name'] = [
'type'        => 'text',
'label'       => esc_html__( 'Last name', 'wp-job-manager' ),
'required'    => 1,
'value'       => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '',
];
}
return $registration_fields;
}

Next you need to save them upon job submission:

add_filter( 'job_manager_create_account_data', 'job_manager_save_extra_fields', 10, 1 );
function job_manager_save_extra_fields( $new_user ){
$new_user['first_name'] = isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '';
$new_user['last_name'] = isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '';
return $new_user;
}

huangapple
  • 本文由 发表于 2023年6月18日 23:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定