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.

Remove IM Fields From Profile Page

Need to declutter the profile options by removing the various IM fields. It’s easy

add_filter('user_contactmethods', 'remove_im_options', 10, 2); 
function remove_im_options( $user_contactmethods, $user ) {
    unset($user_contactmethods['aim']);
    unset($user_contactmethods['yim']);
    unset($user_contactmethods['jabber']);
    //$user_contactmethods = array();
    return $user_contactmethods;
}

Remove Library Tab from Post-Side Media Pop-Up

I’m working on a plugin right now where I’m having to hide or limit a lot of features depending on the current user’s capabilities. Among those limitations is media uploading and editing access. I’m using this snippet to hide the Media Library tab from the Add an Image pop-up on the edit post page.

add_filter('media_upload_tabs','no_library_tab');
function no_library_tab($tabs) {
    if (!current_user_can('promote_users')) { 
        unset($tabs['library']);
    }
    return $tabs;
}

Prevent Option From Being Updated

If you need to hand over the reins to a client, but want to make sure certain options stay unchanged, you can add this code:

add_filter( 'pre_update_option_admin_email', 'prevent_option_change', 10, 2 ); 
function prevent_option_change( $newvalue, $oldvalue ) {
    return $oldvalue;
}

In this case, I’m preserving ‘admin_email’ – just change that part of the filter hook to preserve another option.

Add Extra Content to the Widgets Admin

I don’t have a good example of why you might want or need this, but nevertheless, if you should want to add additional text to the Widgets admin, it’s pretty darn easy:

add_action( 'widgets_admin_page', 'widgets_admin_info' );
function widgets_admin_info() {
    ?>
    <p style="clear:both;">Hello there!!</p>
    <?php
}

Easier Access to Media File from Media Library

Maybe it’s just me, but sometimes I’d link to grab an image’s file URL from the Media Library, but I can’t do it easily. This little snippet will add a link to the ‘row actions’ (“Edit | Delete Permanently | View”) for the File URL (as opposed the the attachment URL you’ll get with the ‘view’ link)

add_filter ('media_row_actions','add_direct_link', 10, 3 );
function add_direct_link( $actions, $post, $detached ) {
	$actions['file_url'] = '<a href="' . wp_get_attachment_url($post->ID) . '">Actual File</a>';
	return $actions;
}

Customize Post Views

Depending on your role and capabilities, you may see something like this on your posts/pages screen “mine () | all () | published ()”

Those items are editable.

In my case, I wanted ‘authors’ to only have access to the ‘mine’ list, since ‘mine’ is default, I wanted to remove the views list entirely, but only for non-administrators.

add_filter('views_edit-post', 'only_show_mine');
function only_show_mine( $views ) {
    if (!current_user_can('promote_users')) {
        $views = array(); //remove all options
        //unset($views['all']);
        //unset($views['publish']);
    }
return $views;
}