用户(图像)上的 WP ACF 字段不时消失

WP ACF Field on User (Image) disappears from time to time

我在 User Form 上有一个名为 avatar 的 ACF 字段(这是错误的吗?),它可以正常工作,我可以为每个用户设置头像图像。然而,这个领域不时被取消(消失),我已经阅读了无数主题,试图找到有类似问题的人,但我没有运气。

这是一个 WP Multisite。我为用户上传的图像已经是缩略图大小(150x150),因为 WP 会自己制作这个缩略图大小(图像预览大小也设置为缩略图),它会导致一些错误并导致图像消失吗?没有多大意义,因为一旦我添加它们,它们就会消失。它不会同时发生在所有用户身上。

如果您需要更多信息,请随时询问。

该问题与 WP Multisite 有关。每当我们在其他网站的其中一个网站上更改用户的头像时,它就会被删除。我们认为这些数据是分开处理的,但不幸的是没有。所以我们决定改为创建 user meta fields ,它似乎可以正常工作。 我添加了对我有用的代码(花了一点时间让它工作,我从不同的来源复制了大部分代码),也许它对某人有帮助。 (困难的部分是图像)

add_action('show_user_profile', 'media_selector_settings_page_callback');
add_action('edit_user_profile', 'media_selector_settings_page_callback');
function media_selector_settings_page_callback($user)
{
    // Save attachment ID
    if (isset($_POST['submit_image_selector']) && isset($_POST['avatar-en'])) :
        update_user_meta($user->ID, 'avatar-en', absint($_POST['avatar-en']));
    endif;
    wp_enqueue_media();
    ?>
    <h3><?php _e("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="bio-en"><?php _e("Bio-en"); ?></label></th>
            <td>
                <textarea name="bio-en" id="bio-en" class="regular-text" rows="8"><?php echo esc_attr(get_the_author_meta('bio-en', $user->ID)); ?></textarea>
                <span class="description"><?php _e("Please enter your bio-en."); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="avatar"><?php _e("Avatar-en"); ?></label></th>
            <td>
                <div class='image-preview-wrapper'>
                    <img id='image-preview-en' src='<?php echo wp_get_attachment_url(esc_attr(get_the_author_meta('avatar-en', $user->ID))); ?>' height='100'>
                </div>
                <input id="upload_image_button" type="button" class="button" value="<?php _e('Upload image'); ?>" />
                <input type='hidden' name='avatar-en' id='avatar-en' value='<?php echo esc_attr(get_the_author_meta('avatar-en', $user->ID)); ?>'>
            </td>
        </tr>
        <tr>
            <th><label for="bio-de"><?php _e("Bio-de"); ?></label></th>
            <td>
                <textarea name="bio-de" id="bio-de" class="regular-text" rows="8"><?php echo esc_attr(get_the_author_meta('bio-de', $user->ID)); ?></textarea>
                <span class="description"><?php _e("Please enter your bio-de."); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="avatar"><?php _e("Avatar-de"); ?></label></th>
            <td>
                <div class='image-preview-wrapper'>
                    <img id='image-preview-de' src='<?php echo wp_get_attachment_url(esc_attr(get_the_author_meta('avatar-de', $user->ID))); ?>' height='100'>
                </div>
                <input id="upload_image_button-de" type="button" class="button" value="<?php _e('Upload image'); ?>" />
                <input type='hidden' name='avatar-de' id='avatar-de' value='<?php echo esc_attr(get_the_author_meta('avatar-de', $user->ID)); ?>'>
            </td>
        </tr>
    </table>

<?php
}
add_action('admin_footer', 'media_selector_print_scripts');
function media_selector_print_scripts()
{
    $my_saved_attachment_post_id = get_option('media_selector_attachment_id', 0);
    ?><script type='text/javascript'>
        jQuery(document).ready(function($) {
            // Uploading files
            var file_frame;
            var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
            var set_to_post_id = <?php echo $my_saved_attachment_post_id; ?>; // Set this
            jQuery('#upload_image_button').on('click', function(event) {
                event.preventDefault();
                // If the media frame already exists, reopen it.
                if (file_frame) {
                    // Set the post ID to what we want
                    file_frame.uploader.uploader.param('post_id', set_to_post_id);
                    // Open frame
                    file_frame.open();
                    return;
                } else {
                    // Set the wp.media post id so the uploader grabs the ID we want when initialised
                    wp.media.model.settings.post.id = set_to_post_id;
                }
                // Create the media frame.
                file_frame = wp.media.frames.file_frame = wp.media({
                    title: 'Select a image to upload',
                    button: {
                        text: 'Use this image',
                    },
                    multiple: false // Set to true to allow multiple files to be selected
                });
                // When an image is selected, run a callback.
                file_frame.on('select', function() {
                    // We set multiple to false so only get one image from the uploader
                    attachment = file_frame.state().get('selection').first().toJSON();
                    // Do something with attachment.id and/or attachment.url here
                    $('#image-preview-en').attr('src', attachment.url).css('width', 'auto');
                    $('#avatar-en').val(attachment.id);
                    // Restore the main post ID
                    wp.media.model.settings.post.id = wp_media_post_id;
                });
                // Finally, open the modal
                file_frame.open();
            });
            // Restore the main ID when the add media button is pressed
            jQuery('a.add_media').on('click', function() {

                wp.media.model.settings.post.id = wp_media_post_id;
            });
        });
    </script>
<?php
}
add_action('admin_footer', 'media_selector_print_scripts_de');
function media_selector_print_scripts_de()
{
    $my_saved_attachment_post_id = get_option('media_selector_attachment_id', 0);
    ?><script type='text/javascript'>
        jQuery(document).ready(function($) {
            // Uploading files
            var file_frame;
            var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
            var set_to_post_id = <?php echo $my_saved_attachment_post_id; ?>; // Set this
            jQuery('#upload_image_button-de').on('click', function(event) {
                event.preventDefault();
                // If the media frame already exists, reopen it.
                if (file_frame) {
                    // Set the post ID to what we want
                    file_frame.uploader.uploader.param('post_id', set_to_post_id);
                    // Open frame
                    file_frame.open();
                    return;
                } else {
                    // Set the wp.media post id so the uploader grabs the ID we want when initialised
                    wp.media.model.settings.post.id = set_to_post_id;
                }
                // Create the media frame.
                file_frame = wp.media.frames.file_frame = wp.media({
                    title: 'Select a image to upload',
                    button: {
                        text: 'Use this image',
                    },
                    multiple: false // Set to true to allow multiple files to be selected
                });
                // When an image is selected, run a callback.
                file_frame.on('select', function() {
                    // We set multiple to false so only get one image from the uploader
                    attachment = file_frame.state().get('selection').first().toJSON();
                    // Do something with attachment.id and/or attachment.url here
                    $('#image-preview-de').attr('src', attachment.url).css('width', 'auto');
                    $('#avatar-de').val(attachment.id);
                    // Restore the main post ID
                    wp.media.model.settings.post.id = wp_media_post_id;
                });
                // Finally, open the modal
                file_frame.open();
            });
            // Restore the main ID when the add media button is pressed
            jQuery('a.add_media').on('click', function() {

                wp.media.model.settings.post.id = wp_media_post_id;
            });
        });
    </script>
<?php
}

add_action('personal_options_update', 'save_extra_user_profile_fields');
add_action('edit_user_profile_update', 'save_extra_user_profile_fields');
function save_extra_user_profile_fields($user_id)
{
    update_user_meta($user_id, 'avatar-en', absint($_POST['avatar-en']));
    update_user_meta($user_id, 'avatar-de', absint($_POST['avatar-de']));
    update_user_meta($user_id, 'bio-en', $_POST['bio-en']);
    update_user_meta($user_id, 'bio-de', $_POST['bio-de']);
}

$args = array(
    'single' => true,
    'show_in_rest' => true,
);
register_meta('user', 'avatar-en', $args);
register_meta('user', 'avatar-de', $args);
register_meta('user', 'bio-en', $args);
register_meta('user', 'bio-de', $args);