Skip to content

Instantly share code, notes, and snippets.

@raftaar1191
Created March 11, 2017 11:59
Show Gist options
  • Save raftaar1191/13eec8d729a85e4fdf2e35dd5e15a5e9 to your computer and use it in GitHub Desktop.
Save raftaar1191/13eec8d729a85e4fdf2e35dd5e15a5e9 to your computer and use it in GitHub Desktop.
Alert the Attachment id of the media that is being upload
<?php
if ( ! function_exists( 'wp_footer_rtmedia_add_script_callback' ) ) {
/**
* Add javascript in the footer
*/
function wp_footer_rtmedia_add_script_callback() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
rtMediaHook.register('rtmedia_js_after_file_upload', function ( resp ) {
if ( jQuery.type( resp ) == 'array' ) {
if ( typeof resp[2] != 'undefined' ) {
media_obj = JSON.parse( resp[ 2 ] );
// Check for attachment_id exists or not.
if ( typeof media_obj !== 'undefined' && typeof media_obj.attachment_id !== 'undefined' ) {
// Check for media_type exists or not and it media_type should be 'photo' only.
if ( typeof media_obj.media_type !== 'undefined' && media_obj.media_type == 'photo' ) {
// Alert Media id
alert( media_obj.attachment_id );
}
}
}
}
return true;
});
});
</script>
<?php
}
}
add_action( 'wp_footer', 'wp_footer_rtmedia_add_script_callback', 10 );
if ( ! function_exists( 'rtmedia_upload_endpoint_response_callback' ) ) {
/**
* Add more varibale after the media is being uploaded
* @param array $data
* @return array
*/
function rtmedia_upload_endpoint_response_callback( $data = array() ){
if ( class_exists( 'RTMediaMedia' ) && is_array( $data ) && isset( $data['media_id'] ) && ! empty( $data['media_id'] ) ) {
$media_id = intval( $data['media_id'] );
$media_obj = new RTMediaMedia();
$media = $media_obj->model->get( array( 'id' => $media_id ) );
if ( isset( $media ) && count( $media ) > 0 ) {
$data['attachment_id'] = $media[0]->media_id;
$data['media_type'] = $media[0]->media_type;
}
}
return $data;
}
}
add_filter( 'rtmedia_upload_endpoint_response', 'rtmedia_upload_endpoint_response_callback', 10, 1 );
@raftaar1191
Copy link
Author

raftaar1191 commented Mar 11, 2017

It alert attachment id that has been added from the rtMedia upload section.
It also checks for the media_type equal to 'photo' only. We can change media type as per our need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment