MultiLingual-Press + Max Mega Menu = PHP Fatal Error
-
I was getting errors like this:
Got error 'PHP message: PHP Fatal error: Uncaught TypeError: Argument 1 passed to Mlp_Nav_Menu_Frontend::maybe_delete_obsolete_item() must be an instance of WP_Post, instance of stdClass given, called in /var/www/vhosts/ayurveda.report/httpdocs/wp-content/plugins/multilingual-press/inc/nav-menu/Mlp_Nav_Menu_Frontend.php on line 53 and defined in /var/www/vhosts/ayurveda.report/httpdocs/wp-content/plugins/multilingual-press/inc/nav-menu/Mlp_Nav_Menu_Frontend.php:74 Stack trace: #0 /var/www/vhosts/ayurveda.report/httpdocs/wp-content/plugins/multilingual-press/inc/nav-menu/Mlp_Nav_Menu_Frontend.php(53): Mlp_Nav_Menu_Frontend->maybe_delete_obsolete_item(Object(stdClass)) #1 /var/www/vhosts/ayurveda.report/httpdocs/wp-includes/class-wp-hook.php(300): Mlp_Nav_Menu_Frontend->filter_items(Array) #2 /var/www/vhosts/ayurveda.report/httpdocs/wp-includes/plugin.php(203): WP_Hook->apply_filters(Array, Array) #3 /var/www/vhosts/ayurveda.report/httpdocs/wp-includes/nav-menu-template.php(189): apply_filters('wp_nav_menu_obj...', A...If I deactivate either of the plugins, the site works again.
I’ve looked at the code: MultiLingual-Press does Type Hinting on its function arguments, in particular where a Post is expected the hint is “WP_Post”.
That normally works, and is no doubt good practice… BUT will fail if any other plugin has created or recast post objects without explicitly making them of type WP_Post. Sadly, it seems that Max Mega Menu (and its clone AP Mega Menu) are creating navigation objects without specifying their type, so they default to StdClass. Then, when they get “walked” by some function in MultiLingual-Press, we end up with the type mismatch fatal error.I also checked out how WordPress itself handles this topic: The expected object function argument type WP_Post is often documented in comment, but never type hinted. So WordPress itself always carefully makes menu items which are of type WP_Post, but never, so to speak, forces the issue. You’ll find code like this: $_post = new WP_Post( $post );
References to consult:
https://codex.ww.wp.xz.cn/Plugin_API/Filter_Reference/nav_menu_link_attributes => $item – Object containing item details. E.G: If the link is to a page $item will be a WP_Post object
https://stackoverflow.com/questions/7839059/type-hinting-for-any-object
http://php.net/manual/en/functions.arguments.php
Now the really interesting question: Should MultiLingual-Press be more pragmatic and less purist, and remove the type hints to match WordPress itself?
Or should Max Mega Menu be improved to ensure that all the menu item objects it modifies or creates are of type WP_Post?My personal preference would be for Max Mega Menu to ensure the correct type is set, but I don’t really mind as long as a solution pops up soon… because I’ve had to shift to “Mega Main Menu”, but in general definitely prefer the way Max Mega Menu works.
This forum post is going in identical wording on the support forums of both plugins. Hope this analysis helps!
Tim
The topic ‘MultiLingual-Press + Max Mega Menu = PHP Fatal Error’ is closed to new replies.