Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action(
- 'wp_ajax_mpp_custom_move_media_to_gallery',
- function () {
- // this is a sample, use your own nonce verification.
- // Step 1. Please uncomment and validate nonce
- // if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'mpp-custom-move-media-to-gallery' ) ) {
- // wp_send_json_error( __( 'Invalid Nonce', 'mpp-custom-move-media-to-gallery' ) );
- // }
- // Step 2:- check presence of attachment & gallery
- $attachment_id = isset( $_POST['attachment_id'] ) ? absint( $_POST['attachment_id'] ) : 0;
- $gallery_id = isset( $_POST['gallery_id'] ) ? absint( $_POST['gallery_id'] ) : 0;
- if ( ! $gallery_id || ! $attachment_id ) {
- wp_send_json_error( __( 'Invalid Request', 'mpp-custom-move-media-to-gallery' ) );
- }
- // Step 3. Check if the current user is allowed to move the attachment to gallery
- // I will live it upto you to decide the criteria.
- // I will probably check for media owner and gallery owner.
- // Step 4. If all is good, let us try to attach the media to gallery
- $result = mpp_import_attachment( $attachment_id, $gallery_id, $override = array() );
- if ( is_wp_error( $result ) ) {
- wp_send_json_error( $result->get_error_message() );
- }
- // if we are here, the ersult is our media id.
- wp_send_json_success( array( 'media_id' => $result ) );
- }
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement