In my child Theme I have set my custom image with add_image_size( 'big', 1500, 1000 )
I left out the crop parameter, but it is false by default anyway.
I want to make this a variable option.
I have just written a plugin that displays all the registered image sizes (including WP’s thumbnail, medium and large sizes) plus my custom size.
With checkboxes I do update_option($_size."_crop", true)
for WP’s image sizes and global $_wp_additional_image_sizes; $_wp_additional_image_sizes[$_size]['crop'] = true
for my custom size.
The thumbnail, medium and large work all fine but I can’t get to override the default false $crop parameter from add_image_size()? When I var_dump($_wp_additional_image_sizes;)
on my plugin page after submitting changes, the boolean displays true, but when I reload the plugin page without sumbit it is like add_image_size()
wins over my plugin?
When I set add_image_size( 'big', 1500, 1000, true);
in functions.php everything works like I want, but I want to make it an option to switch between hard and soft cropping - from my plugin options page obviously.
Here is my code:
class CropRegisteredImageSizes {
private $registered_image_sizes = array();
function __construct() {
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
add_action( 'wp_loaded', array( $this, 'load_registered_image_sizes' ) );
}
function load_registered_image_sizes() {
global $_wp_additional_image_sizes;
$get_intermediate_image_sizes = get_intermediate_image_sizes();
// Create the full array with sizes and crop info
foreach( $get_intermediate_image_sizes as $_size ) {
if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
$this->registered_image_sizes[ $_size ]['width'] = get_option( $_size . '_size_w' );
$this->registered_image_sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
$this->registered_image_sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' );
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
$this->registered_image_sizes[ $_size ] = array(
'width' => $_wp_additional_image_sizes[ $_size ]['width'],
'height' => $_wp_additional_image_sizes[ $_size ]['height'],
'crop' => $_wp_additional_image_sizes[ $_size ]['crop']
);
}
}
}
function set_registered_image_sizes() {
global $_wp_additional_image_sizes;
foreach( $this->registered_image_sizes as $size_key => $size_array ) {
// the standard WP image sizes
if ( in_array( $size_key, array( 'thumbnail', 'medium', 'large' ) ) ) {
if ( false === get_option( $size_key . "_crop" ) ) {
add_option( $size_key . "_crop", $this->registered_image_sizes[$size_key]['crop'] );
} else {
update_option( $size_key . "_crop", $this->registered_image_sizes[$size_key]['crop'] );
}
}
// custom image sizes
elseif ( isset( $_wp_additional_image_sizes[ $size_key ] ) ) {
$_wp_additional_image_sizes[ $size_key ]['crop'] = $this->registered_image_sizes[$size_key]['crop'];
}
}
var_dump($_wp_additional_image_sizes);
}
function add_plugin_page() {
add_options_page( 'Settings Admin', 'Crop Registered Image Sizes', 'manage_options', 'cris_settings_page', array( $this, 'create_admin_page' ) );
}
function create_admin_page() {
if ( isset( $_POST['save'] ) ) {
foreach( $this->registered_image_sizes as $size_key => $size_array ) {
// reset
$this->registered_image_sizes[$size_key]['crop'] = false;
// did at least something get checked?
if ( !empty( $_POST["image_sizes_to_crop"] ) ) {
// compare the sizes with the ones send by POST
if ( array_key_exists( $size_key, $_POST["image_sizes_to_crop"] ) ) {
$this->registered_image_sizes[$size_key]['crop'] = true;
}
}
}
// now set them
$this->set_registered_image_sizes();
}
?>
<div class="wrap">
<h2>Crop Registered Image Sizes</h2>
<form method="post" action="">
<?php
settings_fields( 'cris_option_group' );
do_settings_sections( 'cris_settings_page' );
?>
<input type="submit" name="save" id="save" class="button button-primary" value="Save" />
</form>
</div>
<?php
}
function page_init() {
register_setting( 'cris_option_group', 'co_cris_options' );
add_settings_section( 'setting_section_id_1', '', array( $this, 'print_section_info' ), 'cris_settings_page' );
add_settings_field( 'imageSizes', 'Crop Registered Image Sizes', array( $this, 'imageSizes_callback' ), 'cris_settings_page', 'setting_section_id_1' );
}
function print_section_info() {}
function imageSizes_callback() {
foreach ( $this->registered_image_sizes as $size_key => $size_value ) {
// reference
$size = $this->registered_image_sizes[$size_key];
// disable thumbnail
$tn_disabled = ( $size_key == "thumbnail" ? 'disabled="disabled"' : '' );
// on hold
$tn_disabled = '';
printf ( '<input style="margin-right:10px" type="checkbox" %3$s id="image-size-%1$s" name="image_sizes_to_crop[%1$s]" value="1" %2$s /><label for="image-size-%1$s">%1$s - (' . $size['width'] . 'x' . $size['height'] . ')</label><br />', $size_key, checked( $size['crop'], true, false ), $tn_disabled );
}
}
} // END CropRegisteredImageSizes{}
if ( is_admin() ) {
$crop_registered_image_sizes = new CropRegisteredImageSizes();
}