Reputation: 173
I have strange problem to extend WC_Settings_Page. I want to use it to add setting tab (and some sections inside it in Woocommerce setting sections.
My plugin is based on OOP and all of my files has related namespace. I have a core class which is something like this in the following (if you need to see the complete structure, you can find it in my github repo:
<?php
/**
* The file that defines the core plugin class
*
* A class definition that includes attributes and functions used across both the
* public-facing side of the site and the admin area.
*
* @package Siawood_Products
* @author Mehdi Soltani Neshan <[email protected]>
* @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3
* @link https://wpwebmaster.ir
* @since 1.0.0
*/
namespace Siawood_Products\Includes\Init;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/* Some uses is hear */
use Siawood_Products\Includes\Admin\WC_Siawood_Setting_Tab1;
class Core implements Action_Hook_Interface, Filter_Hook_Interface {
/*constructor of class and other things is here.....*/
public function init_core() {
/* FYI: I checked before is woocommerce is active or not, if active, I call init_core() */
add_action( 'plugins_loaded', [ $this, 'add_setting_page' ] );
}
public function add_setting_page( ) {
new WC_Siawood_Setting_Tab1();
}
}
To sure that Woocommerce plugin is loaded, so I can use from woocommerce_loaded
hook (also I checked it with init
, but the results were the same.
And WC_Siawood_Setting_Tab1
class is:
<?php
/**
* WC_Siawood_Setting_Tab Class File
*
* This file creates admin setting tab for this plugin
*
* @package Siawood_Products
* @author Mehdi Soltani Neshan <[email protected]>
* @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3
* @link https://wpwebmaster.ir
* @since 1.0.1
*/
namespace Siawood_Products\Includes\Admin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Siawood_Setting_Tab Class File
*
* This file creates admin setting tab for this plugin
*
* @package Siawood_Products
* @author Mehdi Soltani Neshan <[email protected]>
* @link https://wpwebmaster.ir
*
*/
class WC_Siawood_Setting_Tab1 extends \WC_Settings_Page {
/**
* Constructor.
*
* @version 1.3.0
* @since 1.0.0
*/
function __construct() {
$this->id = 'my_settings';
$this->label = __( 'my label', 'my-textdomain-woocommerce' );
parent::__construct();
}
}
So when I run my plugin and call init_core()
method, I got this error:
Fatal error: Class 'WC_Settings_Page' not found in ...\wp-content\plugins\siawood-products\includes\admin\class-wc-siawood-setting-tab1.php on line 29
It is strange because I hooked it to woocommerce_loaded or plugin_loaded till it will be able load after loading Woocommerce but it doesn't work.
How can I solve this?
Upvotes: 0
Views: 1377
Reputation: 26319
Instead of initializing the class on plugins_loaded
I would do the following:
public function init_core() {
add_filter( 'woocommerce_get_settings_pages', [ $this, 'add_setting_page' ] );
}
public function add_setting_page( $settings ) {
$settings[] = include_once path_to_your_file . '/class-wc-siawood_setting_tab1 .php'
return $settings;
}
For completeness:
use Siawood_Products\Includes\Admin\WC_Siawood_Setting_Tab1;
class Core implements Action_Hook_Interface, Filter_Hook_Interface {
/*constructor of class and other things is here.....*/
public function init_core() {
add_action( 'woocommerce_get_settings_pages', [ $this, 'add_setting_page' ] );
}
public function add_setting_page( ) {
$settings[] = include_once $path_to_your_file . '/class-wc-siawood_setting_tab1 .php'
return $settings;
}
}
and in your other class file the class must initialize itself
namespace Siawood_Products\Includes\Admin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WC_Siawood_Setting_Tab1 extends \WC_Settings_Page {
/**
* Constructor.
*
* @version 1.3.0
* @since 1.0.0
*/
function __construct() {
$this->id = 'my_settings';
$this->label = __( 'my label', 'my-textdomain-woocommerce' );
parent::__construct();
}
}
return new WC_Siawood_Setting_Tab1();
Upvotes: 2