Search code examples
phpwordpressmybb

Go around cannot redeclare error in PHP


I am trying to integrate WordPress and MyBB forums. Specifically, I just want to add WordPress's navbar (the new menu) to my MyBB website. Following the CODEX example, I've added the following to my header.php of my MyBB installation:

require('/home/linuxdis/public_html/wp-blog-header.php');

However, I get 500 error when navigating to the forum. Examining error_log revealed this:

PHP Fatal error: Cannot redeclare is_super_admin() (previously declared in /home/linuxdis/public_html/forum/inc/functions.php:5484) in /home/linuxdis/public_html/wp-includes/capabilities.php on line 1213

Bummer, the functions are named the same. Other than renaming one of the functions and probably breaking absolutely everything, is there a way to go around this? :/


Solution

  • User require_once() instead of require() and it will make sure it will only include that file once per page.

    Although not ideal in this case, you could wrap each function in a function_exists() check:

    if(!function_exists('myfunc') {
        function myfunc() {....}
    }
    

    This might be your only option if there are direct clashes with WP/MyBB, you're stuck if they both need is_super_admin() though.