【问题标题】:Add checkbox form input to backend Wordpress user information将复选框表单输入添加到后端 Wordpress 用户信息
【发布时间】:2020-08-30 05:20:48
【问题描述】:

所以我正在尝试为我的 Wordpress 网站创建一个自定义注册页面,并找到了有关如何添加自定义字段的教程。我想出了如何使整个表单正常工作,我添加了全名、姓氏和年龄字段。我只是不知道如何使复选框输入显示在该职位的后端。

所以表格是这样的,你可以在team.ifatceg.com查看。

位置是我不知道如何在后端显示的位置。剩下的我可以。

这是描述表单的两张图片以及后端用户页面中显示的内容。问题是他们在复选框中选择的内容没有显示在后端的用户页面上。

https://ibb.co/4PnRns3 https://ibb.co/hgZPz0r

这是插件:

    <?php
/*
Plugin Name: Custom Registration Fields
Plugin URI:
Description:
Version: 0.1
Author: CSSIgniter
Author URI:
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/


/**
 * Front end registration
 */

add_action( 'register_form', 'crf_registration_form' );
function crf_registration_form() {

$first_name = ! empty( $_POST['first_name '] ) ? intval( $_POST['first_name'] ) : '';

    ?>
    <p>
        <label for="first_name"><?php esc_html_e( 'First Name', 'crf' ) ?><br/>
            <input type="text"
            id="first_name"
            name="first_name"
            value="<?php echo esc_attr( $first_name ); ?>"
            class="input"
            />
        </label>
    </p>
     <?php

$last_name = ! empty( $_POST['last_name '] ) ? intval( $_POST['last_name'] ) : '';

    ?>
    <p>
        <label for="last_name"><?php esc_html_e( 'Last Name', 'crf' ) ?><br/>
            <input type="text"
            id="last_name"
            name="last_name"
            value="<?php echo esc_attr( $last_name ); ?>"
            class="input"
            />
        </label>
     <?php

$age = ! empty( $_POST['age'] ) ? intval( $_POST['age'] ) : '';

    ?>
    <p>
        <label for="age"><?php esc_html_e( 'Age', 'crf' ) ?><br/>
            <input type="number"
                   min="16"
                   max="100"
                   step="1"
                   id="age"
                   name="age"
                   value="<?php echo esc_attr( $age ); ?>"
                   class="input"
            />
        </label>
        <?php

$position = ! empty( $_POST['position '] ) ? intval( $_POST['position'] ) : '';

    ?>
    <p>
        <?php esc_html_e( 'What position are you applying for?', 'crf' ) ?><br/>
<small>The writing and moderation team is invite only so all applicants who choose those options and have not been invited will be ignored.</small><br><br>

<form action="third.php" method="get">
    <input type="checkbox" name="writer" id="position" value="writer">
    <label for="writer">Writing</label><br>
    <input type="checkbox" name="writer" id="position" value="video">
    <label for="video">Video Editor</label><br>
    <input type="checkbox" name="mod" id="position" value="moderator">
    <label for="moderator">Moderator</label><br>
</p>
<br><br>
    <?php
}

add_filter( 'registration_errors', 'crf_registration_errors', 10, 3 );
function crf_registration_errors( $errors, $sanitized_user_login, $user_email ) {

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

     if ( empty( $_POST['last_name'] ) ) {
        $errors->add( 'last_name', __( 'Please enter your last name.', 'crf' ) );
    }

    if ( empty( $_POST['age'] ) ) {
        $errors->add( 'age', __( 'Please enter your age.', 'crf' ) );
    }

    if ( ! empty( $_POST['age'] ) && intval( $_POST['age'] ) < 16 ) {
        $errors->add( 'age', __( 'You must be 16 years of age or older to apply.', 'crf' ) );
    }

    return $errors;
}
add_action( 'user_register', 'crf_user_register' );
function crf_user_register( $user_id ) {
    if ( ! empty( $_POST['first_name'] ) ) {
        update_user_meta( $user_id, 'first_name', intval( $_POST['first_name'] ) );
    }

     if ( ! empty( $_POST['last_name'] ) ) {
        update_user_meta( $user_id, 'last_name', intval( $_POST['last_name'] ) );
    }
      if ( ! empty( $_POST['age'] ) ) {
        update_user_meta( $user_id, 'age', intval( $_POST['age'] ) );
    }
     if ( ! empty( $_POST['position'] ) ) {
        update_user_meta( $user_id, 'position', intval( $_POST['position'] ) );
    }
}
/**
 * Back end registration
 */

add_action( 'user_new_form', 'crf_admin_registration_form' );
function crf_admin_registration_form( $operation ) {
    if ( 'add-new-user' !== $operation ) {
        // $operation may also be 'add-existing-user'
        return;
    }

$first_name = ! empty( $_POST['first_name '] ) ? intval( $_POST['first_name'] ) : '';

$last_name = ! empty( $_POST['last_name '] ) ? intval( $_POST['last_name'] ) : '';

$age = ! empty( $_POST['age'] ) ? intval( $_POST['age'] ) : ''; 

$position = ! empty( $_POST['position '] ) ? intval( $_POST['position'] ) : '';

    ?>
    <h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3>

    <table class="form-table">
        <tr> 
           <th><label for="first_name"><?php esc_html_e( 'First Name', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
            <td>
                <input type="text"
                 id="first_name"
            name="first_name"
            value="<?php echo esc_attr( $first_name ); ?>"
            class="input"
                  />
            </td>

<th><label for="last_name"><?php esc_html_e( 'Last Name', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
            <td>
                <input type="text"
                 id="last_name"
            name="last_name"
            value="<?php echo esc_attr( $last_name ); ?>"
            class="input"
                  />
            </td>

            <th><label for="age"><?php esc_html_e( 'Age', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
            <td>
                <label for="age"><?php esc_html_e( 'Age', 'crf' ) ?><br/>
            <input type="number"
                   min="16"
                   max="100"
                   step="1"
                   id="age"
                   name="age"
                   value="<?php echo esc_attr( $age ); ?>"
                   class="input"
            />
            </td>

            <th><label for="position"><?php esc_html_e( 'Position', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
            <td>
                <input type="checkbox" name="position1" value="<?php echo esc_attr( $writer ); ?>">
  <label for="position1">Writing</label><br>
  <input type="checkbox" name="position2" value="<?php echo esc_attr( $video ); ?>">
  <label for="position2">Video Editing</label><br>
  <input type="checkbox" name="position3" value="<?php echo esc_attr( $moderator ); ?>">
  <label for="position3">Moderator</label>
            </td>
</tr>
</table>
<?php
}
add_action( 'user_profile_update_errors', 'crf_user_profile_update_errors', 10, 3 );
function crf_user_profile_update_errors( $errors, $update, $user ) {
    if ( $update ) {
        return;
    }

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

     if ( empty( $_POST['last_name'] ) ) {
        $errors->add( 'last_name', __( 'Please enter your last name.', 'crf' ) );
    }

    if ( empty( $_POST['age'] ) ) {
        $errors->add( 'age', __( 'Please enter your age.', 'crf' ) );
    }

    if ( ! empty( $_POST['age'] ) && intval( $_POST['age'] ) < 16 ) {
        $errors->add( 'age', __( 'You must be 16 years of age or older to apply.', 'crf' ) );
    }
}
add_action( 'edit_user_created_user', 'crf_user_register' );
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );

function crf_show_extra_profile_fields( $user ) {
    ?>
    <h3><?php esc_html_e( 'Application Information', 'crf' ); ?></h3>

    <table class="form-table">
        <tr>
                  <th><label for="age"><?php esc_html_e( 'Age', 'crf' ); ?></label></th>
            <td><?php echo esc_html( get_the_author_meta( 'age', $user->ID ) ); ?></td>
                <th><label for="position"><?php esc_html_e( 'Position', 'crf' ); ?></label></th>
            <td><?php echo esc_html( get_the_author_meta( 'position', $user->ID ) ); ?></td>
        </tr>
    </table>
    <?php
}

谁能帮我弄清楚如何做到这一点?

【问题讨论】:

  • 当你说你不能让它显示在后端时,你的意思是你看不到用户申请的职位吗?或者你有一个单独的表单,你在后端控制并为用户分配一个位置,你不能让它工作?
  • 我更新了我的主帖,谢谢你的问题。这意味着在前端有注册表格。然后在用户信息中,我有一个字段显示他们在注册时输入的内容。 ibb.co/4PnRns3ibb.co/hgZPz0r
  • 知道了,您确认他们的数据已存储在职位部分的数据库中吗?
  • 我想我知道去哪里查,但也不知道去哪里查?
  • phpmyadmin,找到您的数据库并打开 usermeta 表并在某处查找“位置”,然后让我知道它的存储方式。

标签: php html wordpress plugins


【解决方案1】:

在这里,我为你重写了很多代码。试试这个:

<?php
/*
Plugin Name: Custom Registration Fields
Plugin URI:
Description:
Version: 0.1
Author: CSSIgniter
Author URI:
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/


/**
 * Front end registration
 */

add_action( 'register_form', 'crf_registration_form' );
function crf_registration_form() {

$first_name = ! empty( $_POST['first_name '] ) ? intval( $_POST['first_name'] ) : '';

    ?>
    <p>
        <label for="first_name"><?php esc_html_e( 'First Name', 'crf' ) ?><br/>
            <input type="text"
            id="first_name"
            name="first_name"
            value="<?php echo esc_attr( $first_name ); ?>"
            class="input"
            />
        </label>
    </p>
     <?php

$last_name = ! empty( $_POST['last_name '] ) ? intval( $_POST['last_name'] ) : '';

    ?>
    <p>
        <label for="last_name"><?php esc_html_e( 'Last Name', 'crf' ) ?><br/>
            <input type="text"
            id="last_name"
            name="last_name"
            value="<?php echo esc_attr( $last_name ); ?>"
            class="input"
            />
        </label>
     <?php

$age = ! empty( $_POST['age'] ) ? intval( $_POST['age'] ) : '';

    ?>
    <p>
        <label for="age"><?php esc_html_e( 'Age', 'crf' ) ?><br/>
            <input type="number"
                   min="16"
                   max="100"
                   step="1"
                   id="age"
                   name="age"
                   value="<?php echo esc_attr( $age ); ?>"
                   class="input"
            />
        </label>
        <?php

$position = ! empty( $_POST['position '] ) ? intval( $_POST['position'] ) : '';

    ?>
    <p>
        <?php esc_html_e( 'What position are you applying for?', 'crf' ) ?><br/>
<small>The writing and moderation team is invite only so all applicants who choose those options and have not been invited will be ignored.</small><br><br>

<form action="third.php" method="get">
    <input type="checkbox" name="writer" id="position" value="writer">
    <label for="writer">Writing</label><br>
    <input type="checkbox" name="video" id="position" value="video">
    <label for="video">Video Editor</label><br>
    <input type="checkbox" name="mod" id="position" value="moderator">
    <label for="moderator">Moderator</label><br>
</p>
<br><br>
    <?php
}

add_filter( 'registration_errors', 'crf_registration_errors', 10, 3 );
function crf_registration_errors( $errors, $sanitized_user_login, $user_email ) {

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

     if ( empty( $_POST['last_name'] ) ) {
        $errors->add( 'last_name', __( 'Please enter your last name.', 'crf' ) );
    }

    if ( empty( $_POST['age'] ) ) {
        $errors->add( 'age', __( 'Please enter your age.', 'crf' ) );
    }

    if ( ! empty( $_POST['age'] ) && intval( $_POST['age'] ) < 16 ) {
        $errors->add( 'age', __( 'You must be 16 years of age or older to apply.', 'crf' ) );
    }

    return $errors;
}
add_action( 'user_register', 'crf_user_register' );
function crf_user_register( $user_id ) {
    if ( ! empty( $_POST['first_name'] ) ) {
        update_user_meta( $user_id, 'first_name', intval( $_POST['first_name'] ) );
    }

     if ( ! empty( $_POST['last_name'] ) ) {
        update_user_meta( $user_id, 'last_name', intval( $_POST['last_name'] ) );
    }
      if ( ! empty( $_POST['age'] ) ) {
        update_user_meta( $user_id, 'age', intval( $_POST['age'] ) );
    }
     if ( ! empty( $_POST['position'] ) ) {
        update_user_meta( $user_id, 'position', intval( $_POST['position'] ) );
    }
    If( isset($_POST['writer']) ){
        update_user_meta( $user_id, 'writer', true);
    }
        If( isset($_POST['video']) ){
        update_user_meta( $user_id, 'video', true);
    }
        If( isset($_POST['mod']) ){
        update_user_meta( $user_id, 'mod', true);
    }

}
/**
 * Back end registration
 */

add_action( 'user_new_form', 'crf_admin_registration_form' );
function crf_admin_registration_form( $operation ) {
    if ( 'add-new-user' !== $operation ) {
        // $operation may also be 'add-existing-user'
        return;
    }

$first_name = ! empty( $_POST['first_name '] ) ? intval( $_POST['first_name'] ) : '';

$last_name = ! empty( $_POST['last_name '] ) ? intval( $_POST['last_name'] ) : '';

$age = ! empty( $_POST['age'] ) ? intval( $_POST['age'] ) : ''; 

$position = ! empty( $_POST['position '] ) ? intval( $_POST['position'] ) : '';
$writer = ! empty( $_POST['writer '] ) ? intval( $_POST['writer'] ) : '';
$video = ! empty( $_POST['video '] ) ? intval( $_POST['video'] ) : '';
$mod = ! empty( $_POST['mod '] ) ? intval( $_POST['mod'] ) : '';

    ?>
    <h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3>

    <table class="form-table">
        <tr> 
           <th><label for="first_name"><?php esc_html_e( 'First Name', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
            <td>
                <input type="text"
                 id="first_name"
            name="first_name"
            value="<?php echo esc_attr( $first_name ); ?>"
            class="input"
                  />
            </td>

<th><label for="last_name"><?php esc_html_e( 'Last Name', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
            <td>
                <input type="text"
                 id="last_name"
            name="last_name"
            value="<?php echo esc_attr( $last_name ); ?>"
            class="input"
                  />
            </td>

            <th><label for="age"><?php esc_html_e( 'Age', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
            <td>
                <label for="age"><?php esc_html_e( 'Age', 'crf' ) ?><br/>
            <input type="number"
                   min="16"
                   max="100"
                   step="1"
                   id="age"
                   name="age"
                   value="<?php echo esc_attr( $age ); ?>"
                   class="input"
            />
            </td>

            <th><label for="position"><?php esc_html_e( 'Position', 'crf' ); ?></label> <span class="description"><?php esc_html_e( '(required)', 'crf' ); ?></span></th>
            <td>
                <input type="checkbox" name="position1" value="<?php echo esc_attr( $writer ); ?>">
  <label for="position1">Writing</label><br>
  <input type="checkbox" name="position2" value="<?php echo esc_attr( $video ); ?>">
  <label for="position2">Video Editing</label><br>
  <input type="checkbox" name="position3" value="<?php echo esc_attr( $mod ); ?>">
  <label for="position3">Moderator</label>
            </td>
</tr>
</table>
<?php
}
add_action( 'user_profile_update_errors', 'crf_user_profile_update_errors', 10, 3 );
function crf_user_profile_update_errors( $errors, $update, $user ) {
    if ( $update ) {
        return;
    }

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

     if ( empty( $_POST['last_name'] ) ) {
        $errors->add( 'last_name', __( 'Please enter your last name.', 'crf' ) );
    }

    if ( empty( $_POST['age'] ) ) {
        $errors->add( 'age', __( 'Please enter your age.', 'crf' ) );
    }

    if ( ! empty( $_POST['age'] ) && intval( $_POST['age'] ) < 16 ) {
        $errors->add( 'age', __( 'You must be 16 years of age or older to apply.', 'crf' ) );
    }
}
add_action( 'edit_user_created_user', 'crf_user_register' );
add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );

function crf_show_extra_profile_fields( $user ) {
    ?>
    <h3><?php esc_html_e( 'Application Information', 'crf' ); ?></h3>

    <table class="form-table">
        <tr>
                  <th><label for="age"><?php esc_html_e( 'Age', 'crf' ); ?></label></th>
            <td><?php echo esc_html( get_the_author_meta( 'age', $user->ID ) ); ?></td>
                <th><label for="video"><?php esc_html_e( 'Video', 'crf' ); ?></label></th>
            <td><?php echo esc_html( get_the_author_meta( 'video', $user->ID ) ); ?></td>
        </tr>
    </table>
    <?php
}

【讨论】:

  • 这不起作用,但感谢您的编辑。不过,我可能已经想出了如何使用您的解决方案。
  • 没问题,希望你能解决。
【解决方案2】:

我没有尝试使用多项选择题,而是将其切换为单个复选框。

&lt;input type="checkbox" name="writer" value="Yes" /&gt;

现在工作!感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-12
    • 2011-07-04
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2013-12-18
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多