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.

Change Block Format Dropdown in the Visual Editor

Have clients that insist on using <h1> tags throughout their posts? Can’t remember the last time you used the <address> tag? This can help you clean up that block format dropdown menu.

add_filter('tiny_mce_before_init', 'change_mce_dropdown' );
function change_mce_dropdown( $initArray ) {
	$initArray['theme_advanced_blockformats'] = 'p,h3,h4,h5,h6,pre';
	return $initArray;
}

Hide Menu Items

If you need to hide menus from the sidebar of the administration area, you can use this. Keep in mind, this method does not block access, just hides the link.

add_action('admin_menu', 'hide_menu_links');
function hide_menu_links(){
  	global $menu,$submenu;

  	if ($submenu['plugins.php'][15][0] == 'Editor')
	unset($submenu['plugins.php'][15]);

  	if ($menu[15][0] == 'Links')
	unset($menu[15]);

}
// $menu
//     [2] => {Dashboard}
//     [4] => {separator}
//     [5] => {Posts}
//     [10] => {Media}
//     [15] => {Links}
//     [20] => {Pages}
//     [25] => {Comments}
//     [59] => {separator}
//     [60] => {Appearance
//     [65] => {Plugins}
//     [70] => {Users}
//     [75] => {Tools}
//     [80] => {Settings}
//     [99] => {separator}

// $submenu
//     [index.php] =>
//         [0] => {Dashboard}
//         [10] => {Updates}
//     
//     [edit.php] =>
//         [5] => {Posts}
//         [10] => {Add New}
//         [15] => {Categories}
//         [16] => {Post Tags}
//     
//     [upload.php] =>
//         [5] => {Library}
//         [10] => {Add New}
//     
//     [link-manager.php] =>
//         [5] => {Links}
//         [10] => {Add New}
//         [15] => {Link Categories}
//     
//     [edit.php?post_type=page] =>
//         [5] => {Pages}
//         [10] => {Add New}
//     
//     [themes.php] =>
//         [5] => {Themes}
//         [7] => {Widgets}
//         [10] => {Menus}
//     
//     [plugins.php] =>
//         [5] => {Plugins}
//         [10] => {Add New}
//         [15] => {Editor}
//     
//     [users.php] =>
//         [5] => {Users}
//         [10] => {Add New}
//         [15] => {Your Profile
//     
//     [tools.php] =>
//         [5] => {Tools}
//         [10] => {Import}
//         [15] => {Export}
//     
//     [options-general.php] =>
//         [10] => {General}
//         [15] => {Writing}
//         [20] => {Reading}
//         [25] => {Discussions}
//         [30] => {Media}
//         [35] => {Privacy}
//         [40] => {Permalinks}

Simple “Upcoming Posts” Widget

Here’s a simple widget for displaying upcoming posts. Title of widget and number of posts shown is configurable.

add_action( 'widgets_init', 'upcoming_posts_widget_load_widget' ); /* Add our function to the widgets_init hook. */
function upcoming_posts_widget_load_widget() { register_widget( 'upcoming_posts_widget' ); } /* Function that registers our widget. */

class upcoming_posts_widget extends WP_Widget {

    function upcoming_posts_widget() {
        $widget_ops = array( 'classname' => 'upcoming_posts', 'description' => 'Upcoming Posts Widget' ); /* Widget settings. */
        $control_ops = array(  'id_base' => 'upcoming_posts' ); /* Widget control settings. */
        $this->WP_Widget( 'upcoming_posts', 'Upcoming Posts Widget', $widget_ops, $control_ops ); /* Create the widget. */
    }

    function widget( $args, $instance ) {
        extract( $args );  /* before/after widget, before/after title (defined by themes). */
        extract($instance);

        echo $before_widget;
        echo $before_title . $title . $after_title; 

        echo '<ul>';
        $posts = get_posts("post_status=future&numberposts=$numberposts&order=ASC");
        $posts = array_reverse($posts);
        foreach ($posts as $p) {
            echo '<li>'. $p->post_title .'</li>';
        }
        echo '</ul>';
        echo $after_widget; 
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['numberposts'] = (int) $new_instance['numberposts'];
        return $instance;
    }

    function form( $instance ) {
        $defaults = array( 'title' => 'Upcoming Posts', 'numberposts' => 3 );
        $instance = wp_parse_args( (array) $instance, $defaults );
    ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
            </select>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'numberposts' ); ?>">Number Posts:</label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'numberposts' ); ?>" name="<?php echo $this->get_field_name( 'numberposts' ); ?>" value="<?php echo $instance['numberposts']; ?>" />
            </select>
        </p>
    <?php
    }
}

Remove Buttons from the Visual Editor

Unnecessary buttons in the Visual Editor? Try this

add_filter('mce_buttons', 'remove_mce_buttons' );
function remove_mce_buttons($buttons) {
	unset($buttons[16]);
	return $buttons;
}
//     [0] => bold
//     [1] => italic
//     [2] => strikethrough
//     [3] => |
//     [4] => bullist
//     [5] => numlist
//     [6] => blockquote
//     [7] => |
//     [8] => justifyleft
//     [9] => justifycenter
//     [10] => justifyright
//     [11] => |
//     [12] => link
//     [13] => unlink
//     [14] => wp_more
//     [15] => |
//     [16] => spellchecker
//     [17] => fullscreen
//     [18] => wp_adv

If you’re concerned about accidentally removing the wrong one, it’s simple enough to add a check

add_filter('mce_buttons', 'remove_mce_buttons' );
function remove_mce_buttons($buttons) {
	if ($buttons[16] == 'spellchecker')
	unset($buttons[16]);
	return $buttons;
}

Change Admin Footer Text

If you need to customize the WordPress administration area, you’ll find this useful.

add_filter('admin_footer_text', 'left_admin_footer_text_output'); //left side
function left_admin_footer_text_output($text) {
    $text = 'How much wood would a woodchuck chuck?';
    return $text;
}

add_filter('update_footer', 'right_admin_footer_text_output', 11); //right side
function right_admin_footer_text_output($text) {
    $text = "That's purely hypothetical.";
    return $text;
}