1

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 think you need to wrap your entire class declaration (or the file inclusion) in your my_plugin_add_settings() function: – karpstrucking Nov 20 '18 at 18:34
  • produces unfortunately a PHP Fatal error: Uncaught Error: Class 'WooCommerce_Chilexpress_Tags_Settings' not found – Canelo Digital Nov 20 '18 at 19:08
4

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().

2

See working example: https://gist.github.com/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 );

}
  • Thanks, I took exactly the same guide as template for my code, but for a reason I could not detect, I got the mentioned error(s)... I will try first GOKUs answer, as I believe there is really something wrong in my general approach on how the class is included... – Canelo Digital Nov 22 '18 at 16:16
  • @CaneloDigital Okay, good luck! – Remzi Cavdar Nov 22 '18 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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