Medium crop added to settings > media
-
The lack of a medium crop option has been an annoyance of mine for a long time.
I currently use this in functions.php to force a crop.
if ( get_option( "medium_crop") !== false ) { update_option("medium_crop", "1"); } else { add_option("medium_crop", "1"); }
It works, but in order to properly remove it you need to replace it with this:
update_option("medium_crop", "0");
I’m now attempting to create a better solution. Using add_settings_section and add_settings_field I’m adding a new “Crop images” section at the bottom of Settings > Media.
The idea being there’s an option to put a check in the box if you want to crop the medium images. If you don’t want to crop them you uncheck the box.
This code is working to create the new section and the checkbox plus label.
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_cropping', 'Medium size crop', 'crop_medium_callback_function', 'media', 'crop_settings_section' ); register_setting( 'media', 'medium_cropping' ); } // crop_settings_api_init() add_action( 'admin_init', 'crop_settings_api_init' ); // Settings section callback function function crop_settings_callback_function() { echo '<p>Choose whether to also crop the medium size image</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>'; }
However, how do I now merge this with the update_option code, so that once a user clicks “Save Changes” it updates the database?
One important thing I should mention is, I think medium_crop is a standard option, so when I’ve added my settings field I’ve called it “medium_cropping”, I’m not sure about this but then I don’t know how to add the field without add_settings_field or how to use add_settings_field when the option already exists.
Any help by anyone with knowledge of working with the Settings API and existing options would be much appreciated.
- The topic ‘Medium crop added to settings > media’ is closed to new replies.