rtMedia P2 WordPress comment attachment patch

We have provided a patch to make the P2 theme work with rtMedia WordPress Comment Attachment addon.

You can add below code to your P2 theme’s custom JavaScript code:

jQuery( document ).ready( function() {
	
	// To store all files selected by user to upload.
	var rtmedia_comment_attachments;
	
	jQuery( '#commentform #rtmedia_simple_file_input' ).change( function() {
		rtmedia_comment_attachments = jQuery( this ).get( 0 ).files;
	});
	
	jQuery.ajaxPrefilter(function( options, originalOptions ) {
		
		try {
			//@todo: Add condition to check whether FormData() is available or not for current browser. If supported then proceed otherwise do nothing.
			if ( originalOptions.data && originalOptions.data.action && 'new_comment' == originalOptions.data.action ) {

				var form_data = new FormData();

				jQuery.each( originalOptions.data, function( key, value ) {
					form_data.append( key, value );
				});

				// Don't process the files
				options.processData = false;
				// Set content type to false as jQuery will tell the server its a query string request
				options.contentType = false;

				//Append rtmedia necessary data
				jQuery(".rtmedia-simple-file-upload input[type=hidden]").each( function() {
					form_data.append( jQuery( this ).attr( 'name'), jQuery( this ).val() );
				});

				//Append files
				jQuery.each( rtmedia_comment_attachments, function( key, value ) {
					form_data.append( 'rtmedia_file_multiple[' + key + ']', value );
				});

				options.data = form_data;
			}
		} catch( error ) {
			console.log( error.message );
		}
	});
});