0
jQuery.fn.upload_image = function( button ) {
    var button_id = button.attr('id');
    var field_id = button_id.replace( '_button', '' );

    // If the media frame already exists, reopen it.
    if ( file_frame ) {
      file_frame.open();
      return;
    }

    // Create the media frame.
    file_frame = wp.media.frames.file_frame = wp.media({
      title: jQuery( this ).data( 'uploader_title' ),
      button: {
        text: jQuery( this ).data( 'uploader_button_text' ),
      },
      multiple: false
    });

    // When an image is selected, run a callback.
    file_frame.on( 'select', function() {
      var attachment = file_frame.state().get('selection').first().toJSON();
      jQuery("#"+field_id).val(attachment.id);
      jQuery("#listingimage img").attr('src',attachment.url);
      jQuery( '#listingimage img' ).show();
      jQuery( '#' + button_id ).attr( 'id', 'remove_image_button' );
      jQuery( '#remove_image_button' ).text( 'Remove image' );
    });

    // Finally, open the modal
    file_frame.open();
};

jQuery('#listingimage').on( 'click', '#upload_image_button', function( event ) {
    event.preventDefault();
    jQuery.fn.upload_listing_image( jQuery(this) );
});

jQuery('#listingimage').on( 'click', '#remove_image_button', function( event ) {
    event.preventDefault();
    jQuery( '#upload_image' ).val( '' );
    jQuery( '#listingimage img' ).attr( 'src', '' );
    jQuery( '#listingimage img' ).hide();
    jQuery( this ).attr( 'id', 'upload_image_button' );
    jQuery( '#upload_image_button' ).text( 'Set image' );
});

});

  • 2
    Can you please add some additional details showing how this code is enqueued and explain at what point you encounter the error? – Dave Romsey Oct 5 '16 at 5:20
  • 1
    This question is far to vague. Simply posting code and stating it is not working, will not get you a proper answer. Please provide additional details, as to what it is exactly you are trying to achieve, where you think it is failing, and also as @DaveRomsey pointed out, how it is being enqueued so we can try and figure out a solution for you. – EBennett Oct 5 '16 at 7:44
0

Even if this "question" is horrible asked here, i think his problem is that "file_frame.on( 'select'" seems not to work anymore, at least that's the issue I'm recently experiencing.

This seems to have something todo with an WordPress update, because this code worked in the past.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.