-
-
Save morgyface/3d9ccbd12e461018eb024fa44ea41b9e to your computer and use it in GitHub Desktop.
// Add crop options for medium and large images to dashboard > settings > media. | |
function crop_settings_api_init() { | |
// Add the section to media settings | |
add_settings_section( | |
'crop_settings_section', | |
'Crop images', | |
'crop_settings_callback_function', | |
'media' | |
); | |
// Add the fields to the new section | |
add_settings_field( | |
'medium_crop', | |
'Medium size crop', | |
'crop_medium_callback_function', | |
'media', | |
'crop_settings_section' | |
); | |
add_settings_field( | |
'large_crop', | |
'Large size crop', | |
'crop_large_callback_function', | |
'media', | |
'crop_settings_section' | |
); | |
register_setting( 'media', 'medium_crop' ); | |
register_setting( 'media', 'large_crop' ); | |
} // crop_settings_api_init() | |
add_action( 'admin_init', 'crop_settings_api_init', 1 ); | |
// Settings section callback function | |
function crop_settings_callback_function() { | |
echo '<p>Choose whether to crop the medium and large size images</p>'; | |
} | |
// Callback function for our medium crop setting | |
function crop_medium_callback_function() { | |
echo '<input name="medium_crop" type="checkbox" id="medium_crop" value="1"'; | |
$mediumcrop = get_option( "medium_crop"); | |
if ( $mediumcrop == 1 ) { | |
echo ' checked'; | |
} | |
echo '/>'; | |
echo '<label for="medium_crop">Crop medium to exact dimensions</label>'; | |
} | |
// Callback function for our large crop setting | |
function crop_large_callback_function() { | |
echo '<input name="large_crop" type="checkbox" id="large_crop" value="1"'; | |
$largecrop = get_option( "large_crop"); | |
if ( $largecrop == 1 ) { | |
echo ' checked'; | |
} | |
echo '/>'; | |
echo '<label for="large_crop">Crop large to exact dimensions</label>'; | |
} |
This works, thanks! This should be in Wordpress core. I think a rogue plugin switched medium and large images to crop on my install, and this was the only simple way to reset that.
Really glad it helped you out @earthbound19, thanks for commenting!
This is very cool, if I can get it to work. I see the options in the Media Settings and it says it's saving, but my medium images aren't showing the new size. I've tried calling 'medium' and 'medium_crop' but the images are still the old medium size (300px x 300px) after I've updated the size to 420px x 420px and cropped. Do I need to reupload the images or something? Thanks, man!
@morgyface, I just found your medium-crop-added-to-settings-media thread on wordpress.org. I could kiss you. In fact I'm going to if I ever see you, so watch out.
@televators if you haven't already, install a regenerate thumbnail plugin (like this one: https://wordpress.org/plugins/regenerate-thumbnails/) and run it to create your new crops
Sorry @televators, I should've explained you need to re-process your image library once you've got this in place. You can either delete and re-add the images in question, if there's only a couple, or you can use something like the regenerate thumbnail plugin the wondrous @zaccety has mentioned. That'll rattle through the whole media library cropping as it goes.
More than a thumbnail crop
For many years it has bugged me that, for each image size within media, there's not a crop option.
So I have cobbled together this, which essentially adds a new section to the existing Settings > Media page called Crop images.
It's that simple, just add the above code to your themes existing functions.php file and you'll see the new section appear.
Developers, if you're after a less bulky solution, know you'll always want the crop to happen and have no interest in adding options to settings. You can simply add this to your functions file: