Archives

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

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

Customize the Favorites Menu

Ever use that favorites menu up by the “Howdy” greeting? I never really did until I started customizing the options.

One of my favorites is adding a link to the master options page (wp-admin/options.php)

add_filter('favorite_actions', 'custom_favorites');
function custom_favorites($actions) {
		$actions['options.php'] = array('All Settings', 'unfiltered_html'); // this line adds an item
		return $actions;
}

Or, remove the menu entirely

add_filter('favorite_actions', 'remove_favorites');
function remove_favorites($actions) {
		$actions = array(); 
		return $actions;
}

Add More Date/Time Format Options

If you find that you need to offer some non-standard date or time formats to the general settings page, this will help.

add_filter('date_formats', 'add_date_format');
function add_date_format($original) {
        $more = array('D, M jS, Y');
        return array_merge($original, $more);
                        
}

add_filter('time_formats', 'add_time_format');
function add_time_format($original) {
        $more = array('H:i:s a');
        return array_merge($original, $more);
                        
}

Remove “Generator” Tags

Don’t want those “generator” tags in your source? Add this

remove_action( 'wp_head', 'wp_generator' );

To modify the output, you can run in through a filter. In this case, the modification is removal.

add_filter('the_generator', 'remove_generated_by');
function remove_generated_by($footer_text) {
    return '';
}

If you want to be more specific about where the generator tags are are removed, you can change the filter to something like on of these

add_filter('get_the_generator_xhtml', 'remove_generated_by');

Just swap ‘xhtml’ for one of the following as needed: html, xhtml, atom, rss2, rdf, comment, export.

Custom Post Types

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