Advertisement
buddydev

import attachment to gallery

Jun 25th, 2023
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1.  
  2. add_action(
  3.     'wp_ajax_mpp_custom_move_media_to_gallery',
  4.     function () {
  5.  
  6.         // this is a sample, use your own nonce verification.
  7.         // Step 1. Please uncomment and validate nonce
  8.         // if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'mpp-custom-move-media-to-gallery' ) ) {
  9.         // wp_send_json_error( __( 'Invalid Nonce', 'mpp-custom-move-media-to-gallery' ) );
  10.         // }
  11.  
  12.         // Step 2:- check presence of attachment & gallery
  13.         $attachment_id = isset( $_POST['attachment_id'] ) ? absint( $_POST['attachment_id'] ) : 0;
  14.         $gallery_id    = isset( $_POST['gallery_id'] ) ? absint( $_POST['gallery_id'] ) : 0;
  15.         if ( ! $gallery_id || ! $attachment_id ) {
  16.             wp_send_json_error( __( 'Invalid Request', 'mpp-custom-move-media-to-gallery' ) );
  17.         }
  18.         // Step 3. Check if the current user is allowed to move the attachment to gallery
  19.         // I will live it upto you to decide the criteria.
  20.         // I will probably check for media owner and gallery owner.
  21.  
  22.         // Step 4. If all is good, let us try to attach the media to gallery
  23.         $result = mpp_import_attachment( $attachment_id, $gallery_id, $override = array() );
  24.  
  25.         if ( is_wp_error( $result ) ) {
  26.             wp_send_json_error( $result->get_error_message() );
  27.         }
  28.         // if we are here, the ersult is our media id.
  29.         wp_send_json_success( array( 'media_id' => $result ) );
  30.     }
  31. );
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement