X

How To Adjust WordPress Settings on Theme Activation

Snippets by IsItWP

Are you looking for a way to adjust WordPress settings on theme activation? This snippet will change the default WordPress settings when the theme is activated. This snippet also deletes the default post, page, and comment that are created when WordPress is installed.

Instructions:

  1. All you have to do is add this code to your theme’s functions.php file.
  2. Edit the settings in the snippet to suit your needs.
add_action( 'after_setup_theme', 'the_theme_setup' );
function the_theme_setup()
{
	// First we check to see if our default theme settings have been applied.
	$the_theme_status = get_option( 'theme_setup_status' );

	// If the theme has not yet been used we want to run our default settings.
	if ( $the_theme_status !== '1' ) {

		// Setup Default WordPress settings
		$core_settings = array(
			'avatar_default'				=> 'mystery',					// Comment Avatars should be using mystery by default
			'avatar_rating'					=> 'G',							// Avatar rating
			'comment_max_links'				=> 0,							// We do not allow links from comments
			'comments_per_page'				=> 20							// Default to 20 comments per page
		);

		foreach ( $core_settings as $k => $v ) {
			update_option( $k, $v );
		}

		// Delete dummy post, page and comment.
		wp_delete_post( 1, true );
		wp_delete_post( 2, true );
		wp_delete_comment( 1 );

		// Once done, we register our setting to make sure we don't duplicate everytime we activate.
		update_option( 'theme_setup_status', '1' );

		// Lets let the admin know whats going on.
		$msg = '
		<div class="error">
			<p>The ' . get_option( 'current_theme' ) . 'theme has changed your WordPress default <a href="' . admin_url() . 'options-general.php" title="See Settings">settings</a> and deleted default posts & comments.</p>
		</div>';
		add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );
	} 
	// Else if we are re-activing the theme
	elseif ( $the_theme_status === '1' and isset( $_GET['activated'] ) ) {
		$msg = '
		<div class="updated">
			<p>The ' . get_option( 'current_theme' ) . ' theme was successfully re-activated.</p>
		</div>';
		add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );
	}
}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: How to Properly Create a Custom Login Page in WordPress and 11 best WordPress plugins for writers.

Comments   Leave a Reply

  1. What else can we do with this?

  2. Very nice. Although, can we change the setting for the permalink structure?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!