hlashbrooke / WordPress-Plugin-Template

A robust code template for creating a standards-compliant WordPress plugin.
https://hughlashbrooke.com/
GNU General Public License v2.0
1.03k stars 329 forks source link

Settings Image Won't Add #40

Open rizedr opened 8 years ago

rizedr commented 8 years ago

First of all, go in the plugin's settings page. The following steps are to reproduce the issue:

  1. Add an image using "Upload new image"
  2. The image thumbnail will show.
  3. Remove the image using "Remove image"
  4. Add a new image using "Upload new image"
  5. The image won't add.
deweydb commented 8 years ago

Actually the image does add, and if you look with inspector the value of the input field will change, its just that the thumbnail is not being displayed anymore because the container was deleted, and there was nothing written to handle re-adding it.

The changes need to happen in the /assets/settings.js file, but then this file should be minified, and the /assets/settings.min.js contents replaced (if you don't do this step it won't work!). So, in /assets/settings.js change: Line: 52 jQuery("#"+preview_id).attr('src',attachment.sizes.thumbnail.url); To: jQuery("#"+preview_id).attr('src',attachment.sizes.thumbnail.url).show();

Line: 67 jQuery(this).closest('td').find( '.image_preview' ).remove(); To: jQuery(this).closest('td').find( '.image_preview' ).hide();

Please note that wordpress caches admin javascript files, so it is advisable to add some cache busting for a development of a wordpress plugin, i.e. in /includes/class-your-plugin.php around 190, change: public function admin_enqueue_scripts ( $hook = '' ) { wp_register_script( $this->_token . '-admin', esc_url( $this->assets_url ) . 'js/admin' . $this->script_suffix . '.js', array( 'jquery' ), $this->_version ); wp_enqueue_script( $this->_token . '-admin' ); } // End admin_enqueue_scripts () to: public function admin_enqueue_scripts ( $hook = '' ) { wp_register_script( $this->_token . '-admin', esc_url( $this->assets_url ) . 'js/admin' . $this->script_suffix . '.js?t='.time(), array( 'jquery' ), $this->_version ); wp_enqueue_script( $this->_token . '-admin' ); } // End admin_enqueue_scripts ()