Selectively Hiding features/elements in the reorder interface
The Advanced Post Types Order (APTO) plugin in WordPress provides a powerful way to manage and reorder posts and custom post types. However, there are cases where you might want to customize the interface further, such as hiding specific options for certain user roles to keep the dashboard cleaner and more secure. In this guide, we’ll look at how you can use a simple PHP function and CSS to conditionally hide interface options within APTO’s reorder screens.
Why Customize the APTO Reorder Interface?
The APTO interface is comprehensive, but sometimes it offers more options than necessary for certain users. For instance:
- Streamlined Experience: Limiting options for non-admin roles can reduce confusion and focus users on essential tasks.
- Enhanced Security: Restricting access to certain settings minimizes the risk of accidental changes by editors, authors, or other roles.
In this guide, we’ll use custom code to selectively hide elements within the APTO reorder interface for specific user roles. In the code example, the following options will be hidden:
Overview of the Custom Code
To customize the APTO reorder interface, we’ll:
- Check the Current Page: Only apply custom styles to specific APTO reorder pages.
- Validate User Roles: Use conditions to target specific user roles (e.g.,
editor
,author
). - Inject Custom CSS: Use inline CSS to hide the chosen elements.
The code will be added to your theme’s functions.php
file.
Adding the Custom Code to Your Theme
Here’s a detailed look at the code you’ll need to customize the APTO reorder interface.
Code Example
The following code checks if the user is on one of the APTO reorder pages and hides the #sort_options
element if the user’s role is editor
or author
.
// Define a function to add custom styles to the APTO reorder interface based on user role. function apto_custom_admin_styles() { // Verify if we are on a specified APTO reorder page. if ( ! isset( $_GET['page'] ) || ! in_array( $_GET['page'], array( 'apto_edit-phppost_typecustom_post', 'apto_edit-phppost_typemy_hierarchical' ) ) ) return; // Exit if not on a specified page. // Retrieve the current user's data to check their role. $user = wp_get_current_user(); // Define a list of roles that should have a restricted view. $check_roles = array( 'editor', 'author' ); // Only apply custom styles if the user has one of the specified roles. if ( ! array_intersect( $check_roles, $user->roles ) ) return; // Exit if the user role is not in the specified list. <!-- Inline CSS to hide specific elements on the reorder page for selected roles --> echo '< style> #sort_options { display: none; } /* Hide the sort options section */ < /style>'; } // Hook the function into 'admin_head' to apply styles in the admin area. add_action('admin_head', 'apto_custom_admin_styles');
Expanding the Customization
Here are a few ways to extend this customization:
- Hide Additional Elements: Add more CSS rules to hide other sections or elements on the reorder interface.
- Target Other Roles or Pages: Update
$check_roles
and$_GET['page']
to include more roles or reorder pages. - Conditional Styling: Instead of hiding elements, you could change their appearance (e.g., dimming or resizing elements for certain roles).
Example of additional CSS for hiding multiple elements:
<style > #sort_options, .some_other_element { display: none; } </style >