0

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

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

为什么它不起作用?

4

1 回答 1

2

编辑

好的,首先,您不能从任何元框上传文件。原因?好吧,因为 WordPress 将所有帖子内容包装在 a<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'] ) );
    }
}

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

于 2013-09-07T08:35:33.733 回答