how make input and image upload in metabox
-
hi
i need in metabox one row that have input and upload image . example under image
https://pasteboard.co/IQeYV0G.jpg
this is code
page miu_script.jsjQuery(document).ready(function(){ jQuery('.miu-remove').live( "click", function(e) { e.preventDefault(); var id = jQuery(this).attr("id") var btn = id.split("-"); var img_id = btn[1]; jQuery("#row-"+img_id ).remove(); }); var formfield; var img_id; jQuery(document).on("click", '.Image_button', function(e) { e.preventDefault(); var id = jQuery(this).attr("id") var btn = id.split("-"); img_id = btn[1]; formfield = jQuery('#img-'+img_id); var file_frame; var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id var set_to_post_id = 0; // Set this if ( file_frame ) { // Set the post ID to what we want file_frame.uploader.uploader.param( 'post_id', set_to_post_id ); // Open frame file_frame.open(); return; } else { // Set the wp.media post id so the uploader grabs the ID we want when initialised wp.media.model.settings.post.id = set_to_post_id; } // Create the media frame. file_frame = wp.media.frames.file_frame = wp.media({ title: 'Select a image to upload', button: { text: 'Use this image', }, 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 attachment = file_frame.state().get('selection').first().toJSON(); // Do something with attachment.id and/or attachment.url here formfield.val( attachment.url ); if(formfield.parent().find("img").length > 0) formfield.parent().find("img").attr("src", attachment.url); else formfield.parent().find("span").append('<a target="_blank" href="'+attachment.url+'"><img width="25" src="'+attachment.url+'" /></a>'); // Restore the main post ID wp.media.model.settings.post.id = wp_media_post_id; }); // Finally, open the modal file_frame.open(); }); }); function addRow(image_url){ if(typeof(image_url)==='undefined') image_url = ""; itemsCount+=1; var emptyRowTemplate = '<div id=row-'+itemsCount+'> <input style=\'float:left;width:70%\' id=img-'+itemsCount+' type=\'text\' name=\'miu_images['+itemsCount+']\' value=\''+image_url+'\' />' +' <input type=\'button\' href=\'#\' class=\'Image_button button\' id=\'Image_button-'+itemsCount+'\' value=\'Upload\'>' +' <input class="miu-remove button" type=\'button\' value=\'Remove\' id=\'remove-'+itemsCount+'\' />' +' <span>'; if(image_url) { emptyRowTemplate+= '<a target="_blank" href="'+image_url+'"><img width="25" src="'+image_url+'"></a>'; } emptyRowTemplate+='</span>' +'</div>'; jQuery('#miu_images').append(emptyRowTemplate); }
and page multi-image-upload.php
function call_Multi_Image_Uploader() { new Multi_Image_Uploader(); } /** * Get images attached to some post * * @param int $post_id * @return array */ function miu_get_images($post_id=null) { global $post; if ($post_id == null) { $post_id = $post->ID; } $value = get_post_meta($post_id, 'miu_images', true); $images = unserialize($value); $result = array(); if (!empty($images)) { foreach ($images as $image) { $result[] = $image; } } return $result; } if (is_admin()) { add_action('load-post.php', 'call_Multi_Image_Uploader'); add_action('load-post-new.php', 'call_Multi_Image_Uploader'); } /** * Multi_Image_Uploader */ class Multi_Image_Uploader { var $post_types = array(); /** * Initialize Multi_Image_Uploader */ public function __construct() { $this->post_types = array('post', 'page'); //limit meta box to certain post types add_action('add_meta_boxes', array($this, 'add_meta_box')); add_action('save_post', array($this, 'save')); add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts')); } /** * Adds the meta box container. */ public function add_meta_box($post_type) { if (in_array($post_type, $this->post_types)) { add_meta_box( 'multi_image_upload_meta_box' , __('Upload Multiple Images', 'miu_textdomain') , array($this, 'render_meta_box_content') , $post_type , 'advanced' , 'high' ); } } /** * Save the images when the post is saved. * * @param int $post_id The ID of the post being saved. */ public function save($post_id) { /* * We need to verify this came from the our screen and with proper authorization, * because save_post can be triggered at other times. */ // Check if our nonce is set. if (!isset($_POST['miu_inner_custom_box_nonce'])) return $post_id; $nonce = $_POST['miu_inner_custom_box_nonce']; // Verify that the nonce is valid. if (!wp_verify_nonce($nonce, 'miu_inner_custom_box')) return $post_id; // If this is an autosave, our form has not been submitted, // so we don't want to do anything. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; // Check the user's permissions. if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) return $post_id; } else { if (!current_user_can('edit_post', $post_id)) return $post_id; } /* OK, its safe for us to save the data now. */ // Validate user input. $posted_images = $_POST['miu_images']; $miu_images = array(); if (!empty($posted_images)) { foreach ($posted_images as $image_url) { if (!empty($image_url)) $miu_images[] = esc_url_raw($image_url); } } // Update the miu_images meta field. update_post_meta($post_id, 'miu_images', serialize($miu_images)); } /** * Render Meta Box content. * * @param WP_Post $post The post object. */ public function render_meta_box_content($post) { // Add an nonce field so we can check for it later. wp_nonce_field('miu_inner_custom_box', 'miu_inner_custom_box_nonce'); // Use get_post_meta to retrieve an existing value from the database. $value = get_post_meta($post->ID, 'miu_images', true); $metabox_content = '<div id="miu_images"></div><input type="button" onClick="addRow()" value="Add Image" class="button" />'; echo $metabox_content; $images = unserialize($value); $script = "<script> itemsCount= 0;"; if (!empty($images)) { foreach ($images as $image) { $script.="addRow('{$image}');"; } } $script .="</script>"; echo $script; } function enqueue_scripts($hook) { if ('post.php' != $hook && 'post-edit.php' != $hook && 'post-new.php' != $hook) return; wp_enqueue_script('miu_script', plugin_dir_url(__FILE__) . 'miu_script.js', array('jquery')); } }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘how make input and image upload in metabox’ is closed to new replies.