Created
April 3, 2013 17:36
-
-
Save ocean90/5303385 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Custom Gallery Setting | |
*/ | |
( function( $ ) { | |
var media = wp.media; | |
// Wrap the render() function to append controls | |
media.view.Settings.Gallery = media.view.Settings.Gallery.extend({ | |
render: function() { | |
media.view.Settings.prototype.render.apply( this, arguments ); | |
// Append the custom template | |
this.$el.append( media.template( 'custom-gallery-setting' ) ); | |
// Save the setting | |
media.gallery.defaults.size = 'thumbnail'; | |
this.update.apply( this, ['size'] ); | |
return this; | |
} | |
} ); | |
} )( jQuery ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Custom Gallery Setting | |
* Version: 1.0 | |
* Description: Adds a custom setting to the gallery edit in the media modal | |
* Author: Dominik Schilling | |
* Author URI: http://wphelper.de/ | |
* Plugin URI: http://wpgrafie.de/ | |
* | |
* License: GPLv2 or later | |
* | |
* Copyright (C) 2013 Dominik Schilling | |
* | |
* This program is free software; you can redistribute it and/or | |
* modify it under the terms of the GNU General Public License | |
* as published by the Free Software Foundation; either version 2 | |
* of the License, or (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, write to the Free Software | |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
*/ | |
/** | |
* Don't call this file directly. | |
*/ | |
if ( ! class_exists( 'WP' ) ) { | |
die(); | |
} | |
/** | |
* The class. | |
*/ | |
class Custom_Gallery_Setting { | |
/** | |
* Stores the class instance. | |
* | |
* @var Custom_Gallery_Setting | |
*/ | |
private static $instance = null; | |
/** | |
* Returns the instance of this class. | |
* | |
* It's a singleton class. | |
* | |
* @return Custom_Gallery_Setting The instance | |
*/ | |
public static function get_instance() { | |
if ( ! self::$instance ) | |
self::$instance = new self; | |
return self::$instance; | |
} | |
/** | |
* Initialises the plugin. | |
*/ | |
public function init_plugin() { | |
$this->init_hooks(); | |
} | |
/** | |
* Initialises the WP actions. | |
* - admin_print_scripts | |
*/ | |
private function init_hooks() { | |
add_action( 'wp_enqueue_media', array( $this, 'wp_enqueue_media' ) ); | |
add_action( 'print_media_templates', array( $this, 'print_media_templates' ) ); | |
} | |
/** | |
* Enqueues the script. | |
*/ | |
public function wp_enqueue_media() { | |
if ( ! isset( get_current_screen()->id ) || get_current_screen()->base != 'post' ) | |
return; | |
wp_enqueue_script( | |
'custom-gallery-settings', | |
plugins_url( 'js/custom-gallery-setting.js', __FILE__ ), | |
array( 'media-views' ) | |
); | |
} | |
/** | |
* Outputs the view template with the custom setting. | |
*/ | |
function print_media_templates() { | |
if ( ! isset( get_current_screen()->id ) || get_current_screen()->base != 'post' ) | |
return; | |
?> | |
<script type="text/html" id="tmpl-custom-gallery-setting"> | |
<label class="setting"> | |
<span>Size</span> | |
<select class="type" name="size" data-setting="size"> | |
<?php | |
$sizes = apply_filters( 'image_size_names_choose', array( | |
'thumbnail' => __( 'Thumbnail' ), | |
'medium' => __( 'Medium' ), | |
'large' => __( 'Large' ), | |
'full' => __( 'Full Size' ), | |
) ); | |
foreach ( $sizes as $value => $name ) { ?> | |
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, 'thumbnail' ); ?>> | |
<?php echo esc_html( $name ); ?> | |
</option> | |
<?php } ?> | |
</select> | |
</label> | |
</script> | |
<?php | |
} | |
} | |
// Put your hands up... | |
add_action( 'admin_init', array( Custom_Gallery_Setting::get_instance(), 'init_plugin' ), 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment