questions

register_activation_hook is not working – WP

Rate this post

I’m trying to create a plugin for shortcodes. But my activation hook is not working. Plugin is activated in my wp-admin/plugins page but nothing works which is in my code like: My enqueue & shortcode functions are not working. I’ve tried plugin_dir_path(__FILE__) & plugin_dir_url(__FILE__) b ut nothing works.
Here is my code:

if (!class_exists( 'DiliviBlocks' )) {

    class DiliviBlocks{
        public function __construct() {
            $this->setup_actions();
            $this->plugin_dir = plugin_dir_path(__FILE__);
        }
        
        /**
         * Setting up Hooks
         */
        public function setup_actions() {
            register_activation_hook(  plugin_dir_path(__FILE__), array( $this, 'activate_plugin' ) );
        }

        /**
         * Activate callback
         */
        public function activate_plugin() {
            add_action( 'wp_enqueue_scripts', array($this, 'dilivi_blocks_scripts') );
            add_shortcode( 'vans_search_form', array($this, 'vans_search_form_shortcode') );
        }

        public function dilivi_blocks_scripts() {
            wp_enqueue_style( 'dilivi-blocks-plugin-css',  plugin_dir_path(__FILE__) . '/assets/css/styles.css');
            wp_enqueue_script( 'dilivi-blocks-plugin-jquery', plugin_dir_path(__FILE__) . '/assets/js/jquery.js' );
            wp_enqueue_script( 'dilivi-blocks-plugin-js', plugin_dir_path(__FILE__) . '/assets/js/scripts.js' );
        }

        public function vans_search_form_shortcode($atts) {
            return 'Hello World';
        }

    }

    $diliviBlocks = new DiliviBlocks();

 }

Please help me. I’m stuck

 

✔️Solution:

The documentation states that you only need the plugin directory name, and the name of the initial file. You should not include the whole path to it.

If your plugin contains only the file, than just __FILE__ would suffice.

In your case, it will probably be something like dilivi-blocks/divili-blocks.php.

Also, be aware that functions will only be executed once — when your plugin is activated. If it is already active, the functions will not execute. You should know that enqueueing stylesheets and the like will have to be done every time a page loads, so binding them to a single-use hook will not work. The wp_enqueue_scripts hook should be used for that.

Leave a Reply

Your email address will not be published.

Back to top button