Reputation: 3490
I want to do a plugin in wordpress which I want to upload image in two directory. One is for my full image. and another one is thumbnail. So that the full size image does not overload and slow down the whole web page.When user click on that thumbnail then only direct to the full image. I try to search around wordpress codex and found wp_handle_uploads
but I can't create two callback function that save in two places right?How can I do that?
<?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
?>
This is my code, Is this how actually i add thumbnail??
Upvotes: 0
Views: 824
Reputation: 3339
WordPress actually has an option to do this built in, you just need to declare it in your plugin by adding the the right hook. What I would do is specify a custom thumbnail name and size. Every time an image is uploaded into the media library, it is then added as not only the full size image, but another copy of the image scaled to the sizes you have specified with the set_post_thumbnail_size()
function:
https://codex.wordpress.org/Post_Thumbnails
Upvotes: 2