Plugin vs Settings load order (woocommerce dependency)

admin2025-06-06  5

I am developing a Woocommerce dependent plugin which works, and a settings page which behaves funky and throws a Class 'WC_Settings_Page' not found Fatal Error

if ( !defined( 'ABSPATH' ) ) {exit;}

if ( !class_exists( 'WooCommerce_Chilexpress_Tags_Settings' ) ) {

   class WooCommerce_Chilexpress_Tags_Settings extends WC_Settings_Page{
   ...
   }

   function my_plugin_add_settings() {
      return new WooCommerce_Chilexpress_Tags_Settings();
   }
}

add_filter( 'woocommerce_get_settings_pages', 'my_plugin_add_settings', 15 );

this code is in a includes/mysettings.php which is loaded during the plugin init, which alphabetically is woocommerce-chilexpress-etiquetas, so it should be loaded after woocommerce

For a reason I don't understand yet, my plugin settings are loaded always before WooCommerce Settings though throwing me a PHP Fatal Error:

PHP Fatal error:  Class 'WC_Settings_Page' not found

The obvious dirty fix was to insert the WC_Settings_Page code into my own settings. I am trying now to clean this up but somehow it won't work...

So the (yes I know very broad) question is: What could I miss?

I am developing a Woocommerce dependent plugin which works, and a settings page which behaves funky and throws a Class 'WC_Settings_Page' not found Fatal Error

if ( !defined( 'ABSPATH' ) ) {exit;}

if ( !class_exists( 'WooCommerce_Chilexpress_Tags_Settings' ) ) {

   class WooCommerce_Chilexpress_Tags_Settings extends WC_Settings_Page{
   ...
   }

   function my_plugin_add_settings() {
      return new WooCommerce_Chilexpress_Tags_Settings();
   }
}

add_filter( 'woocommerce_get_settings_pages', 'my_plugin_add_settings', 15 );

this code is in a includes/mysettings.php which is loaded during the plugin init, which alphabetically is woocommerce-chilexpress-etiquetas, so it should be loaded after woocommerce

For a reason I don't understand yet, my plugin settings are loaded always before WooCommerce Settings though throwing me a PHP Fatal Error:

PHP Fatal error:  Class 'WC_Settings_Page' not found

The obvious dirty fix was to insert the WC_Settings_Page code into my own settings. I am trying now to clean this up but somehow it won't work...

So the (yes I know very broad) question is: What could I miss?

Share Improve this question edited Nov 20, 2018 at 18:15 Canelo Digital asked Nov 20, 2018 at 18:01 Canelo DigitalCanelo Digital 1414 bronze badges 2
  • I think you need to wrap your entire class declaration (or the file inclusion) in your my_plugin_add_settings() function: – karpstrucking Commented Nov 20, 2018 at 18:34
  • produces unfortunately a PHP Fatal error: Uncaught Error: Class 'WooCommerce_Chilexpress_Tags_Settings' not found – Canelo Digital Commented Nov 20, 2018 at 19:08
Add a comment  | 

2 Answers 2

Reset to default 6

The problem is, by the time your WooCommerce_Chilexpress_Tags_Settings is defined, WC_Settings_Page has indeed not yet loaded; hence you got the fatal error.

And if you want to do it the same way that WooCommerce does it, place the WooCommerce_Chilexpress_Tags_Settings in a separate PHP file, e.g. includes/class-woocommerce-chilexpress-tags-tettings.php, and initialize the class from that file — i.e. return an instance of WooCommerce_Chilexpress_Tags_Settings like so:

<?php
// class-woocommerce-chilexpress-tags-tettings.php

defined( 'ABSPATH' ) || exit;

class WooCommerce_Chilexpress_Tags_Settings extends WC_Settings_Page {

    ...
}

return new WooCommerce_Chilexpress_Tags_Settings();

and include it from my_plugin_add_settings():

function my_plugin_add_settings( $settings ) {
    $settings[] = include 'path/to/includes/class-woocommerce-chilexpress-tags-tettings.php';

    return $settings;
}
add_filter( 'woocommerce_get_settings_pages', 'my_plugin_add_settings' );

And if you noticed, you need to return the $settings from my_plugin_add_settings().

See working example: https://gist.github/bekarice/34aaeda2d4729ef87ad7
You should do something like this:

// If this file is called directly, abort.
defined('ABSPATH') or exit();


if ( !class_exists( 'WooCommerce_Chilexpress_Tags_Settings' ) ) {

    function my_plugin_add_settings() {

        class WooCommerce_Chilexpress_Tags_Settings extends WC_Settings_Page {
            // Your class and your code / logic 
        }

        return new WooCommerce_Chilexpress_Tags_Settings();

    }

    add_filter( 'woocommerce_get_settings_pages', 'my_plugin_add_settings', 15 );

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749164785a316910.html

最新回复(0)