Posted By

FXDB on 11/02/11


Tagged

wordpress


Versions (?)

Adjust settings on theme activation - Add to all new themes


 / Published in: PHP
 

Place this in your functions.php to adjust your default WordPress settings when activating your theme for the first time. This function also deletes the default post, page and comment that are created when you install WordPress.

  1. add_action( 'after_setup_theme', 'the_theme_setup' );
  2. function the_theme_setup()
  3. {
  4. // First we check to see if our default theme settings have been applied.
  5. $the_theme_status = get_option( 'theme_setup_status' );
  6. // If the theme has not yet been used we want to run our default settings.
  7. if ( $the_theme_status !== '1' ) {
  8. // Setup Default WordPress settings
  9. $core_settings = array(
  10. 'avatar_default' => 'mystery', // Comment Avatars should be using mystery by default
  11. 'avatar_rating' => 'G', // Avatar rating
  12. 'comment_max_links' => 0, // We do not allow links from comments
  13. 'comments_per_page' => 20 // Default to 20 comments per page
  14. );
  15. foreach ( $core_settings as $k => $v ) {
  16. update_option( $k, $v );
  17. }
  18. // Delete dummy post, page and comment.
  19. wp_delete_post( 1, true );
  20. wp_delete_post( 2, true );
  21. wp_delete_comment( 1 );
  22. // Once done, we register our setting to make sure we don't duplicate everytime we activate.
  23. update_option( 'theme_setup_status', '1' );
  24. // Lets let the admin know whats going on.
  25. $msg = '
  26. <div class="error">
  27. <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>
  28. </div>';
  29. add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );
  30. }
  31. // Else if we are re-activing the theme
  32. elseif ( $the_theme_status === '1' and isset( $_GET['activated'] ) ) {
  33. $msg = '
  34. <div class="updated">
  35. <p>The ' . get_option( 'current_theme' ) . ' theme was successfully re-activated.</p>
  36. </div>';
  37. add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );
  38. }
  39. }

Report this snippet  

You need to login to post a comment.