Overwriting Core WordPress Functions with Plugins

Is there a way to overwrite a core function is the WordPress core using a plugin? I don’t need to inject code, I need to replace the function entirely with a re-written version.

The specific function is wp_nav_menu_item_post_type_meta_box() in /wp-admin/includes/nav-menu.php

Read More

Basically there’s a lack of functionality that is not meeting a clients need, so I need to overwrite this function with one I have created. I’ve made heavy modifications to the function, and I want to have the ability to overwrite the function with my own through a plugin instead of editing the file directly.

I know this probably isn’t much better than hacking the core, however its a temporary immediate solution.

Related posts

Leave a Reply

2 comments

  1. Not really, no. You can override built-in PHP functions, but not user-defined functions.

    However, all this function does is define a meta box. Why not define your own?

    Once you’ve got your own meta box defined and added, you can call remove_meta_box to remove the standard one:

    remove_meta_box( 'add-POSTTYPENAME', 'nav-menus', 'side');
    

    The meta box is originally added for each custom post type using a loop. The ID of the meta box is defined as add-{$id} where $id is the name of the post-type. So you can remove this meta box for all post types by doing a similar loop, or just for a specific post type. It’s up to you.

    Then just add your custom meta box for the post types you need. Here’s the function that adds the original for reference:

    function wp_nav_menu_post_type_meta_boxes() {
          $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
    
          if ( ! $post_types )
              return;
    
          foreach ( $post_types as $post_type ) {
              $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );
              if ( $post_type ) {
                  $id = $post_type->name;
                  add_meta_box( "add-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', 'default', $post_type );
              }
          }
    }
    
  2. Here is a function that will allow you to remove wp defined meta boxes (even defaults) inside the nav-menus.php page. By removing the meta box, you also remove the checkbox option from the “Screen Options” tab.

    A stand alone remove_meta_box() function call won’t work because, as EAMann said, the boxes are user-defined. You need to check the admin status of the user before you can remove.

    You can place this function inside your theme functions.php with no change to the wp-admin files.

    function hide_meta_in_custom_menu_admin() {
    
        global $pagenow;
        global $current_user;
    
        get_currentuserinfo();
    
        $user_login=$current_user->user_login;
    
        if($user_login=="scholar") {
        //nothing
        } else {
            if (is_admin() && $pagenow=='nav-menus.php') {
                remove_meta_box('add-custom-links', 'nav-menus', 'side');
                remove_meta_box('nav-menu-theme-locations', 'nav-menus', 'side');
                remove_meta_box('add-category', 'nav-menus', 'side');
            }
        }
    }
    
    add_action( 'admin_head', 'hide_meta_in_custom_menu_admin'  );
    

    Simpler code, to remove for any user.

    function hide_meta_in_nav_menu() {
    
        global $pagenow;
    
        if (is_admin() && $pagenow=='nav-menus.php') {
            remove_meta_box('add-custom-links', 'nav-menus', 'side');
            remove_meta_box('nav-menu-theme-locations', 'nav-menus', 'side');
            remove_meta_box('add-category', 'nav-menus', 'side');
        }
    }
    add_action( 'admin_head', 'hide_meta_in_nav_menu'  );