Wordpress - Save post meta with FOREACH and realize operations crud

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:

I tried with the two codes below but I am not able to save the images in the database

1

add_action( 'save_post', function ($post_id) {
		
    if (isset($_POST['my_image_URL'])){
            $urls = $_POST['my_image_URL'];

            Foreach ($urls as $url){
		$key1_values = get_post_custom_values( 'my-image-for-post', $post_id );
		Foreach($key1_values as $value){
			update_post_meta($post_id, 'my-image-for-post', $url, $value);
		}
	    }
   }
	
});

2

add_action( 'save_post', function ($post_id) {

        if (isset($_POST['my_image_URL'])){
        $urls = $_POST['my_image_URL'];

		 Foreach ($urls as $url){
			update_post_meta($post_id, 'my-image-for-post',$url);
		}
	}
	
});

In the jquery code you are creating the inputs correctly with the name='my_image_URL'

$("#show").after("<input class='upload_image' type='text' size='36' name='my_image_URL' value= " +attachment.url+ " /></br><img src=" +attachment.url+" class='my_image' src='' width='80px'></br>");

I have absolutely no idea what your question is but a big plus one for the spelling in your title.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.