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

概要

elementor/safe_mode/initは、Elementorの安全モードに関連するアクションフックです。このフックは、Elementorの安全モードが有効なときに、特定の機能や処理を実行するために使用されます。安全モードは、特定のプラグインやテーマが原因で発生する問題をトラブルシューティングする際に役立ちます。

このアクションフックは、以下のような機能を実装する際によく使われます。

  1. JavaScriptやCSSの読み込み
  2. 特定のカスタムポストタイプの設定
  3. ショートコードの登録
  4. ユーザー権限の確認
  5. エラーメッセージの表示
  6. レイアウトやスタイルの条件付き変更

構文

add_action('elementor/safe_mode/init', 'your_function_name');

パラメータ

このアクションには特定のパラメータはありません。

戻り値

このアクションは特に戻り値を持ちません。

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

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: CSSの読み込み

このコードは、Elementorの安全モードが有効なときに、カスタムCSSファイルを読み込みます。

add_action('elementor/safe_mode/init', function() {
    wp_enqueue_style('my-custom-css', get_template_directory_uri() . '/css/custom.css');
});

引用元: https://developer.wordpress.org/plugins/

サンプルコード 2: カスタムポストタイプの登録

このコードは、安全モードでカスタムポストタイプを登録します。

add_action('elementor/safe_mode/init', function() {
    register_post_type('my_custom_post', [
        'label' => 'My Custom Post',
        'public' => true,
        'has_archive' => true,
    ]);
});

引用元: https://developer.wordpress.org/plugins/

サンプルコード 3: ショートコードの追加

このコードは、安全モード中にショートコードを登録します。

add_action('elementor/safe_mode/init', function() {
    add_shortcode('my_shortcode', function() {
        return '<div>This is my shortcode output!</div>';
    });
});

引用元: https://developer.wordpress.org/plugins/

サンプルコード 4: ユーザー権限の確認

安全モードで現在のユーザーが特定の権限を持っているかどうかを確認します。

add_action('elementor/safe_mode/init', function() {
    if (current_user_can('edit_posts')) {
        // 処理
    }
});

引用元: https://developer.wordpress.org/plugins/

サンプルコード 5: エラーメッセージの表示

このコードは、安全モード中にエラーメッセージを表示します。

add_action('elementor/safe_mode/init', function() {
    if (!function_exists('your_critical_function')) {
        echo '<div class="error">Critical function is not available!</div>';
    }
});

引用元: https://developer.wordpress.org/plugins/

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


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