Hide WordPress Admin Links In Code

Recently on a project the client asked to be able to post testimonials on their site. Normally we install the Testimonials Widget, as it gives us a lot of flexibility, has short codes so we can add to different pages and categorize them, and more. So, in this case, we did, as well, and added a few for them for their approval.

Unbeknownst to us, the theme we had purchased for this client ALSO had a testimonials widget, and the client was asking why the testimonials he was adding wasn’t showing up on the page. So I logged in, and sure enough, there were two (2) “Testimonials” menu items (can you say menu blind? I knew you could).

Well, we don’t want two menu items, and we don’t want to cause confusion, so we needed to remove the one supplied with the theme. Sadly, this wasn’t an option within the theme itself. So I decided to hide the bad one. This is how I did it.

First, I need to know what to hide, so I used the following code to determine the menu item (Note: the capture debug info function is used in almost all of my projects – it has been a life saver numerous times):

add_action( ‘admin_init’, ‘clientname_debug_admin_menu’ );
function clientname_debug_admin_menu() {
_capture_clientname_debug(__FILE__, __LINE__, $GLOBALS[ ‘menu’ ], “/tmp/client_debug.log”);

* _capture_clientname_debug()
* An updated function to display debug data for development or, well, debug,
* purposes.
* @param string $file the name of the file (almost always just __FILE__)
* @param string $line the line number of the file (above – almost always __LINE__)
* @param mixed $code what to display (either a string, array, or object)
* @param string $log_file the full path to the file to log the data to (default is /tmp/debug.log)
* @param string $email the email address to send the file to (default is ” to save to a physical file)
* @return null
function _capture_clientname_debug($file, $line, $code, $log_file = “/tmp/debug.log”, $email = “”) {
$data = “”;

// set the data
if (is_array($code) || is_object($code)) {
$data = ob_get_contents();
} else {
$data = $code;

// log the data to the log file
$data = “===============================\nLine #” . $line . ” of file ‘” . $file . “‘ on ” . date(“Y-m-d g:i:s a”) . “\n\n” . trim(str_replace(”
“, “\n”, $data)) . “\n\n”;

// if we need to also email it, do that here
if ($email) {
mail($email, “Debug / Error Data – $file”, $data);

// always log the data to a file
$fp = @fopen($log_file, “a+”);
@fwrite($fp, $data);

Note to self: swap the fopen(), fwrite() and fclose() commands with the preferred file_put_contents() instead.

I then refreshed the admin dashboard page, and the file /tmp/client_debug.log was created and, among it, was this little piece of information:

[8] => Array
[0] => Testimonials
[1] => edit_posts
[2] => edit.php?post_type=theme_testimonial
[3] =>
[4] => menu-top menu-icon-theme_testimonial
[5] => menu-posts-theme_testimonial
[6] => dashicons-images-alt2

Now, armed the value of key #2, I can now add the following code to hide the menu item:

// remove unwanted links in wp-admin
add_action( ‘admin_init’, ‘clientname_admin_remove_menu_pages’ );
function clientname_admin_remove_menu_pages() {
remove_menu_page( ‘edit.php?post_type=theme_testimonial’ );

When I reloaded the admin dashboard again, the bad link was gone!