0

I've been looking for minutes how to pass arguments in the register_deactivation_hook as follow :

register_deactivation_hook( __FILE__, array( new Cpt, 'delete_cpt') ) );

class Cpt
{
    /**
     * Delete a custom post type
     */
    public function delete_cpt($name = 'test') {
        /**
         * delete a single data
         */
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        $GLOBALS['wpdb']->delete( 
            'cpt', 
            array( 
                'name'=>$name 
            ) 
        );
        $GLOBALS['wpdb']->show_errors();
        echo $GLOBALS['wpdb']->last_query;
        echo $name;
        die();
    }
}

The function executes but the $name var remains empty. I would like to pass the $name directly in register_deactivation_hook;

Is it at least possible ? Nothing seen anywhere.

3 Answers 3

3

Use a complete function as callback, so you can pass the argument to the method. Example:

register_deactivation_hook( 
    __FILE__,
    function() {
        $cpt = new Cpt;
        $cpt->delete_cpt( 'post_type_name' );
    }
);
2
  • This will work only with PHP >= 5.3 .
    – adrian7
    Commented Jul 13, 2014 at 21:22
  • This fit perfectly. I've been thinking about somthing similar. Thanks a lot !
    – Neovea
    Commented Jul 13, 2014 at 21:27
0

You could create a static method inside your Cpt class and call that instead with register_deactivation_hook; Then you create your object and send it data as usual.

-1

You should replace the register_deactivation_hook code line with below code:

register_deactivation_hook( __FILE__, array( 'Cpt', 'delete_cpt' ) );

1
  • That wouldn’t solve the problem. Have you tested your suggestion?
    – fuxia
    Commented Jul 13, 2014 at 17:18

Your Answer

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

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