如何在wordpress中将文件实际保存在两个位置

时间:2013-09-10 04:37:08

标签: php wordpress

我想在wordpress中插件,我想在两个目录中上传图片。一个是我的完整形象。另一个是缩略图。这样全尺寸图像不会过载并减慢整个网页的速度。当用户点击该缩略图时,只能指向完整图像。我尝试搜索wordpress codex并找到wp_handle_uploads,但我不能创建两个回调函数,保存在两个地方吗?我怎么能这样做?

    <?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
    */

//create custom post type
    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);


     }

//set enctype to enable file upload
     add_action('post_edit_form_tag', 'cpis_image_add_post_enctype');
      function cpis_image_add_post_enctype(){
                echo ' enctype="multipart/form-data"';
        }

     //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




        ?>

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



        <?php







    }

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

    function boj_mbe_save_meta( $post_id ) {

        add_filter('upload_dir', 'wallpaper_dir');
            function wallpaper_dir(){
            global $post;
             if ('tagging' == $post->post_type){
               return array(
                'path'    => "C:\\xampp\htdocs\wow\wordpress/wp-content/plugins/yeah/uploads/2013/09", //have to be set
                'url'     => "http://localhost/wow/wordpress/wp-content/plugins/yeah/uploads/2013/09", //have to be set
                'subdir'  => "/2013/09", //have to be set
                'basedir' => "C:\\xampp\htdocs\wow\wordpress/wp-content/plugins/yeah/uploads", //have to be set
                'baseurl' => "http://localhost/wow/wordpress/wp-content/plugins/yeah/uploads", //have to be set
                'error'   => false,
            );

            }
            }



     $file = array(
                        'name'     => "yeah" . $_FILES[ 'taggr_upload' ]['name'],
                        'type'     => $_FILES['taggr_upload']['type'],
                        'tmp_name' => $_FILES['taggr_upload']['tmp_name'],
                        'error'    => $_FILES['taggr_upload']['error'],
                        'size'     => $_FILES['taggr_upload']['size']
                    );

                    $upload_overrides = array( 'test_form' => false );


                wp_handle_upload( $file, $upload_overrides );

            set_post_thumbnail_size( 150, 150 );//is this actually how I add thumbnail?

    }

    //create custom post column


    //shortcode


    ?>

这是我的代码,这实际上是我添加缩略图吗?

1 个答案:

答案 0 :(得分:2)

WordPress实际上有一个内置选项,您只需要通过添加正确的钩子在插件中声明它。我要做的是指定自定义缩略图名称和大小。每次将图像上传到媒体库时,它都会被添加为不仅是全尺寸图像,而是图像的另一个副本缩放到您使用set_post_thumbnail_size()函数指定的尺寸:

https://codex.wordpress.org/Post_Thumbnails