List Category ID in Quick Actions

One thing I’ve found frustrating is that there doesn’t seem to be an easy way of getting a category (or other term) ID. This little snippet will add it to the “quick actions” that appear when you mouseover a term’s row.

add_filter( "tag_row_actions", 'add_cat_id_to_quick_edit', 10, 2 );
function add_cat_id_to_quick_edit( $actions, $tag ) {
	$actions['cat_id'] = 'ID: '.$tag->term_id;
	return $actions;
}

Remove the Media Library Tab

I’ll leave the ‘why’ to you, but here’s the ‘how’

add_filter( 'media_upload_tabs', 'no_media_library_tab' );
function no_media_library_tab( $tabs ) {
    unset($tabs['library']);
    return $tabs;
}

Okay, one possible reason would be that you want to restrict users from accessing media that’s been attached to another post.

Add Copy to the Add New Tag (or other Taxonomy) Page

Maybe you need to add some extra text to the page to explain to a user why they should take a minute to spellcheck their new category or tag. Or perhaps you want to want to encourage them to check if a certain category already exists as a plural or singular from. You can add this extra copy in at least 2 places on the various taxonomy admin pages.

add_action( 'after-' . 'category' . '-table', 'copy_below_table' );
function copy_below_table( $taxonomy ) {
    echo '<p>Lorem ipsum</p>';
}

add_action( 'category' . '_pre_add_form', 'copy_above_form' );
function copy_above_form( $taxonomy ) {
    echo '<p>Dolor sit amet</p>';
}

Add or Change Content in the Featured Image Meta Box

Perhaps you need to provide a little extra instruction about the Featured Image meta box. It doesn’t take much to add your own text to that box:

add_filter( 'admin_post_thumbnail_html', 'add_featured_image_instruction');
function add_featured_image_instruction( $content ) {
    return $content .= '<p>The Featured Image is an image that is chosen as the representative image for Posts or Pages. Click the link above to add or change the image for this post. </p>';
}

Remove Categories Meta Box

Sometimes, the ability to add multiple categories to a single post can cause problems. Or maybe you’re working with custom taxonomies and you need to make sure only one term is assigned to a post. Assuming you’re creating your own alternative category selection box, you can use this to get red of the default meta box.

add_action('admin_init','remove_cat_box');
function remove_cat_box() {
    //args: $categoryslug.'div', $posttype, $location
    remove_meta_box('categorydiv', 'post', 'side');
}

Shortcode with Attributes

For a variety of reasons, you may not be able to add content directly to the WordPress editor. Maybe it’s funky code that’ll get stripped out, or you want a random quote to show up, or it’s some other kind of dynamic content.

That’s what shortcodes are good for! Here’s a simple one that creates a ‘youtube’ shortcode for embedding videos in your post.

add_shortcode( 'youtube', 'youtube_shortcode' );
function youtube_shortcode( $atts ) {
    extract( shortcode_atts( array('id' => '', 'width' => 480, 'height' => 390) , $atts ) );
    $id = trim($id);
    if (empty($id)) return;
    return '<iframe title="YouTube video player" width="'. $width .'" height="'. $height .'" src="http://www.youtube.com/embed/'. $id .'" frameborder="0" allowfullscreen></iframe>';
}

Used in your post like this: . The width and height attributes will default to 480 and 390 respectively if not specified in the shortcode. Quotes around the attribute value are not required unless your value has a space.