0

I am trying to copy directory using WP_Filesystem API with the method copy_dir() on plugin activation, but it is give me the following error message:

Plugin could not be activated because it triggered a fatal error.

Fatal error: Call to undefined method WP_Filesystem_Direct::copy_dir()
in D:\xampp\htdocs\wordpress\wp-content\plugins\xxxxxxxx\xxxxxx.php on line 98

Here is my codes:

function sstssfb_activation_hook_vcheck() {
    delete_option(esc_attr("sstssfb_stop_showingvchecknotice"));

    /* INITIALIZE WPFilesytem */
    $access_type = get_filesystem_method();
    if($access_type === 'direct') {
        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $creds = request_filesystem_credentials($url, '', false, false, array());
            // initialize the API
            if ( ! WP_Filesystem($creds) ) {
                // any problems and we exit
                return;
            }
            global $wp_filesystem;
    /* CREATE SSTSSFB_ASSETS DIRECTORY */
        if(!$wp_filesystem->is_dir(SSTSSFB_ASSET_DIR)) 
        {
            $wp_filesystem->mkdir(SSTSSFB_ASSET_DIR);
        }

    /* MOVE ADDITIONAL assets TO THAT (sstssfb_assets) DIRECTORY */
        // addons
        if(file_exists(SSTSSFBDIR_ADDONS_ORI))
        $wp_filesystem->copy_dir(SSTSSFBDIR_ADDONS_ORI, SSTSSFB_ASSET_DIR, true);
        $wp_filesystem->delete(SSTSSFBDIR_ADDONS_ORI);
        // themes
        if(file_exists(SSTSSFB_THEMEDIR_ORI))
        $wp_filesystem->copy_dir(SSTSSFB_THEMEDIR_ORI, SSTSSFB_ASSET_DIR, true);
        $wp_filesystem->delete(SSTSSFBDIR_ADDONS_ORI);
    }

    }
    register_activation_hook(__FILE__, "sstssfb_activation_hook_vcheck");

What would be the problem here?

  • The problem here, honestly and respectfully, is that having a plugin copy files around the filesystem is almost always the wrong way to solve a problem and will always be subject to server permissions beyond your control and so may not work whatever your do. If you could explain the problem you are trying to solve rather than the broken method you are using to try to solve that problem perhaps someone will have a solution. – s_ha_dum Nov 18 '15 at 15:11
0

That was my fault!

The problem is copy_dir isn't a WP_Filesystem method, so it cannot be called as an instance of WP_Filesystem like this: $wp_filesystem->copy_dir(), but independently like this: copy_dir("source", "destination") after the WP_Filesystem() has already been called and setup.

Reference copy_dir

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.