0
votes

Everything is ok and I can activate my plugin in my local wamp server, but when I try to upload & activate my plugin in the website I got a fatal error says class not found.

I tried to run composer init & composer install using ssh on my website but not working

This is my main plugin PHP file :

    if( file_exists( dirname(__FILE__) . '/vendor/autoload.php'))
        require_once plugin_dir_path(__FILE__) . '/vendor/autoload.php';


function activate_aa_test()
{
    Inc\Base\Activate::activate();
}
function deactivate_aa_test()
{
    Inc\Base\Deactivate::deactivate();
}

register_activation_hook(__FILE__, 'activate_aa_test');
register_deactivation_hook(__FILE__, 'deactivate_aa_test');

And this is composer.json

    "name": "vahid/aa-test",
    "description": "test",
    "type": "project",
    "license": "GPL",
    "authors": [
        {
            "name": "Vahid",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {"Inc\\": "./inc"}
    }
}

And These are WordPress errors:

Fatal error: Uncaught Error:
Class 'Inc\Base\Activate' not found in /home/vahidsapp/public_html/wp-content/plugins/aa-test/aa-test.php:34 
Stack trace: #0 /home/vahidsapp/public_html/wp-includes/class-wp-hook.php(286): activate_aa_test('') 
#1 /home/vahidsapp/public_html/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array) 
#2 /home/vahidsapp/public_html/wp-includes/plugin.php(465): WP_Hook->do_action(Array) 
#3 /home/vahidsapp/public_html/wp-admin/plugins.php(177): do_action('activate_aa-tes...') 
#4 {main} thrown in /home/vahidsapp/public_html/wp-content/plugins/aa-test/aa-test.php on line 34
1
Try composer dump-autoload and check again. - cabrerahector
already tried, not working. - Vahid
I found if I move the activate & deactivate classes from inc/base to inc and change the namespace from namespace Inc\Base; to namespace Inc; i can activate/deactivate the plugin, but in my main plugin I managed to make separate folders for each part, and I can't move all to the root of inc! - Vahid

1 Answers

0
votes

I found the problem, I must add the class map to the composer.json:

"require": {},
    "config": {
        "optimize-autoloader": true
    },
    "autoload": {
        "psr-4": {"Inc\\": "./inc"},
        "classmap": ["inc/"]
    }