I manually registered two photos in the database and in the “meta_key” column I added “my-image-for-post” together with the post id.
In the complete code below the part $url =get_post_meta($post->ID,‘my-image-for-post’, false); I can print the ulrs with the foreach.
I can not save multiple images, I have two doubts:
1 - How do I replace the current urls in db by another url selected in the image manager and if there is no url when clicking save post also register the new urls using foreach?
2 - How do I add in each image the delete and edit button? Because I want that before saving the post and permanently registering the ulrs the user can update, delete or add new images
add_action('plugins_loaded', function(){
if($GLOBALS['pagenow']=='post.php'){
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
}
});
function my_admin_scripts() { wp_enqueue_script('jquery'); wp_enqueue_script('media-upload'); wp_enqueue_script('thickbox'); } // //wp_register_script('my-upload', WP_PLUGIN_URL.'/my-script.js', array('jquery','media-upload','thickbox')); wp_enqueue_script('my-upload');
function my_admin_styles() { wp_enqueue_style('thickbox'); }
add_action('add_meta_boxes', function(){ add_meta_box('my-metaboxx1', 'Galery','func99999', get_post_types(),'normal'); }, 9);
function func99999($post){
$url =get_post_meta($post->ID,'my-image-for-post', false); ?>
<input id="my_upl_button" type="button" value="Upload Image" /><br/>
<?php foreach ($url as $field) {
?>
<img src="<?php echo $field;?>" style="width:200px;" />
<?php
};
?>
<div id="show">
</div>
<script>
jQuery(document).ready( function( $ ) {
var custom_uploader;
$('#my_upl_button').click(function(e) {
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
return;
}
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title: 'Choose Image',
button: {
text: 'Choose Image'
},
multiple: true
});
custom_uploader.on('select', function() {
var selection = custom_uploader.state().get('selection');
selection.map( function( attachment ) {
attachment = attachment.toJSON();
$("#show").after("<input id='upload_image' type='text' size='36' name='upload_image' value= " +attachment.url+ " /></br><img src=" +attachment.url+" class='my_image_URL' src='' width='80px'></br>");
});
});
custom_uploader.open();
});
});
</script>
Current results: