Add Field to General Settings Page

If you need to add an option to a site, but it doesn’t really need to be on its own page. You can probably add it to one of the existing settings pages. Here’s how to add an option to the General Settings page.

In this case, I’m adding a ‘favorite color’ field, probably not the best example, so go ahead and change that. 🙂

$new_general_setting = new new_general_setting();

class new_general_setting {
    function new_general_setting( ) {
        add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
    }
    function register_fields() {
        register_setting( 'general', 'favorite_color', 'esc_attr' );
        add_settings_field('fav_color', '<label for="favorite_color">'.__('Favorite Color?' , 'favorite_color' ).'</label>' , array(&$this, 'fields_html') , 'general' );
    }
    function fields_html() {
        $value = get_option( 'favorite_color', '' );
        echo '<input type="text" id="favorite_color" name="favorite_color" value="' . $value . '" />';
    }
}

The third argument of the register_setting() function is the name of the function that should be used to validate the input data, so customize that as needed.

Add New Dashboard Widget

Add a new widget to the dashboard. Note, this will add the widget to the bottom of the page. If you want to try to move it to the top, check out the Codex page about dashboard widgets

add_action('wp_dashboard_setup', 'add_custom_dash_boxes');
function add_custom_dash_boxes() {
    wp_add_dashboard_widget( 
        'welcome_box',
        __( 'Welcome', 'myplugin_textdomain' ),
        'welcome_box_text'
    );
}
function welcome_box_text() {
    echo 'Custom welcome text here';
}

Define A Default Excerpt

I can’t actually think of an appropriate time and place for this, so if you know of one, please share. But the point is, if you want to create a default excerpt to be used on all new posts, here’s how to set that up.

add_filter('default_excerpt','setup_default_excerpt');
function setup_default_excerpt($excerpt) {
	$excerpt .= 'Put your default excerpt here.';
	return $excerpt;
}

Remove Unwanted Dashboard Widgets

If you feel your dashboard is too cluttered, you can use this to remove any or all of the widgets

add_action( 'wp_dashboard_setup', 'remove_wp_dashboard_widgets' );
function remove_wp_dashboard_widgets() {
	//Plugins
	wp_unregister_sidebar_widget( 'dashboard_plugins' );
	remove_meta_box('dashboard_plugins', 'dashboard', 'normal');

	//Right Now
	//wp_unregister_sidebar_widget( 'dashboard_right_now' );
	//remove_meta_box('dashboard_right_now', 'dashboard', 'normal');

	//Recent Comments
	//wp_unregister_sidebar_widget( 'dashboard_recent_comments' );
	//remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

	//Incoming Links
	//wp_unregister_sidebar_widget( 'dashboard_incoming_links' );
	//remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');

	//WordPress Blog
	wp_unregister_sidebar_widget( 'dashboard_primary' );
	remove_meta_box('dashboard_primary', 'dashboard', 'side');

	//Other WordPress News
	wp_unregister_sidebar_widget( 'dashboard_secondary' ); 
	remove_meta_box('dashboard_secondary', 'dashboard', 'side');

	//Quick Press
	//wp_unregister_sidebar_widget( 'dashboard_quick_press' ); 
	//remove_meta_box('dashboard_quick_press', 'dashboard', 'side');

	//Recent Drafts
	//wp_unregister_sidebar_widget( 'dashboard_recent_drafts' ); 
	//remove_meta_box('dashboard_recent_drafts', 'dashboard', 'side');
}

Extra Username Validation

Run a family-friendly site and want to make sure no inappropriate words end up in usernames? Use this

add_filter( 'validate_username', 'forbidden_username_terms', 10, 2);
function forbidden_username_terms($valid, $username) {
	$badwords = array('badword','nogood');
	foreach($badwords as $bw) {
		if (strpos($username,$bw) !== false) return false; 
	}
	return $username;
}

Adding the NextPage Button Back to the Toolbar

There’s a convenient button for the <!--more--> tag on the toolbar, but nothing for the <!--nextpage--> quicktag. Fix that with this

add_filter('mce_buttons','add_nextpage_button');
function add_nextpage_button($buttons) {
    //array_splice so we can insert the new item without overwriting an existing button
    array_splice($buttons, 15,0, 'wp_page' );
    return $buttons;
}

Append to Content

If you need to auto-append information to the end of your page/post content, there’s an easier way to do it than editing your theme.

add_filter( 'the_content', 'append_to_content' );
function append_to_content($content) {
	global $post;
	//if you want to restrict this to a specific post type
	if (get_post_type($post) == 'post') {
		$content .= '<p style="text-align:center;">***</p>';
	}
	return $content;
}

Can be especially useful for auto-appending custom field information

Add A Security Question to the Register Screen

Want to allow people to register on your site, but don’t want to be flooded with spam users? Try this:

add_action( 'register_form', 'add_register_field' );
function add_register_field() { ?>
	<p>
		<label><?php _e('What is the name of the ship in the TV show Firefly?') ?><br />
		<input type="text" name="user_proof" id="user_proof" class="input" size="25" tabindex="20" /></label>
	</p>
<?php }

add_action( 'register_post', 'add_register_field_validate', 10, 3 );
function add_register_field_validate( $sanitized_user_login, $user_email, $errors) {
	if (!isset($_POST[ 'user_proof' ]) || empty($_POST[ 'user_proof' ])) {
		return $errors->add( 'proofempty', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question.'  );
	} elseif ( strtolower( $_POST[ 'user_proof' ] ) != 'serenity' ) {
		return $errors->add( 'prooffail', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question correctly.'  );
	}
}

This adds an extra box to the register screen with the question “What is the name of the ship in the TV show Firefly?” The user-provided answer is then compared to the official answer. If correct, the user is registered, otherwise WordPress-friendly error(s) are returned.