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:
Description: This is the first plugin.
Version: 1.0.0
Author: First Lastname
Author URI:
*/
Second Plugin:
<?php
/**
* @package SecongPlugin
*/
/*
Plugin Name: SecondPlugin
Plugin URI:
Description: This is the second plugin
Version: 1.0.1
Author: Brad Ahrens
Author URI:
*/
The file names are exactly the same (other than the main folder name and the initial first-plugin.php / second-plugin.php files).
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
Description: This is the first plugin.
Version: 1.0.0
Author: First Lastname
Author URI: http://www.example
*/
Second Plugin:
<?php
/**
* @package SecongPlugin
*/
/*
Plugin Name: SecondPlugin
Plugin URI: https://example
Description: This is the second plugin
Version: 1.0.1
Author: Brad Ahrens
Author URI: http://www.example
*/
The file names are exactly the same (other than the main folder name and the initial first-plugin.php / second-plugin.php files).
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.
$this
? What class do you use in these plugins? – Krzysiek Dróżdż Commented Feb 6, 2019 at 16:51