Skip to content

Instantly share code, notes, and snippets.

@fishnyc22 fishnyc22/admin.js
Created May 16, 2013

Embed
What would you like to do?
jQuery(function($) {
var called = 0;
$('#wpcontent').ajaxStop(function() {
if ( 0 == called ) {
$('[value="uploaded"]').attr( 'selected', true ).parent().trigger('change');
called = 1;
}
});
var oldPost = wp.media.view.MediaFrame.Post;
wp.media.view.MediaFrame.Post = oldPost.extend({
initialize: function() {
oldPost.prototype.initialize.apply( this, arguments );
this.states.get('insert').get('library').props.set('uploadedTo', wp.media.view.settings.post.id);
}
});
});
add_action('admin_enqueue_scripts', 'add_admin_js');
function add_admin_js(){
wp_enqueue_script('admin_js', get_bloginfo( 'template_directory' ) . '/javascripts/admin.js');
}
@radiokills

This comment has been minimized.

Copy link

commented Sep 14, 2013

Mind that you don't have wp.media always availible. I've surrounded everything with if(wp.media){}

@hansharhoff

This comment has been minimized.

Copy link

commented Jan 9, 2014

If you are creating functions.php from scratch remember to put <?php at the beginning. If in addition you are using a child theme functions.php should be:

@nkmail

This comment has been minimized.

Copy link

commented May 3, 2014

radiokills, can you put your code in full here, please. I got "Uncaught ReferenceError: wp is not defined " in my case, so javascripts stop working (

@theenoahmason

This comment has been minimized.

Copy link

commented Feb 18, 2018

Greetings from 2018! This still works flawlessly.

Forked it to an ES6/Babel implementation for those using modern builds!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.