I have created a plugin that register a new CPT but it doesnt work because i have to flush_rewrite_rules() (i can do this by going into the permalink setting page and save, but its not the right way)

I have read inside the wordpress codex that i have to flush_rewrite_rules() on activation, but its not working cuz flush_rewrite_rules() runs before i register the CPT, to work it must run after...

I am using this https://github.com/DevinVinson/WordPress-Plugin-Boilerplate

And it has a class that runs on activation of the plugin i have added there this flush_rewrite_rules()


The boilerplate i have used is already structured with activation/deactivation classes, for example

function activate_l_erbario() {
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-l-erbario-activator.php';

register_activation_hook( __FILE__, 'activate_l_erbario' );

and inside class-l-erbario-activator.php i have

class L_Erbario_Activator {
    public static function activate() {





/admin/class-l-erbario-admin.php -> here i register the CPT

/includes/class-l-erbario.php -> and here i add the add_action with the CPT

  • 1
    So if you need it to run after the post type is registered and when the plugin is activated, you'll probably want use the activation function to hook flush_rewrite_rules() onto something such as admin_init, rather than call it directly Commented Nov 3, 2020 at 13:32
  • On the Wordpress Codex it says this: // ATTENTION: This is only done during plugin activation hook in this example! // You should NEVER EVER do this on every page load!! flush_rewrite_rules();
    – NeaMitika
    Commented Nov 3, 2020 at 16:18
  • OK, then how is that getting run before your CPT registration? Which hook are you using to register your CPT?
    – Rup
    Commented Nov 4, 2020 at 17:25
  • I have edited the main question on how the plugin handles activation
    – NeaMitika
    Commented Nov 5, 2020 at 7:15
  • Thanks. How does it handle the registration? But I'd guess the simplest thing to do is to do the registration again in the activation hook, before calling flush_rewrite_rules.
    – Rup
    Commented Nov 5, 2020 at 8:50

1 Answer 1


After a couple of days trying and trying and trying... i asked a youtuber who develops using the same Wordpress Plugin Boilerplate and he pointed me to this article:


It adds an option inside_option table and it will flush rewrite rules based on that :) It worked perfectly

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.