• Resolved morgyface

    (@morgyface)


    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I personally don’t use the settings API much, I find it rather tedious. So I’m no expert, but I think I know enough to get by. The API is intended for almost exactly what you are doing. The best part of it is your setting is usually automatically saved in the options table without you needing to do anything special. In your case, as you’ve recognized, the issue is you want to override an existing setting. The API was meant for adding new settings.

    I’m not sure what happens if one adds a setting matching an existing setting. I suspect which ever process is executed last takes precedence. Try playing with the priority parameter of add_action(). I would expect a larger number (try 99 for example) would execute last, but if that doesn’t work try a very small number like 0 or 1.

    If that doesn’t work, try using ‘medium_crop’ as the actual field ID, not just the input name. Try different priorities again. It’s the ol’ try anything you can think of until it works approach to coding ??

    If none of that works, I think explicitly setting the ‘medium_crop’ option from within a sanitation callback for register_setting() will do the job. If you go this route, use a completely unique setting ID, including the field name and all (still get the current value from ‘medium_crop’ though), as this setting becomes a “dummy” setting solely to fire the sanitation callback that does the actual option change. The setting saved by the API does nothing itself.

    The callback name is passed as a third parameter to register_setting()

    Thread Starter morgyface

    (@morgyface)

    Well thanks to your nudge and ongoing support @bcworkz I’ve only gone and got it flippin’ working, I changed the add_settings_field id to medium_crop to match the existing option and field and also added an add_action priority of 1 and it retains the checked status and crops. I’ve tried checking, unchecking, and uploading images in both situations and all seems good. I’m now thinking I’ll add a large crop option and bundle it all up. I might even build my first plug-in. I shall post again shortly with the complete solution for the benefit of others.

    Thread Starter morgyface

    (@morgyface)

    Here’s the complete solution which you’d add to functions.php:

    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>';
    }

    I’ve also created a gist here and will, when time permits, create a plugin.

    Thanks again for your help @bcworkz!

    Moderator bcworkz

    (@bcworkz)

    Sweet! That worked out nicely, well done!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Medium crop added to settings > media’ is closed to new replies.