Table of Contents
Integrating with coming soon mode
This guide provides examples for third-party developers and hosting providers on how to integrate their systems with WooCommerce's coming soon mode. For more details, please read the developer blog post. For site visibility settings, please refer to the admin documentation.
Introduction
WooCommerceのカミングスーンモードでは、作業中に一時的にサイトを見えなくすることができます。このガイドでは、この機能をシステムに統合する方法、サイトの可視性設定が変更されたときにサーバーキャッシュをクリアする方法、coming soon modeを他のプラグインと同期させる方法を紹介します。
Prerequisites
- PHPおよびWordPressの開発に精通していること。
Step-by-step instructions
Clear server cache on site visibility settings change
When the site's visibility settings change, it may be necessary to clear a server cache to apply the changes and re-cache customer-facing pages. The update_option hook can be used to achieve this.
add_action( 'update_option_woocommerce_coming_soon', 'clear_server_cache', 10, 3 );
add_action( 'update_option_woocommerce_store_pages_only', 'clear_server_cache', 10, 3 );
function clear_server_cache( $old_value, $new_value, $option ) {
// Implement your logic to clear the server cache.
if ( function_exists( 'your_cache_clear_function' ) ) {
your_cache_clear_function();
}
}
Clear server cache on template changes
By default, Coming-soon pages are set with Cache-Control: max-age=60 header. This setting enables CDNs and other caching mechanisms to cache the page for 60 seconds, balancing the need for efficient performance with reasonable update times.
ユーザがcoming soonテンプレートを変更した場合、クライアント側のキャッシュが期限切れになったときに変更が即座に反映されるように、キャッシュをすべてパージすることをお勧めします。
You can use the save_post_wp_template, save_post_wp_template_part, and save_post_wp_global_styles hooks to detect when a template is updated and trigger the cache purge.
add_action( 'save_post_wp_template', 'purge_cache_on_template_change', 10, 3 );
add_action( 'save_post_wp_template_part', 'purge_cache_on_template_change', 10, 3 );
add_action( 'save_post_wp_global_styles', 'purge_cache_on_template_change', 10, 3 );
function purge_cache_on_template_change( $post_id, $post, $update ) {
// Check if the template is associated with the coming soon mode.
if ( 'coming-soon' === $post->post_name ) {
// Implement your logic to clear the server cache.
if ( function_exists( 'your_cache_clear_function' ) ) {
your_cache_clear_function();
}
}
}
Syncing coming soon mode with other plugins
近日公開モードは、プラグインやアプリケーションからプログラムで同期させることができます。以下は使用例です:
- メンテナンスモード・プラグインとの統合
- ホスティングプロバイダーのcoming soonモードとの統合。
Trigger from WooCommerce
以下の例では、coming soon modeオプションが更新されたときにプラグインのステータスを設定するようなコードを実行できます:
add_action( 'update_option_woocommerce_coming_soon', 'sync_coming_soon_to_other_plugins', 10, 3 );
function sync_coming_soon_to_other_plugins( $old_value, $new_value, $option ) {
$is_enabled = $new_value === 'yes';
// Implement your logic to sync coming soon status.
if ( function_exists( 'your_plugin_set_coming_soon' ) ) {
your_plugin_set_coming_soon( $is_enabled );
}
}
Trigger from other plugins
You can use the following example to enable or disable WooCommerce coming soon mode from another plugin by directly updating woocommerce_coming_soon option:
function sync_coming_soon_from_other_plugins( $is_enabled ) {
// Check user capability.
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( 'You do not have sufficient permissions to access this page.' );
}
// Set coming soon mode.
if ( isset( $is_enabled ) ) {
update_option( 'woocommerce_coming_soon', $is_enabled ? 'yes' : 'no' );
}
}
2-way sync with plugins
If 2-way sync is needed, use the following example where update_option will not recursively call sync_coming_soon_from_other_plugins:
add_action( 'update_option_woocommerce_coming_soon', 'sync_coming_soon_to_other_plugins', 10, 3 );
function sync_coming_soon_to_other_plugins( $old_value, $new_value, $option ) {
$is_enabled = $new_value === 'yes';
// Implement your logic to sync coming soon status.
if ( function_exists( 'your_plugin_set_coming_soon' ) ) {
your_plugin_set_coming_soon( $is_enabled );
}
}
function sync_coming_soon_from_other_plugins( $is_enabled ) {
// Check user capability.
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( 'You do not have sufficient permissions to access this page.' );
}
if ( isset( $is_enabled ) ) {
// Temporarily remove the action to prevent a recursive call.
remove_action( 'update_option_woocommerce_coming_soon', 'sync_coming_soon_to_other_plugins', 10, 3 );
// Set coming soon mode.
update_option( 'woocommerce_coming_soon', $is_enabled ? 'yes' : 'no' );
// Re-add the action.
add_action( 'update_option_woocommerce_coming_soon', 'sync_coming_soon_to_other_plugins', 10, 3 );
}
}
One-way binding with option override
We could also programmatically bind the coming soon option from another plugin by overriding the woocommerce_coming_soon option. This is advantageous since it simplifies state management and prevents possible out-of-sync issues.
In the following example, we're binding the coming soon option from another plugin by overriding the woocommerce_coming_soon option.
add_filter( 'pre_option_woocommerce_coming_soon', 'override_option_woocommerce_coming_soon' );
function override_option_woocommerce_coming_soon( $current_value ) {
// Implement your logic to sync coming soon status.
if ( function_exists( 'your_plugin_is_coming_soon' ) ) {
return your_plugin_is_coming_soon() ? 'yes' : 'no';
}
return $current_value;
}
add_filter( 'pre_update_option_woocommerce_coming_soon', 'override_update_woocommerce_coming_soon', 10, 2 );
function override_update_woocommerce_coming_soon( $new_value, $old_value ) {
// Check user capability.
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( 'You do not have sufficient permissions to access this page.' );
}
// Implement your logic to sync coming soon status.
if ( function_exists( 'your_plugin_set_coming_soon' ) ) {
your_plugin_set_coming_soon( $new_value === 'yes' );
}
}
Custom exclusions filter
開発者は、近日公開の保護をバイパスするカスタム除外を追加することが可能です。これは、特定のIPアドレスの画面を常に回避したり、特定のランディングページを利用可能にするような除外に便利です。
Disabling coming soon in all pages
WooCommerceのcoming soonモードと似たような動作をする他の機能がある場合、意図しないコンフリクトを引き起こす可能性があります。すべての顧客向けページを除外することで、カミングスーンモードを無効にすることができます。以下はその例です:
add_filter( 'woocommerce_coming_soon_exclude', function() {
return true;
}, 10 );
Disabling coming soon except for a specific page
Use the following example to exclude a certain page based on the page's ID. Replace <page-id> with your page identifier:
add_filter( 'woocommerce_coming_soon_exclude', function( $is_excluded ) {
if ( get_the_ID() === <page-id> ) {
return true;
}
return $is_excluded;
}, 10 );
Custom share links
次の例は、カスタム共有コードと統合する方法を示しています。クッキーやその他のストレージを使用して、ユーザーがサイト内を移動する際のアクセスを永続化することをお勧めします:
add_filter( 'woocommerce_coming_soon_exclude', function( $exclude ) {
// Implement your logic to get and validate share code.
if ( function_exists( 'your_plugin_get_share_code' ) && function_exists( 'your_plugin_is_valid_share_code' ) ) {
$share_code = your_plugin_get_share_code();
if ( your_plugin_is_valid_share_code( $share_code ) ) {
return true;
}
}
return $exclude;
} );
Extend "Apply to store pages only" setting
When using the Apply to store pages only setting, you may want to add a custom page to the list of store pages which will be restricted by coming soon mode. You can use the following example to add a custom page:
add_filter( 'woocommerce_store_pages', function( $pages ) {
$page = get_page_by_path( 'your-page-slug' );
if ( $page ) {
$pages[] = $page->ID;
}
return $pages;
} );