0

I built a WordPress plugin following Alessandro Castellani's tutorials with no problem.

However, when I built a second plugin and activate it, for some reason it overwrites the first one. In the sidebar, I get two links for the same (the second) plugin, whereas the first one does not appear. Even if I deactivate the first one, the second plugin appears as double.

Desired Behaviour: The two plugins work and act as separate units.

Current Behaviour: They seem to be overlapping and taking settings from each other and the second one removes the first one from the sidebar. What appears is the second plugin twice.

I have a different name for the plugin and reference this throughout

/**
 * @package PluginNumberTwo
 */

Could the problem be because my __construct function within the BaseController is nearly identical?

First Plugin:

namespace Inc\Base;

class BaseController
{


    public $plugin_path;
    public $plugin_url;
    public $plugin;

    public function __construct(){
        $this->plugin_path = plugin_dir_path( dirname( __DIR__ ) );
        $this->plugin_url = plugin_dir_url( dirname( __DIR__ ) );
        $this->plugin = plugin_basename(dirname(dirname(__DIR__))) .'/first-plugin.php';
    ...
    }
}

Second Plugin:

namespace Inc\Base;

class BaseController
{
    public $plugin_path;
    public $plugin_url;
    public $plugin;

    public function __construct(){
        $this->plugin_path = plugin_dir_path( dirname( __DIR__ ) );
        $this->plugin_url = plugin_dir_url( dirname( __DIR__ ) );
        $this->plugin = plugin_basename(dirname(dirname(__DIR__))) .'/second-plugin.php';
    ...
    }
}

One potential problem is that some of the functions have the same names.

public function register()
public function addPages( array $pages )
public function addAdminMenu()
public function setSettings( array $settings )
public function setSections( array $sections )
public function setFields( array $fields )
public function registerCustomFields()

Would this potentially cause the problem?

Also, here are the comments at the beginning of each plugin file:

First plugin:

<?php
/**
 * @package FirstPlugin
 */
/*
    Plugin Name: FirstPlugin
    Plugin URI: https://example.com
    Description: This is the first plugin.
    Version: 1.0.0
    Author: First Lastname
    Author URI: http://www.example.com
*/

Second Plugin:

<?php
/**
* @package SecongPlugin
*/
/*
    Plugin Name: SecondPlugin
    Plugin URI: https://example.com
    Description: This is the second plugin
    Version: 1.0.1
    Author: Brad Ahrens
    Author URI: http://www.example.com
*/

The file names are exactly the same (other than the main folder name and the initial first-plugin.php / second-plugin.php files).

10
  • And what is $this? What class do you use in these plugins? Commented Feb 6, 2019 at 16:51
  • Good question. I'll post a more thorough explanation in the original post. Commented Feb 6, 2019 at 16:53
  • Please do, because that is the crucial thing in here. Commented Feb 6, 2019 at 16:54
  • So, the class is the BaseController which is the same in both plugins and then I am getting the plugin_path within the public function __construct Commented Feb 6, 2019 at 16:56
  • Ehhh... OK. It doesn't matter how the functions are called, if they are methods of a class and not global functions. But it does matter what class are you using in these plugins. Can you show the class definition part? Commented Feb 6, 2019 at 16:56

1 Answer 1

1

OK, so...

It doesn't matter if the functions are the same or if the __construct methods are almost identical - all of these functions are not global - they are class methods - so they can be the same.

Problem lies somewhere else...

In both plugins you declare a class with identical name in the same namespace... So these classes conflict with each other.

The class name should be unique - so using BaseController as a class name is rather bad idea.

Notice After you change names of these classes, remember also to change the name in initialization part.

Somewhere in your plugin you have something like this:

new BaseController();

You have to change that, so the proper class gets initialized.

6
  • Thanks Krzysiek! I'll try renaming all of the classes to see if that helps. Commented Feb 6, 2019 at 17:21
  • Remember that you also have to use proper class for initialization. Commented Feb 6, 2019 at 17:22
  • So, I renamed BaseController to BaseControllerTest throughout the 2nd plugin. Unfortunately, I'm getting the same error :( I also do not have new BaseController(); in the code. Commented Feb 6, 2019 at 17:33
  • You have to initialize the class somewhere in there - without initialization the constructor won't be called, so your code won't work at all. It's really hard to guess what's wrong with code that I don't see... Commented Feb 6, 2019 at 17:34
  • 1
    No problem. Sometimes it's hard to debug a code even when it's yours and you see it ;) Been there, done that ;) Commented Feb 6, 2019 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.