WordPress Function File Snippets


Below is a collection of WordPress snippets which will allow you to build custom Dashboard Login themes, restrict user permissions / menu visbility and a lot more.

1) Remove Metaboxes:
	function remove_post_custom_fields() {
		remove_meta_box('postexcerpt', 'post', 'normal'); // removes excerpt metabox
		remove_meta_box('trackbacksdiv', 'post', 'normal'); // removes trackbacks metabox
		remove_meta_box('commentstatusdiv', 'post', 'normal'); // removes discussion metabox
		remove_meta_box('postcustom', 'post', 'normal'); // removes custom metaboxes (other than defined here)
		remove_meta_box('commentsdiv', 'post', 'normal'); // removes comments metabox
		remove_meta_box('revisionsdiv', 'post', 'normal'); // removes revision metabox
		remove_meta_box('authordiv', 'post', 'normal'); // removes author metabox
		remove_meta_box('sqpt-meta-tags', 'post', 'normal'); // removes  metabox
		remove_meta_box('categorydiv', 'post', 'normal'); // removes categories metabox
		remove_meta_box('slugdiv', 'post', 'normal'); // removes slugs metabox
		remove_meta_box('formatdiv', 'post', 'normal'); // removes formats metabox
		remove_meta_box('tagsdiv-post_tag', 'post', 'normal'); // removes tags metabox
		remove_meta_box('pageparentdiv', 'post', 'normal'); // removes attributes metabox
	}
	add_action( 'admin_menu' , 'remove_post_custom_fields' );


2) Change WordPress Admin Default Color Scheme:

	function set_default_admin_color($user_id) {
		$args = array(
			'ID' => $user_id,
			'admin_color' => 'midnight'
	);
	wp_update_user( $args );
	}
	add_action('user_register', 'set_default_admin_color');​


3) Remove Dashboard Widgets:

	function disable_default_dashboard_widgets() {
		//remove_meta_box('dashboard_right_now', 'dashboard', 'core');
		remove_meta_box('dashboard_recent_comments', 'dashboard', 'core');
		remove_meta_box('dashboard_incoming_links', 'dashboard', 'core');
		remove_meta_box('dashboard_plugins', 'dashboard', 'core');
		remove_meta_box('dashboard_quick_press', 'dashboard', 'core');
		remove_meta_box('dashboard_recent_drafts', 'dashboard', 'core');
		remove_meta_box('dashboard_primary', 'dashboard', 'core');
		remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');
		remove_action( 'welcome_panel', 'wp_welcome_panel');
		remove_meta_box('dashboard_custom_feed', 'dashboard', 'core');
		remove_meta_box('woocommerce_dashboard_recent_reviews', 'dashboard', 'core');
	}
		add_action('admin_menu', 'disable_default_dashboard_widgets');​


4) 'Ronco Support' Dashboard Widget:

	add_action('wp_dashboard_setup', 'ronco');
	function ronco() {
	wp_add_dashboard_widget('ronco_help', 'For Help or Customizations', 'ronco_help');
	}
	function ronco_help() {
	echo '<p style="font-size: 14px;">If you need any further assistance feel free to contact us:</p> <p style="color: red;font-size: 14px;">Web: <a href="http://www.roncocala.com/contact-us/" target="_blank" style="text-decoration: bold; font-weight:strong;">Ronco Cayman</a></h2>
	<p style="color: red;font-size: 14px;">Email: <a href="mailto:support@roncocala.com">support@roncocala.com</a></p> <p style="color: red;font-size: 14px;">Phone: <a href="tel:3459167253">24/7 Support - (345) 916-7253</a></p>';
	}


5) 'Ronco Style' Footer:

	function remove_footer_admin () {
	echo 'Powered by <a href="http://www.wordpress.org" target="_blank">WordPress</a> &nbsp;&#x272D;&nbsp; Created by: <img src="http://www.islanddog.ky/wp-content/uploads/2016/11/ronc-16x16.png" alt="Ronco Metrics" /> <a href="http://www.roncocala.com" target="_blank">Ronco Metrics</a>.&nbsp; &nbsp;</p>';
	}
	add_filter('admin_footer_text', 'remove_footer_admin');

	add_filter( 'update_footer', function () {
		return '';
 }, 999);


6) Remove Un-needed UI Elements:

	function remove_some_wp_widgets () {
		unregister_widget('WP_Widget_Calendar');
		unregister_widget('WP_Widget_Search');
		unregister_widget('WP_Widget_Recent_Comments');
		unregister_widget('WP_Widget_Pages');
		unregister_widget('WP_Widget_Archives');
		unregister_widget('WP_Widget_Links');
		unregister_widget('WP_Widget_Meta');
		unregister_widget('WP_Widget_Categories');
		unregister_widget('WP_Widget_Recent_Posts');
		unregister_widget('WP_Widget_Tag_Cloud');
		unregister_widget('WP_Nav_Menu_Widget');
	}
	add_action('widgets_init','remove_some_wp_widgets', 1);


7) Remove WooCommerce (2.6.11) Widgets: 

function remove_woo_widgets() {
		unregister_widget( 'WC_Widget_Recent_Products' );
		unregister_widget( 'WC_Widget_Featured_Products' );
		unregister_widget( 'WC_Widget_Product_Categories' );
		unregister_widget( 'WC_Widget_Product_Tag_Cloud' );
		unregister_widget( 'WC_Widget_Cart' );
		unregister_widget( 'WC_Widget_Layered_Nav' );
		unregister_widget( 'WC_Widget_Layered_Nav_Filters' );
		unregister_widget( 'WC_Widget_Price_Filter' );
		unregister_widget( 'WC_Widget_Product_Search' );
		unregister_widget( 'WC_Widget_Top_Rated_Products' );
		unregister_widget( 'WC_Widget_Recent_Reviews' );
		unregister_widget( 'WC_Widget_Recently_Viewed' );
		unregister_widget( 'WC_Widget_Best_Sellers' );
		unregister_widget( 'WC_Widget_Onsale' );
		unregister_widget( 'WC_Widget_Random_Products' );
}

		remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );


8) Remove Upgrade Notifications to Non-Admins

	if ( !current_user_can( 'edit_users' ) ) {
  		add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
  		add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}


	global $user_login;
	get_currentuserinfo();
 
	if (!current_user_can('update_plugins'))
 	{ // checks to see if current user can update plugins
 	add_action('init', create_function('$a', "remove_action( 'init', 'wp_version_check' );") , 2);
 	add_filter('pre_option_update_core', create_function('$a', "return null;"));
 	}


9) 'Ronco Style' Custom WP-Login Screen*

	function my_custom_login() {
		echo '<link rel="stylesheet" type="text/css" href="' . get_bloginfo('stylesheet_directory') . '/login/ronco-login.css" />';
	}
	add_action('login_head', 'my_custom_login');

	function my_login_logo_url() {
		return get_bloginfo( 'url' );
	}
	add_filter( 'login_headerurl', 'my_login_logo_url' );

	function my_login_logo_url_title() {
	return 'Ronco - Login';
	}
	add_filter( 'login_headertitle', 'my_login_logo_url_title' );


10) 'Ronco Style' Dashboard Logo*

	add_action('admin_head', 'ronco_logo');

	function ronco_logo() {
	echo '
	<style type="text/css">
	#header-logo { background-image: url('.get_bloginfo('template_directory').'/images/ronco.gif) !important; }
	</style>
	';
	}


10) Remove WordPress Default Admin Bar Links

add_action( 'admin_bar_menu', function ( $admin_bar ) {
  $admin_bar->remove_menu( 'wp-logo' );          // Remove the WordPress logo
    $admin_bar->remove_menu( 'about' );            // Remove the about WordPress link
    $admin_bar->remove_menu( 'wporg' );            // Remove the about WordPress link
    $admin_bar->remove_menu( 'documentation' );    // Remove the WordPress documentation link
    $admin_bar->remove_menu( 'support-forums' );   // Remove the support forums link
    $admin_bar->remove_menu( 'feedback' );         // Remove the feedback link
  $admin_bar->remove_menu( 'updates' );          // Remove the updates link
  $admin_bar->remove_menu( 'comments' );         // Remove the comments link
  $admin_bar->remove_menu( 'new-content' );      // Remove the content link

}, 999); // Needs to have low priority


This list will be improved on over time to update snippets or add new ones. Please note 9-10 require the logo, style and other files to be located in your Child Theme to function correctly.


Did you find this article useful?