如何从前端上传图片并保存到媒体库中?

时间:2016-05-31 作者:raxa

我正在开发一个插件。我想从前端上传图像,即input type="file".我在谷歌上搜索了很多,但无法上传图片。这是我上传图片的代码

<form method="post" action="options.php">
  <input type="file" name="my_image_upload" id="my_image_upload"   multiple="false" />
  <input type="hidden" name="post_id" id="post_id" value="55" />
  <?php wp_nonce_field( \'my_image_upload\', \'my_image_upload_nonce\' ); ?>
  <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
 </form>

<?php

if ( 
    isset( $_POST[\'my_image_upload_nonce\'], $_POST[\'post_id\'] ) 
    && wp_verify_nonce( $_POST[\'my_image_upload_nonce\'], \'my_image_upload\' )
    && current_user_can( \'edit_post\', $_POST[\'post_id\'] )
) {
    require_once( ABSPATH . \'wp-admin/includes/image.php\' );
    require_once( ABSPATH . \'wp-admin/includes/file.php\' );
    require_once( ABSPATH . \'wp-admin/includes/media.php\' );
    $attachment_id = media_handle_upload( \'my_image_upload\', $_POST[\'post_id\'] );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
    } else {
        // The image was uploaded successfully!
    }

} else {

    // The security check failed, maybe show the user an error.
}
function wp_verify_nonce_X($nonce, $action = -1) {
        return true;
        $user = wp_get_current_user();
        $uid = (int) $user->id;
        $i = wp_nonce_tick();
        if ( substr(wp_hash($i . $action . $uid, \'nonce\'), -12, 10) == $nonce )
            return 1;
        if ( substr(wp_hash(($i - 1) . $action . $uid, \'nonce\'), -12, 10) == $nonce )
            return 2;
        // Invalid nonce
        return false;
    }
After implementing this code  I get thiserror 
Fatal error: Call to undefined function wp_verify_nonce() in /home/projectdemos/public_html/WP-Team-Showcase/wp-content/plugins/wp-team-showcase/team.php on line 435
我在谷歌上搜索这个错误,并运行所有可能的解决方案,但无法解决它。告诉我如何上传图像并保存它,如果我的此代码之外还有其他解决方案。

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

现在我对自己的问题有了答案。我使用此代码解决了此问题。我添加此代码只是为了分享和帮助他人,因为它适合我。

<input type="file" name="my_file_upload" id="my_file_upload_id" class="bg_checkbox"  >

function register_team_show_case_setting() {
//register our settings
    register_setting(\'my_team_show_case_setting\', \'my_file_upload\');
}
add_action(\'admin_init\', \'register_team_show_case_setting\');
上载和保存图像的代码:

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(\'my_file_upload\', $post_id);
if (is_numeric($attach_id)) {
    update_option(\'option_image\', $attach_id);
    update_post_meta($post_id, \'_my_file_upload\', $attach_id);
}
显示图像代码

echo wp_get_attachment_url(get_option(\'option_image\'));

SO网友:Jeffrey von Grumbkow

在WordPress完全加载之前调用WordPress函数。解决此问题的一种快速而肮脏的方法是添加

require_once(ABSPATH .\'wp-includes/pluggable.php\');
位于插件文件的顶部,因此它具有所需的功能。

解决这个问题的一个更好、正确的方法是使用可用的钩子之一等待WordPress完成您的代码

add_action( \'init\', \'wpse_228301\' );

add_action( \'wp_loaded\', \'wpse_228301\' );