我一直在关注 GoldenApples前端文件上传 教程,但是我确实在前端发布页面上工作了,但是我现在想完成的工作是添加来自“编辑配置文件”页面的映像(同样,这是一个前端文件上传方案。

在主题函数中,我有:

function insert_complogo($file_handler,$user_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

$attach_id = media_handle_upload( $file_handler, $user_id );

if ($setthumb) update_usermeta($user_id,'_thumbnail_id',$attach_id);
return $attach_id;
}  

在我的profile-edit.php中,我有:

if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_complogo($file,$user_id);
}
}  

也是这样:

if ( !empty( $_POST['authorphoto'] ) )
    update_usermeta( $current_user->id, 'comp_logo', esc_attr($_POST['comp_logo']   ) );   

我的输入类型是:

<p>
   <label><?php _e('Add Company Logo', 'comp_logo') ?></label><br /> 
                        <input type="file" name="comp_logo" id="comp_logo" value="Upload Logo" size="50" />
</p>  

从上面的前三个块中可以看出,我已将post_id引用更改为user_id,

现在,公司徽标的上传确实可以正常工作,但是即使使用user_id和update_usermeta,信息也将保存到wp_postmeta表中,这不是我希望它的位置,因为我需要从wp_usermeta中删除wp_usermeta的信息来填充作者.php页。

所以我的问题(最终到达那里)是为什么当我使用update_usermeta是wp_postmeta中的附件信息时,我猜到了它,因为codex说图像附件是一种帖子,所以无论他们走到哪里,但是我该如何从WP_POSTMETA获取该图像并将其与正确的作者相关联?

有帮助吗?

解决方案 2

实际上,通过制作一个可以从Frontend中插入用户编辑页面的插件或使用侧边栏中的单独短代码来解决它。以下是它的症结,只需要在错误报告中做一些位和鲍勃即可。

require(ABSPATH . WPINC . '/pluggable.php');

define('WP-AUTHOR-LOGO_VERSION', '0.3.0');
define('WP-AUTHOR-LOGO_PLUGIN_URL', plugin_dir_url( __FILE__ ));

register_activation_hook(__FILE__, 'wpal_createfolder');

function wpal_createfolder() {
$target = ABSPATH . 'wp-content/uploads/wpal_logos';
wp_mkdir_p( $target );
}

// Directory for uploaded images 
$uploaddir = ABSPATH . 'wp-content/uploads/wpal_logos';  

// Allowed mimes    
$allowed_ext = "jpg, gif, png";  

// Default is 50kb 
$max_size = get_option(wpal_size);  

// height in pixels, default is 175px 
$max_height = get_option(wpal_height);  

// width in pixels, default is 450px 
$max_width = get_option(wpal_width);  


// Check mime types are allowed  
$extension = pathinfo($_FILES['wpaluploader']['name']);  
$extension = $extension[extension];  
$allowed_paths = explode(", ", $allowed_ext);  
for($i = 0; $i < count($allowed_paths); $i++) {  
if ($allowed_paths[$i] == "$extension") {  
    $ok = "1";  
}  
}  

// Check File Size  
if ($ok == "1") {  
if($_FILES['wpaluploader']['size'] > $max_size)  
{  
    print "Image size is too big!";  
    exit;  
}  

// Check Height & Width  
if ($max_width && $max_height) {  
    list($width, $height, $type, $w) = getimagesize($_FILES['wpaluploader']['tmp_name']);  
    if($width > $max_width || $height > $max_height)  
    {  
        print "Image is too big! max allowable width is&nbsp;" . get_option(wpal_width) ."px and max allowable height is&nbsp;" . get_option(wpal_width) ."px";  
        exit;  
    }  
}  
global $user_id;
get_currentuserinfo();
$image_name=$current_user->id.'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)

// Rename file and move to folder
$newname="$uploaddir./".$image_name;  
if(is_uploaded_file($_FILES['wpaluploader']['tmp_name']))  
{ 
    move_uploaded_file($_FILES['wpaluploader']['tmp_name'], $newname);  
}  
print "Your image has been uploaded successfully!";  
}


// Create shortcode for adding to edit user page
add_shortcode("wp-author-logo", "wpaluploader_input");

function wpaluploader_input() {
$wpaluploader_output = wpaluploader_showform();
return $wpaluploader_output;
}

function wpaluploader_showform() {
$wpaluploader_output = '<p><label for="wpauploader">Upload Company Logo:</label><br />
<input type="file" name="wpaluploader" id="wpaluploader" />
<br />
<small style="color:#ff0000; font-size:0.7em;">Allowed image types are .jpg, .gif, .png.<br />
Max image width = ' . get_option(wpal_width) . 'px, Max image height = ' . get_option(wpal_height) . 'px </small>';
return $wpaluploader_output;
}

// Create other Shortcode for full form
add_shortcode("wp-author-logofull", "wpaluploader_inputfull");

function wpaluploader_inputfull() {
$wpaluploader_outputfull = wpaluploader_showformfull();
return $wpaluploader_outputfull;
}

function wpaluploader_showformfull() {
$wpaluploader_outputfull = '<form method="post" id="adduser" action="' . str_replace( '%7E', '~', $_SERVER['REQUEST_URI']) .'" enctype="multipart/form-data">
<p><label for="wpauploader">Upload Company Logo:</label><br />
<input type="file" name="wpaluploader" id="wpaluploader" />
<br />
<input name="updateuser" type="submit" id="updateuser" class="submit button" value="Upload" />
                        ' . wp_nonce_field( 'update-user' ) . '
                        <input name="action" type="hidden" id="action" value="update-user" />
<small style="color:#ff0000; font-size:0.7em;">Allowed image types are .jpg, .gif, .png.<br />
Max image width = ' . get_option(wpal_width) . 'px, Max image height = ' . get_option(wpal_height) . 'px </small>';
return $wpaluploader_outputfull;
} 

add_action('admin_menu', 'wpal_menu');

function wpal_menu() {
add_options_page('WP Author Logo', 'WP Author Logo', 'manage_options', 'wpal_wp-author-logo', 'wpal');
}

function wpal() {
if (!current_user_can('manage_options'))  {
    wp_die( __('You do not have sufficient permissions to access this page.') ); 
}
?>   
<div class="wrap">
<div class="leftwrap">
    <?php    echo "<h2>" . __( 'Wordpress Author Logo Plugin', 'wpal_lang' ) . "</h2>"; ?>

    <?php  
        if($_POST['wpal_author_logo_success'] == 'Y') {  
            //Form data sent  
            $wpal_width = $_POST['wpal_width'];  
            update_option('wpal_width', $wpal_width);  

            $wpal_height = $_POST['wpal_height'];  
            update_option('wpal_height', $wpal_height);

            $wpal_size = $_POST['wpal_size'];  
            update_option('wpal_size', $wpal_size);    

            $wpal_logos = $_POST['wpal_logos'];  
            update_option('wpal_logos', $wpal_logos); 
        ?>  
        <div class="updated"><p><strong><?php _e('WP Author Logo Plugin Options Saved.' ); ?></strong></p></div>  
        <?php  
        } else {  
            //Normal page display  
            $wpal_width = get_option('wpal_width');  
            $wpal_height = get_option('wpal_height');
            $wpal_size = get_option('wpal_size');  
            $wpal_logos = get_option('wpal_logos');    
        }  
    ?>    

    <form name="wpal_settingsform" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">  
        <input type="hidden" name="wpal_author_logo_success" value="Y">  
        <?php    echo "<h4>" . __( 'Wordpress Author Logo Settings', 'wpal_lang' ) . "</h4>"; ?>  
        <p><label for="wpal_width"><?php _e("Maximum Width: " ); ?></label><br /><input type="text" name="wpal_width" value="<?php echo $wpal_width; ?>" size="20"><?php _e("px"); ?></p>  
        <p><label for="wpal_height"><?php _e("Maximum Height: " ); ?></label><br /><input type="text" name="wpal_height" value="<?php echo $wpal_height; ?>" size="20"><?php _e("px" ); ?></p>
        <p><label for="wpal_size"><?php _e("Maximum Size: " ); ?></label><br /><input type="text" name="wpal_size" value="<?php echo $wpal_size; ?>" size="20"><?php _e("Bytes: hint 50000 bytes = 50Kbs" ); ?></p>  
        <p><label for="wpal_logos"><?php _e("Logo Images Folder: " ); ?></label><br /><input type="text" name="wpal_logos" value="<?php echo $wpal_logos; ?>" size="20"><?php _e(" ex: /wpal_logo_images/" ); ?></p>  

        <p class="submit">  
            <input type="submit" name="Submit" value="<?php _e('Update Options', 'wpal_lang' ) ?>" />  
        </p>  
    </form>
</div><!-- / leftwrap -->
</div><!-- / wrap -->
<?php } ?>

其他提示

问题在这一行中:

$attach_id = media_handle_upload( $file_handler, $user_id );

当您使用Media_handle_upload并提供第二个参数(在您的情况下),附件与具有该ID的帖子相关与用户相同的ID,这是将其保存在PostMeta表中的方式。

现在快速修复是删除 $user_id 从那行:

$attach_id = media_handle_upload( $file_handler);

接下来,我很确定这部分没有任何作用:

if ( !empty( $_POST['authorphoto'] ) )
    update_usermeta( $current_user->id, 'comp_logo', esc_attr($_POST['comp_logo']   ) );  

由于输入字段带有 type="file" 不包括在 $_POST 但在 $_FILES.

为了在作者模板上显示图像,您可以使用 wp_get_attachment_image_src这样的事情:

$image_attributes = wp_get_attachment_image_src( get_usermeta($user_id,'_thumbnail_id',true )); // returns an array
echo '<img src="'.$image_attributes[0].'">';
许可以下: CC-BY-SA归因
scroll top