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.
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');
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'); }
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;
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');
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');
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');
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');
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');
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; }
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!!! :)
0 Comments