Adding wp_enqueue_media(); causes problem

I want to add the media uploader in the theme options page. If I add following code in the options page, the media uploader works fine there, but it creates problem in the standard posts featured image. It does not let me select any image from there.

Would it be because I’m adding it in wrong way?

if ( ! did_action( 'wp_enqueue_media' ) ){
    wp_enqueue_media();
}

Here’s the uploading function I’m using:

$('#upload_img').click(function(){
    wp.media.editor.send.attachment = function(props, attachment){          
        $('#theme_options\\[img_url\\]').val(attachment.url);
    }
    wp.media.editor.open(this);
    return false;
});

1 Answer
1

There’s not enough information to really determine what is happening, but the best course of action is to make sure your code is only added to the options page, so it can’t interfere with anything on the edit page. You can use get_current_screen for that, like this:

add_action( 'current_screen', 'wpse113256_this_screen' );

function wpse113256_this_screen() {
    $current_screen = get_current_screen();
    if( $current_screen ->id === "options" ) {
        // Run your code
    }
}

You’ll have to check if the ID of your options page is actually “options”.

Leave a Comment