$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Plugin vs Settings load order (woocommerce dependency)|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Plugin vs Settings load order (woocommerce dependency)

matteradmin6PV0评论

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 );

}
Post a comment

comment list (0)

  1. No comments so far