Monthly Archives: February 2011

Hide Menu Items

Tell me what you think

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

Tell me what you think

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

Tell me what you think

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

3 responses

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;
}

Custom Post Types

Tell me what you think

Adding a new post type, for when ‘posts’ and ‘pages’ just won’t do.

add_action( 'init', 'my_new_post_types' );
function my_new_post_types() {
    $labels = array(
        'name' => _x('Hotels', 'post type general name'),
        'singular_name' => _x('Hotel', 'post type singular name'),
        'add_new' => _x('Add New', 'hotel'),
        'add_new_item' => __('Add New Hotel'),
        'edit_item' => __('Edit Hotel'),
        'new_item' => __('New Hotel'),
        'view_item' => __('View Hotel'),
        'search_items' => __('Search Hotels'),
        'not_found' =>  __('No hotels found'),
        'not_found_in_trash' => __('No hotels found in Trash'), 
        'parent_item_colon' => '',
        'menu_name' => 'Hotels'    
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true, 
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','author','thumbnail','excerpt','comments')
    ); 
    register_post_type('hotel',$args);
}

Hide Plugin Update count

Tell me what you think

In case you need to hide the plugin update count (can’t/don’t want to update yet) you can hide it with this. Can be especially useful if you don’t want clients to wander over the the update area.

add_action('admin_menu', 'remove_plugin_update_count');
function remove_plugin_update_count(){
    global $menu, $submenu;
    $menu[65][0] = 'Plugins';
    $submenu['index.php'][10][0] = 'Updates';
}

Remove Title Attribute from Featured Image

1 response

If you have featured images (formerly post thumbnails) that aren’t named as well as they could be, it’s nice to remove the title attribute so that things like “header-large” don’t appear in the tool tips.

add_filter('post_thumbnail_html', 'remove_feat_img_title');
function remove_feat_img_title($img) {
    $img = preg_replace('/title="(.*?)"/','',$img);
    return $img; 
}

Custom Taxonomies

Tell me what you think

When you need more than just ‘categories’ and ‘tags’ use custom taxonomies.

Especially useful in combination with custom post types.

add_action('init', 'my_new_taxonomies');
function my_new_taxonomies() {

	$labels = array(
	'name' => __( 'Action Hooks' ),
	'singular_name' => __( 'Action Hook' ),
	'search_items' =>  __( 'Search Action Hooks' ),
	'popular_items' => __( 'Popular Action Hooks' ),
	'all_items' => __( 'All Action Hooks' ),
	'parent_item' => null,
	'parent_item_colon' => null,
	'edit_item' => __( 'Edit Action Hook' ),
	'update_item' => __( 'Update Action Hook' ),
	'add_new_item' => __( 'Add New Action Hook' ),
	'new_item_name' => __( 'New Action Hook' ),
	'separate_items_with_commas' => __( 'Separate hooks with commas' ),
	'add_or_remove_items' => __( 'Add or remove hooks' ),
	'choose_from_most_used' => __( 'Choose from the most used hooks' ),
	'menu_name' => __( 'Action Hooks' ),
	); 

	register_taxonomy('action_hook','post',array(
	'hierarchical' => false,
	'labels' => $labels,
	'show_ui' => true,
	'query_var' => true,
	'rewrite' => array( 'slug' => 'action_hook' ),
	));
}

My FreeNAS Box

1 response

I wasn’t planning on building a computer last week, but I made the mistake of browsing NewEgg while also thinking it would be neat to have some sort of home server for easily sharing files. So here I am, less than 1 week from clicking the first “Add to Cart” button, with my FreeNAS box.

I hope most of you are smarter than I am and won’t need any of this info, but here it is anyway. (more…)