プラグインElementorのelementor/maintenance_mode/mode_changedアクションの使用方法・解説

概要

elementor/maintenance_mode/mode_changedは、Elementorのメンテナンスモードが変更されたときに発火します。このフックは、ウェブサイトのメンテナンスステータスを監視し、その変更に応じて特定のアクションを実行するために使用されます。一般的に、以下のような機能を実装する際に役立ちます。

  1. メンテナンスモードの状態に基づくメール通知の送信
  2. メンテナンスモード時の特定のフロントエンドコンテンツの表示・非表示
  3. メンテナンスモードの変更履歴を保存するためのログ機能
  4. 独自のメンテナンスモードページをレンダリングする
  5. メンテナンスモードの状態を基にしたユーザーアクセスの制御
  6. 他のプラグインやテーマがメンテナンスモードの設定に従うように連携させる

構文

add_action('elementor/maintenance_mode/mode_changed', 'your_custom_function');

function your_custom_function($mode) {
    // ここに処理を実装
}

パラメータ

  • $mode: 変更後のメンテナンスモードの状態(例: 有効, 無効)

戻り値

特に戻り値は存在しませんが、フック内で実行する関数によって結果が生成されます。

使用可能なプラグインとバージョン

  • Elementorバージョン: 3.0以降
  • WordPressバージョン: 5.0以降

この関数のアクションでの使用可能性

アクション名 使用例
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード

サンプルコード1: メール通知の送信

add_action('elementor/maintenance_mode/mode_changed', 'notify_admin_on_mode_change');

function notify_admin_on_mode_change($mode) {
    $to = 'admin@example.com';
    $subject = 'Maintenance Mode Updated';
    $message = 'The maintenance mode has been changed to: ' . $mode;
    wp_mail($to, $subject, $message);
}

このコードは、メンテナンスモードが変更された際に管理者にメール通知を送信します。

サンプルコード2: カスタムメンテナンスページの表示

add_action('elementor/maintenance_mode/mode_changed', 'custom_maintenance_page');

function custom_maintenance_page($mode) {
    if ($mode === 'enabled') {
        include plugin_dir_path(__FILE__) . 'custom-maintenance.php';
        exit; // 他の処理を中止
    }
}

メンテナンスモードが有効になると、カスタムのメンテナンスページが表示されるようにします。

サンプルコード3: モード変更履歴の保存

add_action('elementor/maintenance_mode/mode_changed', 'log_mode_change');

function log_mode_change($mode) {
    $log = 'Maintenance mode changed to: ' . $mode . ' on ' . date('Y-m-d H:i:s');
    file_put_contents(plugin_dir_path(__FILE__) . 'maintenance_log.txt', $log.PHP_EOL, FILE_APPEND);
}

このサンプルは、メンテナンスモードの変更履歴をログファイルに保存します。

サンプルコード4: ユーザーアクセスの制御

add_action('elementor/maintenance_mode/mode_changed', 'control_user_access');

function control_user_access($mode) {
    if ($mode === 'enabled' && !current_user_can('manage_options')) {
        wp_die('This site is under maintenance. Please check back later.');
    }
}

メンテナンスモードが有効な場合、一般ユーザーにアクセスを制限します。

サンプルコード5: 外部APIへのリクエスト

add_action('elementor/maintenance_mode/mode_changed', 'notify_external_api_on_mode_change');

function notify_external_api_on_mode_change($mode) {
    wp_remote_post('https://example.com/api/notify', [
        'body' => json_encode(['mode' => $mode]),
        'headers' => ['Content-Type' => 'application/json']
    ]);
}

メンテナンスモードが変更された際に、外部APIに通知を送信します。

この関数について質問する


上の計算式の答えを入力してください