• Resolved moabmike579

    (@moabmike579)


    This error comes and goes for no apparent reason:
    “Plugin could not be activated because it triggered a fatal error.
    Fatal error: Cannot declare class WP_Filesystem_Base, because the name is already in use in /home2/moabiion/public_html/wp-admin/includes/class-wp-filesystem-base.php on line 14″ (It is declared there as expected: class WP_Filesystem_Base { … )

    With a custom plugin xxx.php used only on one page, I’m trying tp read an ordinary text file xxx.csv located in the same folder as xxx.php. I’m doing so with the class WP_Filesystem_Direct, an extension of WP_Filesystem_Base. To define/reference them, I use a pair of include statements:

    `include ‘/home2/moabiion/public_html/wp-admin/includes/class-wp-filesystem-base.php’;
    include ‘/home2/moabiion/public_html/wp-admin/includes/class-wp-filesystem-direct.php’;`

    I use methods from these class definitions (ie declarations) to access xxx.csv:
    `$fileSystem = new WP_Filesystem_Direct(0);
    $path = $fileSystem->wp_plugins_dir();
    $path .= ‘xxxplugin/xxx.csv’;
    $getFileResponse = $fileSystem->get_contents($path);’

    Often all this compiles and runs fine. Other times (as development of the plugin and website progresses) for mysterious reasons I get the error described above. Odd, because I never try to (re-)declare the two classes (-base or -direct), only instantiate and call their methods.

    Perhaps an autoloader conflict? I know little about them.

    Thanks for any help for this newbie!

    • This topic was modified 4 years, 4 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 2 replies - 1 through 2 (of 2 total)
  • if( !class_exists( 'WP_Filesystem_Direct' ) ) {
        require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
        require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
    }

    you should check for class exists first then include/require those files once for your use.

    Thread Starter moabmike579

    (@moabmike579)

    Vijay – thanks so much! Similarly, I discovered that using include_once() rather than include() did the trick.

    Moab Mike

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Plugin dup declaration of WP_Filesystem_Base?’ is closed to new replies.