Silahkan Melihat Tutorial di website kami dengan nyaman ENJOY YOUR LIFE ☕

Create Multiple Images Upload using Custom field or Meta box in Wordpress

Create Multiple Images Upload using Custom field or Meta box in Wordpress

<?php
/**
 * Plugin Name:       My Gallery
 */

add_action( 'load-post.php', 'smashing_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'smashing_post_meta_boxes_setup' );

function smashing_post_meta_boxes_setup(){
add_action( 'add_meta_boxes', 'smashing_add_post_meta_boxes' );
add_action( 'save_post', 'smashing_save_post_class_meta', 10, 2 );
}

function smashing_add_post_meta_boxes(){
add_meta_box(
    'smashing-post-class',      // Unique ID
    esc_html__( 'Post Class', 'example' ),    // Title
    'smashing_post_class_meta_box',   // Callback function
    'post',         // Admin page (or post type)
    'normal',         // Context
    'default'         // Priority
  );
}


function smashing_post_class_meta_box($object){ ?>
<?php wp_nonce_field( basename( __FILE__ ), 'smashing_post_class_nonce' ); ?>

 
  <p>
  </br>
  <label for="smashing-post-class"><?php _e( "Upload Your Image", 'example' ); ?></label>
  </br>
  <input type="file" id="wp_custom_attachment" name="wp_custom_attachment[]" multiple="multiple" size="25" />
  <br />
  <?php $imgs= get_post_meta(get_the_ID(), 'wp_custom_attachment', true);

  $images=1;
$imgs=json_decode(base64_decode($imgs));
  if(count($imgs)>0){

  foreach ($imgs as $img){
   ?>

      <img src="<?php echo  $img; ?>" width="100px" height="100px"  />
      <?php if($images %4 ==0){ echo "<br>"; } ?>
    
    
<?php  $images++;} }
   ?>
  <input type="hidden" value="<?php echo base64_encode(json_encode($imgs)); ?>" name="save_images"/>
  </p>
  <?php
}


function smashing_save_post_class_meta( $post_id, $post ) {

if ( ! function_exists( 'wp_handle_upload' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
}

  /* Verify the nonce before proceeding. */
  if ( !isset( $_POST['smashing_post_class_nonce'] ) || !wp_verify_nonce( $_POST['smashing_post_class_nonce'], basename( __FILE__ ) ) )
    return $post_id;

  /* Get the post type object. */
  $post_type = get_post_type_object( $post->post_type );

  /* Check if the current user has permission to edit the post. */
  if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
    return $post_id;

  /* Get the posted data and sanitize it for use as an HTML class. */
  $new_meta_value = ( isset( $_POST['smashing-post-class'] ) ? sanitize_html_class( $_POST['smashing-post-class'] ) : '' );


if(!empty($_FILES['wp_custom_attachment']['name'])) {
for($i=0;$i<=count($_FILES['wp_custom_attachment']['name']);$i++){

$_FILES['wp_custom_attachment']['name'][ $i ];
  if ( '' != $_FILES['wp_custom_attachment']['name'][ $i ] ) {

  $upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'][ $i ], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name'][ $i ]));

if(isset($upload['error']) && $upload['error'] != 0) {
wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
}
$images[] = $upload['url'];
  }




}

if(count($images)==0){
$images =$_POST['save_images'];
}else{
$images = base64_encode(json_encode($images));
}

   add_post_meta($post_id, 'wp_custom_attachment', $images);
    update_post_meta($post_id, 'wp_custom_attachment', $images); 

}

}

function update_edit_form() {
    echo ' enctype="multipart/form-data"';
} // end update_edit_form
add_action('post_edit_form_tag', 'update_edit_form');





0 komentar:

Post a Comment

Create Multiple Images Upload using Custom field or Meta box in Wordpress