Change Default Media Settings on Theme Activation

Let’s face it, if you’re a developer you want to make things as simple as possible for your clients. One way to do this is to set the default media sizes when your theme is activated. Your users should never need to try to figure out the width of the content area. They shouldn’t have find where the default settings are located. They shouldn’t have to worry about any of that. When they upload an image or use an embed code, the correct sizes should already be set. Here’s how you can do that for them.

// Reset default media sizes
if ( is_admin() && isset( $_GET['activated'] ) && $pagenow == 'themes.php' ){
	global $wpdb;
	$wpdb->query("UPDATE $wpdb->options SET option_value='200' WHERE option_name='thumbnail_size_w'");
	$wpdb->query("UPDATE $wpdb->options SET option_value='200' WHERE option_name='thumbnail_size_h'");
	$wpdb->query("UPDATE $wpdb->options SET option_value='1' WHERE option_name='thumbnail_crop'");
	
	$wpdb->query("UPDATE $wpdb->options SET option_value='400' WHERE option_name='medium_size_w'");
	$wpdb->query("UPDATE $wpdb->options SET option_value='400' WHERE option_name='medium_size_h'");
	
	$wpdb->query("UPDATE $wpdb->options SET option_value='800' WHERE option_name='large_size_w'");
	$wpdb->query("UPDATE $wpdb->options SET option_value='800' WHERE option_name='large_size_h'");

	$wpdb->query("UPDATE $wpdb->options SET option_value='800' WHERE option_name='embed_size_w'");
	$wpdb->query("UPDATE $wpdb->options SET option_value='0' WHERE option_name='embed_size_h'");
	$wpdb->query("UPDATE $wpdb->options SET option_value='1' WHERE option_name='embed_autourls'");
}

This piece of code goes in the functions.php file of the theme. When the theme is activated, it sets all the default media sizes available on the Settings > Media page. Feel free to remove any lines that you don’t need. Then change the remaining values to fit the size of your theme and you’re ready to go!

Posted November 3, 2011 by Alex Mansfield

6 responses to “Change Default Media Settings on Theme Activation”

  1. Luke says:

    I’ve looked before how to do something like this, so it’s nice to find it now.

    In the past I’ve set the embed size for video using the following code. I wonder if there is something similar for the default image sized.

    function themeit_embed_defaults( $embed_size ) {
    $embed_size[‘width’] = 550;
    $embed_size[‘height’] = 400;

    return $embed_size;
    }
    add_filter( ’embed_defaults’, ‘themeit_embed_defaults’ );

  2. Johannes says:

    Why not just use WordPress update_option() instead? Also I think that it is better to do some kind of form or checkup, as it feels a bit risky to just put a get variable that approves everything.

    Assume that you of some reason change settings in admin, and everything is happy until some user of some reason just happens to hit just that URL. Slim chance, but why take it at all?

Leave a Reply

Your email address will not be published. Required fields are marked *

  • PublishedNovember 3, 2011
  • Posted InWordpress
  • DifficultyIntermediate