Last active
January 31, 2024 10:29
-
-
Save morgyface/3d9ccbd12e461018eb024fa44ea41b9e to your computer and use it in GitHub Desktop.
WordPress | Create checkboxes on Settings > Media which crop medium and large image sizes.
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
// 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>'; | |
} |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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