Taking Control of WordPress 3.8 Admin Bar

What is WordPress Admin Bar?

WordPress admin bar was introduced in WordPress 3.1. The purpose of the WordPress Admin Bar was to connect the backend (dashboard) of a WordPress to the Live. It always shows up right below the browser address bar and looks somewhat like below image:

wordpress admin nav

The WordPress Admin Bar helps webmasters switch to the basic features of the Dashboard directly from their WordPress front-end. Although the presence of the WordPress Admin Bar is a boon, most webmasters don’t like the presence of anything sitting on our sites, I guess the 1-click access to the WordPress Dashboard isn’t what everybody is comfortable with.

Why an Admin Bar?

“First step towards a front-end editor” – Matt Mullenweg, the man who gave birth to WordPress.

The WordPress admin bar has been tagged as the first step towards bringing the WordPress editor right into your site and moving it out of the Dashboard. It will be interesting to see how WordPress incorporates the Dashboard features into our blog’s front end without making it tacky. For now, Admin Bar is here and needs a lot of improvements.

Remove Links from the WordPress Admin Bar

There is lot of things, That you want to remove from the WordPress Admin Bar. Before we start with adding new links to the WordPress Admin Bar (which is a bit complex) we will start removing remove links from the WordPress Admin Bar.

Remove Author Name Menu or just sub-parts

The very first drop down menu in the WordPress Admin Bar is the name of author. It will have two options – Edit Profile and Logout. You can remove this complete menu (or just the sub-parts) by inserting the below code in

functions.php

of your current theme:

<?php

function editMyAdminBar() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('my-account-with-avatar');
    // This will hide the menu item with author name.

    $wp_admin_bar->remove_menu('edit-profile');
    // This will remove the sub-part "Edit My Profile".

    $wp_admin_bar->remove_menu('logout');
    // This will remove the sub-part "Logout".
}

add_action('wp_before_admin_bar_render', 'editMyAdminBar');
?>

Next is the Dashboard link in the WordPress Admin Bar. Insert below code in

functions.php

:

<?php

function editMyAdminBar() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('dashboard');
}

add_action('wp_before_admin_bar_render', 'editMyAdminBar');
?>

Next is the Add New link which has lot of sub items too and we have some other menus in our admin bar. Use the below code to remove all or sub items:

<?php

function editMyAdminBar() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('new-content');
    // This removes the complete menu “Add New”. You will not require the below “remove_menu” if you using this line.

    $wp_admin_bar->remove_menu('new-post');
    // This will hide the menu item “Post”.

    $wp_admin_bar->remove_menu('new-page');
    // This will hide the menu item “Page”.

    $wp_admin_bar->remove_menu('new-media');
    // This will hide the menu item “Media”.

    $wp_admin_bar->remove_menu('new-link');
    // This will hide the menu item “Link”.

    $wp_admin_bar->remove_menu('new-user');
    // This will hide the menu item “User”.

    $wp_admin_bar->remove_menu('new-theme');
    // This will hide the menu item “Theme”.

    $wp_admin_bar->remove_menu('new-plugin');
    // This will hide the menu item “Plugin”.

    $wp_admin_bar->remove_menu('comments');
    // This will hide the menu “Comments”.

    $wp_admin_bar->remove_menu('appearance');
    // This removes the complete menu “Appearance”. You will not require the below “remove_menu” if you using this line.

    $wp_admin_bar->remove_menu('new-themes');
    // This will hide the menu item “Themes”.

    $wp_admin_bar->remove_menu('new-widgets');
    // This will hide the menu item “Widgets”.

    $wp_admin_bar->remove_menu('new-menus');
    // This will hide the menu item “Menus”.

    $wp_admin_bar->remove_menu('updates');
    // This will hide the menu “Updates”.

    $wp_admin_bar->remove_menu('get-shortlink');
    // This will hide the menu “Shortlink”.
}

add_action('wp_before_admin_bar_render', 'editMyAdminBar');
?>

Now we are moving to Add Links to WordPress Admin Bar

Most of sites have a link to the homepage, if you want to add one then you can use the below code in your

functions.php

:

<?php
function add_new_item_to_adminbar() {
    global $wp_admin_bar;
    $wp_admin_bar->add_menu(array(
        'id' => 'view-site', //the view-site ID that refers to what we are doing.
        'title' => __('Visit Site'), //the anchor text that links to homepage.
        'href' => site_url())); //the homepage URL to which the anchor text will connect.
}

add_action('wp_before_admin_bar_render', 'add_new_item_to_adminbar');
?>

Add sub-menu item in “Add New” menu

This will be interesting. We will add a new item inside a menu that already exists. The below code will add a My Media option in the menu Add New.

<?php
function add_submenu_to_adminbar() {
    global $wp_admin_bar;
    $wp_admin_bar->add_menu( array(
        'parent' => 'new-content',
        // new-content is the ID for “Add New” menu so we use it as parent ID.
        
        'id' => 'new_media', 
        // You can add any value here as you are adding something very new here.
        
        'title' => __('Media'), // The anchor text.
        
        'href' => admin_url( 'media-new.php') 
        // name of file to which you will link to.
        ));
}
add_action( 'wp_before_admin_bar_render', 'add_submenu_to_adminbar' );
?>

Hope! this is a good tutorial for all my blog reader, If any one have any query’s and error please put your comments bellow.

How to Control WordPress Custom Post Types, Capabilities and Roles

As We all knows about Custom Post Types and how to create them, when we create custom post types without Capabilities all users except subscriber roles can publish, read, edit and delete them by default. What if we wanted to create a custom post type that Author could read, edit and publish but Author Staff could only read? Well here’s were custom post types and capabilities gets interesting! Take the following basic custom post type for registering Article:

<?php 
add_action( 'init', 'register_article_post' );

function register_article_post() {

    $labels = array( 
        'name' => _x( 'Articles', 'article' ),
        'singular_name' => _x( 'Article', 'article' ),
        'add_new' => _x( 'Add New', 'article' ),
        'add_new_item' => _x( 'Add New Article', 'article' ),
        'edit_item' => _x( 'Edit Article', 'article' ),
        'new_item' => _x( 'New Article', 'article' ),
        'view_item' => _x( 'View Article', 'article' ),
        'search_items' => _x( 'Search Articles', 'article' ),
        'not_found' => _x( 'No articles found', 'article' ),
        'not_found_in_trash' => _x( 'No articles found in Trash', 'article' ),
        'parent_item_colon' => _x( 'Parent Article:', 'article' ),
        'menu_name' => _x( 'Articles', 'article' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'supports' => array( 'title', 'editor' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
    );
    register_post_type( 'article', $args );
}
?>

The above example would give all users (above subscriber) full capabilities over the custom post type Article, like all can read, publish, edit and delete them. When we add ‘map_meta_cap, capabilities and capability_type’ to the $args array we can take more control over it.

Lets modify the $args first by adding map_meta_cap capabilities, and capability_type:

<?php 
add_action( 'init', 'register_article_post' );

function register_article_post() {

    $labels = array( 
        'name' => _x( 'Articles', 'article' ),
        'singular_name' => _x( 'Article', 'article' ),
        'add_new' => _x( 'Add New', 'article' ),
        'add_new_item' => _x( 'Add New Article', 'article' ),
        'edit_item' => _x( 'Edit Article', 'article' ),
        'new_item' => _x( 'New Article', 'article' ),
        'view_item' => _x( 'View Article', 'article' ),
        'search_items' => _x( 'Search Articles', 'article' ),
        'not_found' => _x( 'No articles found', 'article' ),
        'not_found_in_trash' => _x( 'No articles found in Trash', 'article' ),
        'parent_item_colon' => _x( 'Parent Article:', 'article' ),
        'menu_name' => _x( 'Articles', 'article' ),
    );

    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'supports' => array( 'title', 'editor' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,

        'map_meta_cap' => true,
        // map_meta_cap will allow us to remap the existing capabilities with new capabilities to match the new custom post type

        // capabilities are what we are customising so lets remap them
        'capabilities' => array(
            'edit_post' => 'edit_article',
            'edit_posts' => 'edit_articles',
            'edit_others_posts' => 'edit_other_articles',
            'publish_posts' => 'publish_articles',
            'edit_publish_posts' => 'edit_publish_articles',
            'read_post' => 'read_articles',
            'read_private_posts' => 'read_private_articles',
            'delete_post' => 'delete_article'
        ),
        // capability_type defines how to make words plural, by default the
        // second word has an 's' added to it and for 'article' that's fine
        // however when it comes to words like book the plural would become
        // books so it's worth adding your own regardless of the plural.
        'capability_type' => array('article', 'articles'),
    );
    register_post_type( 'article', $args );
}
?>

So now we have a Custom Post Type with Capabilities remapped to our own custom Capabilities with our own plural words. All we have to do now is add the capability to the roles.

Look at bellow example:

<?php 
function manage_capabilities() {
    // gets the role to add capabilities to
    $admin = get_role( 'administrator' );
    $editor = get_role( 'editor' );
	// replicate all the remapped capabilites from the custom post type article
    $caps = array(
    	'edit_article',
    	'edit_articles',
    	'edit_other_articles',
    	'publish_articles',
    	'edit_published_articles',
    	'read_articles',
    	'read_private_articles',
    	'delete_article'
    );
    // give all the capabilities to the administrator
    foreach ($caps as $cap) {
	    $admin->add_cap( $cap );
    }
    // limited the capabilities to the editor or a custom role 
    $editor->add_cap( 'edit_article' );
    $editor->add_cap( 'edit_articles' );
    $editor->add_cap( 'read_articles' );
}
add_action( 'admin_init', 'manage_capabilities');
?>

I think this blog post is really helpful for my blog reader’s, if any one have any query’s then please put your comments

WordPress 3.8 arrives with redesigned dashboard, new theme and widget screens, vector-based icons, and many more

WordPress.org announced the debut of WordPress 3.8, which introduces a modern new design that the company calls its “most beautiful update yet.” You can download the new release now from WordPress.org/Download (it’s 6.10MB).

Dubbed “Parker” in honor of American jazz saxophonist and composer Charlie Parker, WordPress 3.8 brings a new look to the entire admin dashboard.

WordPress 3.8, WordPress King Praveen Singh Shekhawat

The new features are as follows:

  • Modern aesthetic: The new WordPress dashboard has a fresh, uncluttered design that embraces clarity and simplicity.
  • Clean typography: The Open Sans typeface provides simple, friendly text that is optimized for both desktop and mobile viewing. It’s even open source, just like WordPress.
  • Refined contrast: With superior contrast and large, comfortable type, the new design is easy to read and a pleasure to navigate.
  • High definition at high speed: WordPress is sharper than ever with new vector-based icons that scale to your screen. By ditching pixels, pages load significantly faster, too.
  • Admin color schemes: WordPress now includes eight new admin color schemes, which can be previewed and changed from your Profile page.
  • Refined theme management: The new themes screen lets you survey your themes at a glance. Click to add more information, and use your keyboard’s navigation arrows to flip through themes.
  • Smoother widget experience: The widgets screen has been streamlined. On large monitors, multiple widget areas stack side-by-side to use the available space while on tablets, just tap a widget to add it.
  • Twenty Fourteen: Turn your blog into a magazine with this new theme. Choose a grid or a slider to display featured content on your homepage. Customize your site with three widget areas or change your layout with two page templates.

WordPress 3.8 Colors, WordPress King Praveen Singh Shekhawat

Disable Post Revisions in WordPress and Reduce Database Size

Post revisions is a great feature that WordPress has to offer but for some users it might not be the best option. For users who have limited database space, or for users creating WordPress as a CMS where they don’t need autosave, they can simply disable Post Revisions feature in WordPress by following this tutorial. By reducing the database size, you can also speed up your site.

Open wp-config.php located in your WordPress root directory and add the following

 define('AUTOSAVE_INTERVAL', 600 ); // seconds
define('WP_POST_REVISIONS', false ); 

This code will disable all future revisions to be saved and it will also increase your autosave interval from 60 seconds to 600 seconds, so it means your post will be autosaving every 10 minute instead of every minute.

The above code, will not delete your past revisions that are already saved in your database. To delete all previous revisions Click >>

How To Create A Shortcode

WordPress makes it pretty simple to make your own shortcodes, so let’s create a shortcode for print a “hello” text. Open your function file and copy the below php code and past into function.php file.

function MyShortCode()
{
echo "Hello";
}
add_shortcode('mycode', 'MyShortCode');

Now use the [mycode] for print “hello”

Add New Widgets Area To Your Theme.

This is one of the most useable thing in wordpress. For adding new widget area to your theme paste the following code in your functions.php file:

if ( function_exists('register_sidebar') ) :
register_sidebar(array('name'=>'MySidebar',
'before_widget' => '<li class="widget">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h3>',
));
endif;

Above code will register a sidebars. You can register as many as you need. To display the sidebar in your theme, simply paste the code where you like:

<?php dynamic_sidebar('MySidebar'); ?>

Add / Remove Author Profile Fields in WordPress

Add Fields to wordpress profile.

If you want to add some additional fields to author profile page, then you would need to copy the below code and past into theme’s function.php file.

function NewProfileFields( $jobFields ) {
// Add Job 
$jobFields['job'] = 'Job';
//add Job Location
$jobFields['job_location'] = 'Job Location';

return $contactmethods;
}
add_filter('user_contactmethods','NewProfileFields',10,1);

Remove Fields from wordpress profile.

If you want to remove any fields such as AIM, Yahoo IM, Jabber etc. from the profile then simply paste the following code into function.php file.

 
function HideProfileFields( $MyFields ) {
  unset($MyFields['aim']);
  unset($MyFields['jabber']);
  unset($MyFields['yim']);
  return $MyFields;
}
add_filter('user_contactmethods','HideProfileFields',10,1);