为什么move_uploaded_file方法在WordPress中不起作用?

时间:2013-09-07 07:10:00

标签: php wordpress

我在wordpress中创建了一个metabox,用于将照片上传到我的插件自定义目录。我使用了以下代码

<form  method="post" enctype="multipart/form-data">
<input type="file" name="taggr_upload">
</form>

这是我在add_meta_box()方法

旁边创建的表单

然后我添加了这个save_post动作钩子

add_action( 'save_post', 'boj_mbe_save_meta' );

function boj_mbe_save_meta( $post_id ) {
        move_uploaded_file($_FILES['taggr_upload']['tmp_name'], 'photo/'. basename( $_FILES['taggr_upload']['name'] ));
}

为什么打开我的文件夹时它不会保存到我的照片文件夹中?

这是完整的代码:

<?php 
/*
Plugin Name: random plug
Plugin URI: http://example.com/wordpress-plugins/my-plugin
Description: A plugin demonstrating Cron in WordPress
Version: 1.0
Author: Brad Williams
Author URI: http://wrox.com
License: GPLv2
*/

add_action('init', 'register_tagging_post');

 function register_tagging_post(){
       $tagging_args = array(
           'public' => true,
           'supports' => array(
               'title',
               'thumbnail'

           ), 
           'query_var' => 'tagging',
           'rewrite' => array(
              'slug' => 'tagging',
              'with_front' => false
           ),
            'labels' => array(
            'name' => 'Albums',
            'singular_name' => 'Album',
            'add_new' => 'Add New Album',
            'add_new_item' => 'Add New Album',
            'edit_item' => 'Edit Album',
            'new_item' => 'New Album',
            'view_item' => 'View Album',
            'search_items' => 'Search Albums',
            'not_found' => 'No Albums Found',
            'not_found_in_trash' => 'No Albums Found In Trash'
        ),
       );

       register_post_type('tagging', $tagging_args);
 }


 //add metabox
 add_action( 'add_meta_boxes', 'boj_mbe_create' );

function boj_mbe_create() {
    //create a custom meta box
    add_meta_box( 'boj-meta', 'My Custom Meta Box', 'boj_mbe_function', 'tagging', 'normal', 'high' );
}

function boj_mbe_function( $post ) {
    //retrieve the meta data values if they exist
    ?>
    <form  method="post" enctype="multipart/form-data">

     <input type="file" name="taggr_upload">
    </form>

    <?php
         echo WP_PLUGIN_DIR;

//hook to save the meta box data
add_action( 'save_post', 'boj_mbe_save_meta' );

function boj_mbe_save_meta( $post_id ) {
    //verify the meta data is set
    if ( isset( $_POST['taggr_upload'] ) ) {
        //save the meta data

       $path_to_plugin = WP_PLUGIN_DIR . '/yeah';
     move_uploaded_file($_FILES['taggr_upload']['tmp_name'], $path_to_plugin . '/photo/'. basename( $_FILES['taggr_upload']['name'] ));
    }
}

//create custom post column
}
?>

为什么它不起作用?

1 个答案:

答案 0 :(得分:2)

修改

首先,您无法从任何元文件上传文件。原因?好吧,因为WordPress将所有帖子内容包装在<form>内,而这些帖子没有必要的属性来上传文件。

因此,解决方案是提供使用WordPress默认媒体上传器上传的选项,并改为保存URL。

这是一个工作示例。请将此作为指南,而不是您的生产代码。实际上,您希望微调媒体上传器按钮,以便多个按钮可以为多个输入添加多个URL。

http://example.com/wordpress-plugins/my-plugin     描述:一个在WordPress中演示Cron的插件     版本:1.0     作者:Brad Williams     作者URI:http://wrox.com     许可证:GPLv2     * /

add_action( 'init', 'register_tagging_post' );

function register_tagging_post() {
    $tagging_args = array(
        'public' => true,
        'supports' => array(
            'title',
            'thumbnail'
        ),
        'query_var' => 'tagging',
        'rewrite' => array(
            'slug' => 'tagging',
            'with_front' => false
        ),
        'labels' => array(
            'name' => 'Albums',
            'singular_name' => 'Album',
            'add_new' => 'Add New Album',
            'add_new_item' => 'Add New Album',
            'edit_item' => 'Edit Album',
            'new_item' => 'New Album',
            'view_item' => 'View Album',
            'search_items' => 'Search Albums',
            'not_found' => 'No Albums Found',
            'not_found_in_trash' => 'No Albums Found In Trash'
        ),
    );
    register_post_type( 'tagging', $tagging_args );
}


//add metabox
add_action( 'add_meta_boxes', 'boj_mbe_create' );

function boj_mbe_create() {
    //create a custom meta box
    add_meta_box( 'boj-meta', 'My Custom Meta Box', 'boj_mbe_function', 'tagging', 'normal', 'high' );
}

function boj_mbe_function( $post ) {
    $file_meta_data = get_post_meta( $post->ID, 'taggr_file', true );
?>
<input type="text" class="regular-text" name="taggr_file" id="taggr_file" value="<?php echo $file_meta_data; ?>" />
<button id="taggr_upload" class="button-primary">Upload</button>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        // Uploading files
        var file_frame;
        jQuery('#taggr_upload').on('click', function( event ) {

            event.preventDefault();

            // If the media frame already exists, reopen it.
            if ( file_frame ) {
                file_frame.open();
                return;
            }

            // Create the media frame.
            file_frame = wp.media.frames.file_frame = wp.media({
                title: jQuery( this ).data( 'uploader_title' ),
                button: {
                    text: jQuery( this ).data( 'uploader_button_text' ),
                },
                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
                var attachment = file_frame.state().get('selection').first().toJSON();
                // Do something with attachment.id and/or attachment.url here
                $('#taggr_file').val(attachment.url);
            });

            // Finally, open the modal
            file_frame.open();
        });
    });
</script>
    <?php
}

//hook to save the meta box data
add_action( 'save_post', 'boj_mbe_save_meta' );

function boj_mbe_save_meta( $post_id ) {
    //verify the meta data is set
    if ( isset( $_POST['taggr_file'] ) ) {
        update_post_meta( $post_id, 'taggr_file', stripslashes( $_POST['taggr_file'] ) );
    }
}

如果您仍然遇到任何问题,请随时回复:)