Reputation: 3490
I created a metabox in wordpress for uploading photo to my plugin custom directory. I used the following code
<form method="post" enctype="multipart/form-data">
<input type="file" name="taggr_upload">
</form>
This is the form I created in side the add_meta_box()
method
Then I add this save_post action hook
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'] ));
}
Why it doesn't save into my photo folder when I open my folder?
This is the full code:
<?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
}
?>
Why it doesn't work?
Upvotes: 0
Views: 4463
Reputation: 1078
EDIT
Okay first of all, you can not upload a file from any of the metaboxes. Reason? Well, because WordPress wraps all of the post stuff inside a <form>
which does not have the necessary attributes to upload files.
So the solution is to provide an option to upload using WordPress default media uploader and save the URL instead.
Here is a working example. Please use this as a guide, not as your production code. In reality you'd like to fine tune the media uploader button so that multiple button can add multiple urls to multiple inputs.
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 ) {
$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'] ) );
}
}
If you're still facing any problem, then feel free to reply :)
Upvotes: 2