Using WordPress ‘parent_file’ PHP filter

The parent_file WordPress PHP filter allows you to modify the parent file of an admin menu sub-menu item, which enables plugins to rearrange sub-menu items.

Usage

add_filter('parent_file', 'my_custom_parent_file');
function my_custom_parent_file($parent_file) {
  // your custom code here
  return $parent_file;
}

Parameters

  • $parent_file (string): The parent file of a sub-menu item.

More information

See WordPress Developer Resources: parent_file

Examples

Move a sub-menu item to a custom parent menu

Move the “Categories” sub-menu item from the “Posts” menu to a custom parent menu called “Content Management.”

add_filter('parent_file', 'move_categories_submenu');

function move_categories_submenu($parent_file) {
  global $submenu_file;

  if ('edit-tags.php?taxonomy=category' == $submenu_file) {
    $parent_file = 'content-management.php';
  }
  return $parent_file;
}

Move all sub-menu items from a parent menu to another parent menu

Move all sub-menu items from the “Tools” menu to the “Settings” menu.

add_filter('parent_file', 'move_tools_submenu');

function move_tools_submenu($parent_file) {
  global $submenu_file, $submenu;

  if (isset($submenu['tools.php'])) {
    foreach ($submenu['tools.php'] as $key => $submenu_item) {
      $submenu['options-general.php'][] = $submenu_item;
      unset($submenu['tools.php'][$key]);
    }
  }
  return $parent_file;
}

Change the parent file based on user capabilities

Change the parent file for a specific sub-menu item based on the user’s capabilities.

add_filter('parent_file', 'change_parent_file_for_users');

function change_parent_file_for_users($parent_file) {
  global $submenu_file;

  if (current_user_can('manage_options') && 'custom-submenu.php' == $submenu_file) {
    $parent_file = 'options-general.php';
  }
  return $parent_file;
}

Move a custom post type sub-menu item to the “Posts” menu

Move a custom post type called “Reviews” from its own menu to the “Posts” menu.

add_filter('parent_file', 'move_reviews_submenu');

function move_reviews_submenu($parent_file) {
  global $submenu_file, $post_type;

  if ('edit.php?post_type=reviews' == $parent_file && 'reviews' == $post_type) {
    $parent_file = 'edit.php';
  }
  return $parent_file;
}

Conditionally move a sub-menu item based on the current user role

Move the “Media Library” sub-menu item to the “Settings” menu for users with the “editor” role.

add_filter('parent_file', 'move_media_library_submenu');

function move_media_library_submenu($parent_file) {
  global $submenu_file, $current_user;

  if (in_array('editor', $current_user->roles) && 'upload.php' == $submenu_file) {
    $parent_file = 'options-general.php';
  }
  return $parent_file;
}