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

概要

elementor/app/init アクションフックは、Elementorが初期化する際に実行される処理を追加するために使用されます。このフックは、Elementorの設定やカスタマイズを行う際に役立つことが多く、以下のような機能を実装する際に好まれます。

  1. Elementorのウィジェットやテンプレートの登録
  2. カスタムスタイルやスクリプトの追加
  3. Elementorの設定パラメータの変更
  4. Webサイトのデフォルトレイアウトのカスタマイズ
  5. 特定の条件に基づくウィジェットの表示・非表示
  6. 他のプラグインとの統合や相互作用の処理

構文

add_action('elementor/app/init', 'your_custom_function');

function your_custom_function() {
    // Your custom code here
}

パラメータ

  • このアクション自体には引数はありません。

戻り値

  • このアクションは、実行中の処理に関して特定の戻り値を持たない。

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

  • Elementor: バージョン 2.0以上
  • WordPress: バージョン 4.0以上

サンプルコード

サンプルコード1: カスタムウィジェットの登録

add_action('elementor/app/init', function() {
    ElementorPlugin::instance()->widgets_manager->register_widget_type(new ElementorYour_Custom_Widget());
});

このコードはElementorにカスタムウィジェットを登録します。ウィジェットクラス ElementorYour_Custom_Widget を自分で定義する必要があります。

サンプルコード2: カスタムCSSの追加

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

このコードは、Elementorで使用されるカスタムCSSファイルを追加します。

サンプルコード3: ウィジェットの条件付き表示

add_action('elementor/app/init', function() {
    if (is_single()) {
        ElementorPlugin::instance()->widgets_manager->register_widget_type(new ElementorYour_Conditional_Widget());
    }
});

このコードは、投稿ページでのみウィジェットを登録します。

サンプルコード4: フィルターフックを使用した設定変更

add_action('elementor/app/init', function() {
    $settings = ElementorPlugin::instance()->settings;
    $settings->set('some_setting', 'new_value');
});

このコードは、Elementorの設定をカスタマイズするための例です。

サンプルコード5: スクリプトの追加

add_action('elementor/app/init', function() {
    wp_enqueue_script('custom-elementor-script', get_template_directory_uri() . '/js/custom-elementor.js', array('jquery'), null, true);
});

このコードは、ElementorにカスタムJavaScriptファイルを追加します。

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

アクション 使用可能性
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

この表では、Elementorのアクションフックelementor/app/initが、他のWordPressアクションフックで使用されるかどうかを示しています。このフックは、Elementor固有の初期化時に特に重要です。

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


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