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

概要

Elementor の elementor/document/config アクションは、Elementor エディタのドキュメント設定を変更するために使用されます。このアクションは、Elementor を使用してウェブサイトのカスタマイズを行う際に特に有用です。具体的には、次のような機能を実装する際によく使用されます。

  1. 特定のユーザー役割に応じたオプションの制御
  2. テンプレートの条件付きロジックの実装
  3. ドキュメントのメタデータの変更
  4. ウィジェットの可用性の制御
  5. 言語や地域に基づくカスタマイズ
  6. スタイルやスクリプトの動的変更

構文

add_action('elementor/document/config', function($document, $config) {
    // ここに処理を書く
});

パラメータ

  • $document: 現在のドキュメントを示す Elementor ドキュメントオブジェクト。
  • $config: ドキュメント設定の配列。

戻り値

このアクション自体は何も返しません。

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

  • Elementor: 3.0.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/document/config', function($document, $config) {
    if (!current_user_can('administrator')) {
        unset($config['widgets']['my_custom_widget']);
    }
});

このコードは、管理者以外のユーザーが特定のウィジェット(my_custom_widget)を表示できないようにします。

サンプルコード 2: ドキュメントのメタデータの変更

add_action('elementor/document/config', function($document, $config) {
    $config['meta']['custom_key'] = 'custom_value';
});

この例では、Elementor ドキュメントのメタデータにカスタムキーと値を追加します。

サンプルコード 3: 言語に応じたスタイルの挿入

add_action('elementor/document/config', function($document, $config) {
    if (get_locale() == 'ja_JP') {
        $config['styles'][] = 'path_to_japanese_style.css';
    }
});

このコードは、日本語の locale の場合に特定のスタイルシートを追加します。

サンプルコード 4: 特定の条件に基づくウィジェットの無効化

add_action('elementor/document/config', function($document, $config) {
    if ($document->get_settings('custom_condition')) {
        unset($config['widgets']['another_widget']);
    }
});

custom_condition の設定が TRUE の場合に特定のウィジェットを無効にします。

サンプルコード 5: ドキュメントのタイトル変更

add_action('elementor/document/config', function($document, $config) {
    if ($document->get_title() == 'Old Title') {
        $document->set_title('New Title');
    }
});

この例では、ドキュメントのタイトルが特定の条件を満たす場合に新しいタイトルに変更します。

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


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