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
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.
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: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:
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 thewp-admin
files.Simpler code, to remove for any user.