Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am experimenting with creating a simple plugin to create a custom post type named project but am having some trouble with the rewrite rules not been flushed on activation.

I have the main plugin file with this function:

register_activation_hook( __FILE__, 'Project_Custom_Post_Type::activate' );

Then within my class I have this:

public function activate() {
  flush_rewrite_rules();
}

My class has a construct of:

public function __construct() {
  add_action( 'init', array( $this, 'register_post_type' ), 0 );
}

I cannot see why it is not working? My deactivation flush works fine.

share|improve this question

2 Answers 2

Your string is not read as a callback. You should pass an array:

$pcpt = new Project_Custom_Post_Type;
register_activation_hook( __FILE__, array( $pcpt, 'activate' ) );

Note that init happens before plugin activation, so not callbacks from your class will be executed.

share|improve this answer

On activation, the "init" action has already run. So your flush is actually taking place just fine, but your post type is not being registered before the flush takes place. Add the code to register your post type to your activation function, before you flush the rewrite rules.

Also, yes, your activation hook call is incorrect, as toscho pointed out.

share|improve this answer
    
Ok so I now have it working with this: register_activation_hook( __FILE__, array( $project, 'activate' ) ); With my activate function including this line: $this->my_register_post_type(); All works great. I still have the my_register_post_type(); function being called on 'init' also. I presume it needs to be called in both places? –  Philip Benton Oct 14 '13 at 20:03
    
Yes, it needs to be in both places. –  Otto Oct 15 '13 at 10:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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