happyplugin-menu-conflict

One of the most bugging problem when you are designing a plugin and want to a a menu page is the you don’t know if the the menu position that you choose for your menu is already taken.

WordPress menu system does not allow to display menu that have the same position number.

If 2 menus have the same position number only the first one that was hooked will be display and all the other ones will be ignored.

The current solutions that you can find on the web are not dealing with the problem but try to give avoid it by using decimal number so the chance the that you have 2 menus using the same position will be low.

The problem with that solution is that your plugin could be installed on different environment and although if you are decimal menu position you would decrease the possibility of conflict there is a better solution that that.

After developing a lot of plugin for customer we have encounted  that problem and needed a solution that solve this problem for good no matter what plugins or menus are already installed on the client website.

 

The function can be added anywhere on you plugin code as long that it will be available to read by your add_menu function.

The function get 2 parameters:

$start – The position location you want to place the your menu in and the second

$increment – The increment that that position will be increased the original position is already taken.

In order to use it just call the function you should use:

If you are using inside the same class you will need to call it like this:

A complete code snippet can look like this:

We are calling the admin_menu hook as late as we can so we will have the entire menu items already in place.

Let me know what you think in the comments or if this snippet helped you in creating better plugins.

Happy Plugins

About the author: Happy Plugin is developing WordPress plugin and custom theme for more than 4 years. Most of our work is developing custom plugins and solution for customers.

Add your opinion to the discussion

Loading Facebook Comments ...

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *