Search posts by ID in admin

Not sure i understand why you’d want to query by ID, but that said it’s possible in a hacky kind of way(i like this method because it’s simple). add_action( ‘parse_request’, ‘idsearch’ ); function idsearch( $wp ) { global $pagenow; // If it’s not the post listing return if( ‘edit.php’ != $pagenow ) return; // If … Read more

if admin is logged in

current_user_can will accept a role name but, sadly, the behavior with roles is not entirely consistent. The following should work and is simpler than what you have, by a little bit. $current_user = wp_get_current_user(); if (user_can( $current_user, ‘administrator’ )) { // user is an admin }

Admin: very slow edit page caused by core meta query

If you want to test your custom SQL to see how it affects the loading time, you can try this query swapping: /** * Restrict the potential slow query in the meta_form() to the current post ID. * * @see http://wordpress.stackexchange.com/a/187712/26350 */ add_action( ‘add_meta_boxes_post’, function( $post ) { add_filter( ‘query’, function( $sql ) use ( … Read more

How to pass parameters to admin_notices?

I think a better implementation would be a “message” class e.g.: class WPSE_224485_Message { private $_message; function __construct( $message ) { $this->_message = $message; add_action( ‘admin_notices’, array( $this, ‘render’ ) ); } function render() { printf( ‘<div class=”updated”>%s</div>’, $this->_message ); } } This allows you to instantiate the message at any time prior to rendering: … Read more

How do I remove dashboard access from specific user roles?

To lock subscribers and contributors out of the admin: function wpse23007_redirect(){ if( is_admin() && !defined(‘DOING_AJAX’) && ( current_user_can(‘subscriber’) || current_user_can(‘contributor’) ) ){ wp_redirect(home_url()); exit; } } add_action(‘init’,’wpse23007_redirect’); Hope that helps. All roles give the user a capability that is the name of that role, so you can use any role name as a capability.

How to remove entire admin menu?

The correct hook to use is admin_menu and then create a function to remove the menus you want to remove. The following 2 functions remove all the menus. add_action( ‘admin_menu’, ‘remove_admin_menus’ ); add_action( ‘admin_menu’, ‘remove_admin_submenus’ ); //Remove top level admin menus function remove_admin_menus() { remove_menu_page( ‘edit-comments.php’ ); remove_menu_page( ‘link-manager.php’ ); remove_menu_page( ‘tools.php’ ); remove_menu_page( ‘plugins.php’ … Read more

Adding a custom admin page

You need just two steps: Hook into the action admin_menu, register the page with a callback function to print the content. In your callback function load the file from plugin_dir_path( __FILE__ ) . “included.html”. Demo code: add_action( ‘admin_menu’, ‘wpse_91693_register’ ); function wpse_91693_register() { add_menu_page( ‘Include Text’, // page title ‘Include Text’, // menu title ‘manage_options’, … Read more