0

I am building a plugin based on a boilerplate gerenrated on wppb.me The problem I am having is getting the register_activation_hook to actually execute without following it with add_action

This is the code segment in promo-card.php

define('PROMO_DIR_PATH', plugin_dir_path(FILE));

function activate_promo_card() { log_me('activate_promo_card function start'); /* debug logger call */ require_once (PROMO_DIR_PATH . 'includes/class-promo-card-activator.php'); $promoCreate=new Promo_Card_Activator; $promoCreate->promo_db_master_create(); $promoCreate->promo_db_detail_create(); }

function deactivate_promo_card() { log_me('deactivate_promo_card function start'); /* debug logger call */ require_once (PROMO_DIR_PATH . 'includes/class-promo-card-deactivator.php'); Promo_Card_Deactivator::deactivate(); }

register_activation_hook( PROMO_DIR_PATH, 'activate_promo_card' ); //add_action('init', 'activate_promo_card'); register_deactivation_hook( PROMO_DIR_PATH, 'deactivate_promo_card' ); //add_action('init', 'deactivate_promo_card');

If I use the add_action then it fires the function in all cases which is not good. What am I missing?

1 Answer 1

1

The register_activation_hook() function takes two parameters: the first parameter is the path to the main plugin file, and the second parameter is the name of the function to be executed when the plugin is activated. In your case, you are passing the plugin directory path to the register_activation_hook() function instead of the path to the main plugin file.

To fix this, you need to change the first parameter of the register_activation_hook() function to the path to the main plugin file.

define('PROMO_DIR_PATH', plugin_dir_path(__FILE__));

function activate_promo_card() {
  // ...
}

function deactivate_promo_card() {
  // ...
}

register_activation_hook( __FILE__, 'activate_promo_card' );
register_deactivation_hook( __FILE__, 'deactivate_promo_card' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.