0

There is some hook that is triggered after WooCommerce Install (I mean the WooCommerce Wizard, not the plugin installation)? I didn't found a hook to do this and the Wizard Class (WC_Admin_Setup_Wizard) didn't have much hooks to achieve that. Any ideas?

The problem I'm trying to solve is that WooCommerce is setting fresh_site option to 0 (I believe is because the posts it creates in installation process). So I want reset it to 1 after installation and setup my theme. Thanks!

1 Answer 1

1

WooCommerce itself isn't setting fresh_site to 0 directly (the string fresh_site doesn't appear anywhere in WooCommerce's source code), however you are correct that it's happening because WooCommerce is creating pages.

WordPress sets a site to not be fresh like this (from wp-includes/default-filters.php):

// Mark site as no longer fresh
foreach ( array( 'publish_post', 'publish_page', 'wp_ajax_save-widget', 'wp_ajax_widgets-order', 'customize_save_after' ) as $action ) {
    add_action( $action, '_delete_option_fresh_site', 0 );
}

So when WooCommerce creates its pages, publish_page is fired and the site is no longer considered fresh.

Unfortunately there doesn't appear to be any hooks that fire specifically when the setup wizard is complete, when completing any steps in the wizard, or even when the default WooCommerce pages are created. I think I have figured out a workaround, though.

The entirety of the WooCommerce setup wizard takes place on an admin page, ?page=wc-setup, which is output by WooCommerce using the admin_init hook, at the default priority of 10. So what we can do is hook into admin_init at a higher priority (lower number), check if we're in the WooCommerce wizard, and if so, remove the hook that sets a site to not be fresh. This way nothing that happens during the setup wizard can make the site no longer fresh:

function wpse_340803_keep_setup_wizard_fresh() {
    if ( isset( $_GET['page'] ) && 'wc-setup' === $_GET['page'] ) {
        foreach ( array( 'publish_post', 'publish_page', 'wp_ajax_save-widget', 'wp_ajax_widgets-order', 'customize_save_after' ) as $action ) {
            remove_action( $action, '_delete_option_fresh_site', 0 );
        }
    }
}
add_action( 'admin_init', 'wpse_340803_keep_setup_wizard_fresh', 0 );

Since the page creation is triggered inside admin_init at priority 10, as part of the setup wizard, the _delete_option_fresh_site is no longer hooked to be fired when the pages are created, meaning that the site should still be fresh afterwards.

2
  • So, @JacobPeattie If I delete the action to delete the fresh site option, I should set it manually, right? There's no conflict in remove these actions binded to '_delete_option_fresh_site'? I mean, some plugin could crash if make use of that option? Commented Jun 18, 2019 at 13:45
  • 1
    You're not deleting the action, you're only removing it for the duration of the current request. The action still gets added by WordPress on eveyr page load, but you're just removing it during the setup wizard. Outside the setup wizard everything will function as normal. Commented Jun 18, 2019 at 14:05

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.