Php wordpress允许管理员使用自定义字段编辑用户配置文件

Php wordpress允许管理员使用自定义字段编辑用户配置文件,php,wordpress,custom-fields,user-profile,Php,Wordpress,Custom Fields,User Profile,我正在尝试使用一些自定义字段自定义Wordpress站点用户的配置文件。然后,自定义字段应显示在前端。 我已设法在用户管理仪表板上显示字段,以允许站点管理员进行更新 问题1:但是,保存配置文件后,更新不会保存在仪表板中 问题2:更新的字段未显示在用户配置文件的前端 以下是我的子主题的functions.php中的代码: //add field for editing on user profile /** * @param $user * @author Webkul edited b

我正在尝试使用一些自定义字段自定义Wordpress站点用户的配置文件。然后,自定义字段应显示在前端。 我已设法在用户管理仪表板上显示字段,以允许站点管理员进行更新

问题1:但是,保存配置文件后,更新不会保存在仪表板中

问题2:更新的字段未显示在用户配置文件的前端

以下是我的子主题的functions.php中的代码:

//add field for editing on user profile

/**
 *  @param $user
 *  @author Webkul edited by Wano
 */

// add_action('show_user_profile', 'wk_custom_user_profile_fields');
add_action('edit_user_profile', 'wk_custom_user_profile_fields');

function wk_custom_user_profile_fields($user)
{
    echo '<h3 class="heading">Custom Fields</h3>';

?>

    <table class="form-table">
        <tr>
            <th><label for="due">Amount Due</label></th>

            <td><input type="text" class="input-text form-control" name="due" id="due" />
            </td>
        </tr>
        <tr>
            <th><label for="status">Payment Status</label></th>

            <td>
                <select id="status" name="status" size="">
                    <option value="pending">Pending</option>
                    <option value="approved">Approved</option>
                </select>
            </td>
        </tr>
        <tr>
            <th><label for="received">Payment received</label></th>

            <td><input type="text" class="input-text form-control" name="received" id="received" />
            </td>
        </tr>
    </table>

<?php
}

然后字段就不会显示在前端。任何帮助都将不胜感激

这是名为dashboard.php的配置文件页面的屏幕截图。我的自定义代码位于Wayne的注释//代码之间

我显示的是登录用户的用户名,然后在下面我打算显示自定义字段$due、$status和$received。我为代码添加了custom-code.php

下面是我在custom-code.php中显示的字段,这显然是非常错误的

<?php 
    do_action('wk_custom_user_profile_fields', $due);
    do_action('wk_custom_user_profile_fields', $status);
    do_action('wk_custom_user_profile_fields', $received);
?>

似乎逻辑是正确的

也许,将值提取到
HTML
模板可以解决这个问题

add_action( 'edit_user_profile', 'wk_custom_user_profile_fields' );

function wk_custom_user_profile_fields( $user ) {
    $due      = get_user_meta( $user->ID, 'due', true );
    $status   = get_user_meta( $user->ID, 'status', true );
    $received = get_user_meta( $user->ID, 'received', true );

    echo '<h3 class="heading">Custom Fields</h3>';

    ?>

    <table class="form-table">
        <tr>
            <th><label for="due">Amount Due</label></th>

            <td><input type="text" class="input-text form-control" name="due" id="due" value="<?php echo esc_attr( $due ) ?>"/>
            </td>
        </tr>
        <tr>
            <th><label for="status">Payment Status</label></th>

            <td>
                <select id="status" name="status" size="">
                    <option value="pending" <?php selected( $status, 'pending' ) ?>>Pending</option>
                    <option value="approved" <?php selected( $status, 'approved' ) ?>>Approved</option>
                </select>
            </td>
        </tr>
        <tr>
            <th><label for="received">Payment received</label></th>

            <td><input type="text" class="input-text form-control" name="received" id="received" value="<?php echo esc_attr( $received ) ?>"/>
            </td>
        </tr>
    </table>

    <?php
}

太棒了,谢谢你,这解决了第一个问题,即表单不能在后端更新。现在,这些值保存在管理仪表板中。现在我只需要在前端显示这些值,我不认为它们会神奇地出现;您可能需要将这些添加到操作中。谢谢@Andril Kovalenko。我正在使用一个名为Kadence的主题和一个名为affiliate的附属插件。附属插件调用dashboard.php文件,该文件是用户配置文件页面。我已经为这个动作添加了一个custom.php文件,但是我不知道如何调用这个动作。我已经用dashboard.php模板更新了这个问题。谢谢你@AndriiKovalenko。这就成功了。
add_action( 'edit_user_profile', 'wk_custom_user_profile_fields' );

function wk_custom_user_profile_fields( $user ) {
    $due      = get_user_meta( $user->ID, 'due', true );
    $status   = get_user_meta( $user->ID, 'status', true );
    $received = get_user_meta( $user->ID, 'received', true );

    echo '<h3 class="heading">Custom Fields</h3>';

    ?>

    <table class="form-table">
        <tr>
            <th><label for="due">Amount Due</label></th>

            <td><input type="text" class="input-text form-control" name="due" id="due" value="<?php echo esc_attr( $due ) ?>"/>
            </td>
        </tr>
        <tr>
            <th><label for="status">Payment Status</label></th>

            <td>
                <select id="status" name="status" size="">
                    <option value="pending" <?php selected( $status, 'pending' ) ?>>Pending</option>
                    <option value="approved" <?php selected( $status, 'approved' ) ?>>Approved</option>
                </select>
            </td>
        </tr>
        <tr>
            <th><label for="received">Payment received</label></th>

            <td><input type="text" class="input-text form-control" name="received" id="received" value="<?php echo esc_attr( $received ) ?>"/>
            </td>
        </tr>
    </table>

    <?php
}
<?php

    $user_id = get_current_user_id();
    
    if ( ! is_user_logged_in() ) {
        return;
    }
    
    $due      = get_user_meta( $user_id, 'due', true );
    $status   = get_user_meta( $user_id, 'status', true );
    $received = get_user_meta( $user_id, 'received', true );
    
    echo esc_html( $due );
    echo esc_html( $status );
    echo esc_html( $received );