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.

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

Hide Admin Bar

If you’re one of those that don’t love the admin bar, here’s how to disable it, as well as the option to show/hide it.

add_filter('show_admin_bar', '__return_false');
add_action( 'admin_print_scripts-profile.php', 'hide_admin_bar_prefs' );
function hide_admin_bar_prefs() {
    ?><style type="text/css">.show-admin-bar {display:none;}</style><?php 
}

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.

Define A Default Excerpt

I can’t actually think of an appropriate time and place for this, so if you know of one, please share. But the point is, if you want to create a default excerpt to be used on all new posts, here’s how to set that up.

add_filter('default_excerpt','setup_default_excerpt');
function setup_default_excerpt($excerpt) {
	$excerpt .= 'Put your default excerpt here.';
	return $excerpt;
}

Extra Username Validation

Run a family-friendly site and want to make sure no inappropriate words end up in usernames? Use this

add_filter( 'validate_username', 'forbidden_username_terms', 10, 2);
function forbidden_username_terms($valid, $username) {
	$badwords = array('badword','nogood');
	foreach($badwords as $bw) {
		if (strpos($username,$bw) !== false) return false; 
	}
	return $username;
}