从编辑个人资料页面上传前端图像。(金苹果)

时间:2011-06-02 作者:MartinJJ

我一直在跟踪goldenapples front end file uploads 教程,但是我确实有我的前端发布页面,但我现在想要完成的是从编辑配置文件页面添加一个图像(这也是一个前端文件上载场景)。

在主题函数中。php我有:

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;
}  
在我的个人资料编辑中。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\\u id引用更改为user\\u id,

现在上传公司徽标确实有效,但是即使使用user\\u id和update\\u usermeta,信息也会保存到wp\\u postmeta表中,这不是我想要的位置,因为我需要从wp\\u usermeta中提取信息来填充作者。php页面。

因此,我的问题(最终得出结论)是,为什么当我使用update\\u usermeta时,附件信息会进入wp\\u Posteta,我猜是因为codex说图像附件是一种帖子类型,所以无论它们在哪里,但我如何从wp\\u Posteta获取该图像并将其与正确的作者关联?

2 个回复
最合适的回答,由SO网友:MartinJJ 整理而成

实际上,通过制作一个插件,可以将钩子从前端短接到用户编辑页面,或者在侧边栏中使用一个单独的短接代码,可以绕过它。下面是问题的症结所在,只需对错误报告进行一些BIT和BOB。

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 } ?>

SO网友:Bainternet

问题出在这一行:

$attach_id = media_handle_upload( $file_handler, $user_id );
当您使用media\\u handle\\u upload并提供第二个参数(在您的情况下是这样做的)时,附件与具有该ID的帖子相关联,因此WordPress基本上认为您是在告诉它将其保存为与用户具有相同ID的帖子的附件,这就是将其保存在Posteta表中的方式。

现在,快速修复方法是删除$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].\'">\';

结束

相关推荐

fetch images and videos

是否可以获取特定网站的视频和图像并将其发布?我的意思是我正在考虑写一个函数,在这里我只写网站名称,然后在网站url的帮助下,所有或最新的图片和视频都会发布到我的帖子上。这可能吗?