We have collected a useful list of 10+ WordPress Code Snippets that we believe you will find really useful especially if you develop sites like we do. These snippets are added directly to a themes functions.php file in order to add additional functionality to your site.

It’s being more than decade for us now to use WordPress and within all these years we worked on number of WordPress projects. While working on all these projects, we have been able to build up a collection of little snippets and tricks that helps us from time to time.

10+ Useful WordPress Code Snippets

All the snippets we’ve been adding have been tested with the WordPress 3.6 version. Based on all the experimentation’s we did, you will find the collection of WordPress Code Snippets, that we used for our WordPress projects.

Remove Comments menu from Admin Dashboard

// Remove Comments menu from Admin Dashboard
function remove_menus () {
global $menu;
$restricted = array(__('Comments'));
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}
}
add_action('admin_menu', 'remove_menus');

Source

Remove Dashboard Widgets

// Remove Dashboard Widgets
function remove_dashboard_widgets() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
if (!current_user_can('manage_options')) {
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
}

Source

Remove Metaboxes from Custom Post Type Edit Screens

// Remove Metaboxes from Custom Post Type Edit Screens
if (is_admin()) :
function remove_post_meta_boxes() {
if(!current_user_can('administrator')) {
remove_meta_box('tagsdiv-post_tag', 'custom_post_type', 'normal');
remove_meta_box('categorydiv', 'custom_post_type', 'normal');
remove_meta_box('authordiv', 'custom_post_type', 'normal');
remove_meta_box('postexcerpt', 'custom_post_type', 'normal');
remove_meta_box('trackbacksdiv', 'custom_post_type', 'normal');
remove_meta_box('commentstatusdiv', 'custom_post_type', 'normal');
remove_meta_box('postcustom', 'custom_post_type', 'normal');
remove_meta_box('commentstatusdiv', 'custom_post_type', 'normal');
remove_meta_box('commentsdiv', 'custom_post_type', 'normal');
remove_meta_box('revisionsdiv', 'custom_post_type', 'normal');
remove_meta_box('authordiv', 'custom_post_type', 'normal');
remove_meta_box('pageparentdiv', 'custom_post_type', 'normal');
remove_meta_box('slugdiv', 'custom_post_type', 'normal');
}
}
add_action('admin_menu', 'remove_post_meta_boxes');
endif;

Source

Remove Featured Image Metabox from Custom Post Type Edit Screens

// Remove Featured Image Metabox from Custom Post Type Edit Screens
function remove_image_box() {
if ($current_user->user_level < 10){
remove_meta_box('postimagediv','custom_post_type','side');
}
}
add_action('do_meta_boxes', 'remove_image_box');

Source

Remove Dynamic Widget Metabox from Custom Post Type Edit Screens

// Remove Dynamic Widget Metabox from Custom Post Type Edit Screens
function remove_dynwid_box() {
if ($current_user->user_level < 10){
remove_meta_box('dynwid','custom_post_type','side');
}
}
add_action('do_meta_boxes', 'remove_dynwid_box');

Source

Remove editor only for author based on Custom Post Types

// Remove editor only for author based on Custom Post Types
function remove_editor() {
if (current_user_can('author')){
remove_post_type_support('custom_post_type','editor');
}
}
add_action('admin_init','remove_editor');

Source

Remove Edit Title, Add new, Permalink, View Post buttons from Custom Post Type Edit Screens

// Remove Edit Title, Add new, Permalink, View Post buttons from Custom Post Type Edit Screens
function posttype_admin_css() {
if (current_user_can('author')){
global $post_type;
if($post_type == 'custom_post_type') {
echo '<style type="text/css">#titlediv,.add-new-h2,#edit-slug-box,#view-post-btn,.updated p a{display: none;}</style>';
}
}
}
add_action('admin_head', 'posttype_admin_css');

Source

Remove Items from admin bar for authors

// Remove Items from admin bar for authors
function remove_admin_bar_links() {
if (current_user_can('author')){
global $wp_admin_bar;
$wp_admin_bar->remove_menu('new-content'); // Remove Add New
$wp_admin_bar->remove_menu('comments'); // Remove Comments
$wp_admin_bar->remove_menu('wpseo-menu'); // Remove Yoast SEO links menu
$wp_admin_bar->remove_menu('documentation'); // Remove the WP documentation link
$wp_admin_bar->remove_menu('support-forums'); // Remove the support forums link
$wp_admin_bar->remove_menu('feedback'); // Remove the feedback link
}
}
add_action('wp_before_admin_bar_render', 'remove_admin_bar_links');

Source

Remove Posts menu for Editor, Author, and Contributor

// Remove Posts menu for Editor, Author, and Contributor
if (! current_user_can('manage_options')) {
add_action('admin_menu', 'notadmin_remove_menus', 999);
function notadmin_remove_menus() {
remove_menu_page('edit.php'); // Posts
remove_menu_page('tools.php'); // Tools
remove_submenu_page('edit.php?post_type=custom_post_type', 'post-new.php?post_type=custom_post_type'); // Custom Post Type
}
}

Source

Add new dashboard widget

// Add new dashboard widget
function new_dash_widget_function() { ?>
<p>Some text or Html.</p>
<?php }
function add_dashboard_widgets() {
wp_add_dashboard_widget('new_dash_widget', 'Your_Widget_name', 'new_dash_widget_function');
}
add_action('wp_dashboard_setup', 'add_dashboard_widgets');

Source

Trim AJAX Calendar Future POSTS to be valid

// Trim AJAX Calendar Future POSTS to be valid
add_filter('get_calendar', 'customize_calendar_output');
function customize_calendar_output($calendar_output) {
$calendar_output = str_replace('<table id="wp-calendar" summary="' . esc_attr__('Calendar') . '">', '<table id="wp-calendar">', $calendar_output);
return $calendar_output;
}

Source

Trim posts with status future like post status publish

// Trim posts with status future like post status publish
function custom_archive_query($query) {
if ( $query->is_archive() )
{
$query->set('post_status', array('future', 'publish'));
}
return $query;
}
add_filter('pre_get_posts', 'custom_archive_query');

Source


* Remember to backup your template files and/or database before performing any edits. Take care!!! :)

David

Graphic and website designer, currently, I attend to the maintenance of about thirty sites for existing clients as well as creating websites for the new ones. In my free time, I get into the secrets of the world of Linux, SSH, WP-CLI and I’m preparing this blog.

More:

7 Ways To Boost A Websites Rankings & Traffic... How to boost a websites rankings & traffic? This Infographic was designed to cut through the noise a bit. There is a ton of information out there on w...
10 Free Graphic Design Resources by Mighty Deals MightyDeals - best designer resources at great discounts for graphic, web and creative designers. These free resources are available just for an e-mai...
Top 11 Features of Windows 8 Dot Com Infoway (DCI), has created yet another infographic illustrating the best features of Windows 8. “Top 11 Features of Windows 8” is the 5th in a...
10+ Fresh Free WordPress Themes from 2017 Let's take a look at the selection of the best WordPress themes for your next project. These themes are highly customizable so you can adapt them to y...
Site Development With WordPress and HTML5 Site Development with WordPress and HTML5 is the new concept for developers and programmers in favor of adding photo galleries, videos and many multim...

This Post Has One Comment

  1. Pingback: 10+ Useful WordPress Code Snippets | Design News

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Newsletter

Join our mailing list to receive the latest news and updates.

You have Successfully Subscribed!

Pin It on Pinterest