0

我想在 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


    ?>

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

4

1 回答 1

2

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

https://codex.wordpress.org/Post_Thumbnails

于 2013-09-10T04:43:52.573 回答