Front-End User Profile

时间:2017-10-05 作者:730wavy

我有一个前端用户配置文件模板,我正在工作,一切都很好,除了上传配置文件图片。我创建了一个自定义用户字段,我正在尝试使用按钮文件类型上载图片并将URL存储在自定义字段中。

看一看-https://pastebin.com/rPErwCgU

我目前已经在使用此代码进行前端帖子上传,它允许多张图片。我需要找出如何为只有1张照片的用户执行此操作,并将其存储在附加到当前用户的自定义字段中--

if (!empty($_FILES[\'sightMulti\'][\'tmp_name\'][0])) {
    $i = 1;
    $files = $_FILES[\'sightMulti\'];
    foreach ($files[\'name\'] as $key => $value) {
        if ($files[\'name\'][$key]) {
            $file = array(
                \'name\' => $files[\'name\'][$key],
                \'type\' => $files[\'type\'][$key],
                \'tmp_name\' => $files[\'tmp_name\'][$key],
                \'error\' => $files[\'error\'][$key],
                \'size\' => $files[\'size\'][$key]
            );
            $_FILES = array("sight" . $i => $file);
            $newuploadMulti = sight("sight" . $i, $pid);
            if ($i == 1) {
                update_post_meta($pid, \'_thumbnail_id\', $newuploadMulti);
            }
            add_post_meta($pid, \'imic_property_sights\', $newuploadMulti, false);
        }
        $i++;
    }
}
表单中的字段--

<div class="col-md-12 col-sm-12">
        <label><?php _e(\'Upload Images\', \'framework\'); ?></label>
        <p><?php _e(\'Upload images that are best clicked for better appearance of your property\', \'framework\'); ?></p> 
</div>
<div class="row" id="multiplePhotos" style="margin-top:15px;">
    <div class="col-md-12 col-sm-12">
        <?php
        echo\'<div class="image-placeholder" id="photoList">\';
        if (!empty($property_sights_value)) {
            foreach ($property_sights_value as $property_sights) {
                $default_featured_image = get_post_meta($Property_Id, \'_thumbnail_id\', true);
                if ($default_featured_image == $property_sights) {
                    $def_class = \'default-feat-image\';
                } else {
                    $def_class = \'\';
                }
                echo \'<div class="col-md-2 col-sm-2">\';
                echo \'<div id="property-img"><div id="property-thumb" class="\' . $def_class . \'"><a id="feat-image" class="accent-color default-image" data-original-title="Set as default image" data-toggle="tooltip" style="text-decoration:none;" href="#"><div class="property-details" style="display:none;"><span class="property-id">\' . $Property_Id . \'</span><span class="thumb-id">\' . $property_sights . \'</span></div><img src="\' . wp_get_attachment_thumb_url($property_sights) . \'" class="image-placeholder" id="filePhoto2" alt=""/></a>\';
                if (get_query_var(\'site\')) {
                    echo \'<input rel="\' . $Property_Id . \'" type="button" id="\' . $property_sights . \'" value="Remove" class="btn btn-sm btn-default remove-image">\';
                }
                echo \'</div></div>\';
                echo \'</div>\';
            }
        }
        echo \'</div>\';
        ?>
        <input id="filePhotoMulti" type="file" name="sightMulti[]" multiple onChange="previewMultiPhotos();">
    </div>
</div>

UPDATE

这是我的右上方的字段,其他字段可以保存罚款-

if ( !empty( $_POST[\'agent-image\'] ) )
update_user_meta( $current_user->ID, \'agent_image\', esc_attr( $_POST[\'agent-image\'] ) );

if(!empty($_FILES)) {
$uploaddir = wp_upload_dir();

$file = $_FILES[agent-image];

$uploadfile = $uploaddir[\'path\'] . \'/\' . basename( $file[\'name\'] );

move_uploaded_file( $file[\'tmp_name\'] , $uploadfile );

$filename = basename( $uploadfile );

$wp_filetype = wp_check_filetype(basename($filename), null );

$attachment = array(
    \'post_mime_type\' => $wp_filetype[\'type\'],
    \'post_title\'     => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
);

$attach_id = wp_insert_attachment( $attachment, $uploadfile );
}
我的输入字段--

<input type="file" name="agent-image">
我是如何在作者身上使用它的。php模板--

<?php 
$author_pic = get_the_author_meta(\'agent-image\', $user->ID);
echo \'<div><img src="\'. $author_pic .\'" alt="\'. $userName .\'" class="img-thumbnail authppic"></div>\'; ?>

UPDATE 2

代码现在导致该页面出现HTTP 500错误--

if ( !empty( $_FILES[agent-image][name] )) {
$uploaddir = wp_upload_dir();

$file = $_FILES[agent-image];

$uploadfile = $uploaddir[\'path\'] . \'/\' . basename( $file[\'name\'] );

move_uploaded_file( $file[\'tmp_name\'] , $uploadfile );

$filename = basename( $uploadfile );

$wp_filetype = wp_check_filetype(basename($filename), null );

$attachment = array(
    \'post_mime_type\' => $wp_filetype[\'type\'],
    \'post_title\'     => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
);

$attach_id = wp_insert_attachment( $attachment, $uploadfile );
update_user_meta( $current_user->ID, \'agent_image\', $attach_id ));
}

2 个回复
SO网友:Andrew Welch

更新:如评论中所述,您可能希望了解如何在Wordpress中创建元框和上传文件,而无需先使用CMB2之类的框架。然而,作为一名经验丰富的Wordpress开发人员,我发现使用CMB2大大提高了我的工作效率。

我强烈建议使用CMB2 创建前端表单以编辑用户配置文件。我只是做了同样的事情,并修改了这些链接中的代码:

您有3个函数(函数名称来自上面的第二篇链接文章):一个注册cmb2字段

function wds_frontend_form_register() {
//code..
}
可以创建用于显示表单的快捷码:

function wds_do_frontend_form_submission_shortcode( $atts = array() ) {
//code..
}
第三个处理表单的提交:

function wds_handle_frontend_new_post_form_submission( $cmb, $post_data = array() ) {
//code..
}
您可以轻松更新您的用户,而无需在此处插入帖子wp_update_user()update_user_meta().

链接中提供了完整的代码示例。CMB2 field types reference docs here. Link to file field type (allowing image upload).

此链接example-functions.php 还有一个编辑用户配置文件字段的示例(但这会自动挂接到管理端)。如果您还想在那里添加字段,那么这很方便。

祝你好运我真的建议CMB2 真是太棒了。

SO网友:lukgoh
if ( !empty( $_FILES[agent-image][name] ) ) {

                $file = $_FILES[agent-image];

                $file_return = wp_handle_upload( $file, array(\'action\' => \'tm_about_you\' ) );

                if( isset( $file_return[\'error\'] ) || isset( $file_return[\'upload_error_handler\'] ) ) {

                  return false;

                } else {

                    $filename = $file_return[\'file\'];

                    $attachment = array(
                        \'post_mime_type\' => $file_return[\'type\'],
                        \'post_title\'     => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $filename ) ),
                        \'post_content\'   => \'\',
                        \'post_status\'    => \'inherit\',
                        \'guid\' => $file_return[\'url\']
                    );

                    $attachment_id = wp_insert_attachment( $attachment, $file_return[\'url\'] );

                    // Generate the metadata for the attachment, and update the database record.
                    $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
                    wp_update_attachment_metadata( $attachment_id, $attachment_data );

                    update_user_meta( $current_user->ID, \'agent_image\', $attachment_id ));

                }

            }
结束

相关推荐

Users redirected to old site

如果我以管理员身份登录,一切都很好。但是,如果我以用户身份登录,它会重定向到旧的测试站点(用于运行站点的开发服务器)。我已经检查了数据库中的siteurl和home值是否正确。我也在使用付费会员pro插件。我搜索了数据库,找到了740个旧URL的条目:(有人能建议在不破坏现场的情况下,最好的前进方式吗?谢谢