概要
elementor/dynamic_tags/before_render
アクションは、Elementorプラグインのダイナミックタグがレンダリングされる前に実行されます。このフックを使用すると、ダイナミックタグの出力をカスタマイズしたり、タグに対して特定の処理を実行したりすることができます。このアクションは、以下の機能を実装する際によく使われます。
- ダイナミックタグの表示内容の変更
- 特定の条件に基づいて表示の切り替え
- 外部データソースと統合したタグの作成
- タグのスタイリングやクラスの追加
- カスタムフィールドの値を取得して表示
- タグのキャッシュ管理
構文
do_action( 'elementor/dynamic_tags/before_render', $instance );
パラメータ
$instance
: 現在のダイナミックタグのインスタンス。
戻り値
- このアクション自体は特に戻り値を持たず、カスタム処理が実行されます。
使用可能なプラグインElementorのバージョン
- Elementor 3.0 以降
使用可能なワードプレスのバージョン
- WordPress 5.0 以降
サンプルコード
サンプル1: タグのカスタム出力
add_action( 'elementor/dynamic_tags/before_render', function( $instance ) {
if ( 'my_custom_tag' === $instance->get_name() ) {
$instance->set_raw_data( 'カスタム出力' );
}
} );
このコードは、特定のカスタムダイナミックタグが描画される前に、その出力を「カスタム出力」に置き換えます。
サンプル2: 条件に基づいた表示
add_action( 'elementor/dynamic_tags/before_render', function( $instance ) {
if ( ! is_user_logged_in() ) {
$instance->set_raw_data( 'ログインしていないユーザーには表示しません。' );
}
} );
このコードは、ユーザーがログインしていない場合に、特定のタグの表示を変更します。
サンプル3: 外部APIからのデータ取得
add_action( 'elementor/dynamic_tags/before_render', function( $instance ) {
if ( 'external_api_data' === $instance->get_name() ) {
$response = wp_remote_get( 'https://api.example.com/data' );
if ( is_array( $response ) && ! is_wp_error( $response ) ) {
$data = json_decode( wp_remote_retrieve_body( $response ), true );
$instance->set_raw_data( $data['desired_field'] );
}
}
} );
このコードは、外部APIからデータを取得し、特定のダイナミックタグにそのデータを設定します。
サンプル4: CSSクラスの追加
add_action( 'elementor/dynamic_tags/before_render', function( $instance ) {
$instance->add_render_attribute( '_wrapper', 'class', 'my-custom-class' );
} );
このサンプルでは、ダイナミックタグに特定のCSSクラスを追加します。
サンプル5: カスタムフィールドの値の表示
add_action( 'elementor/dynamic_tags/before_render', function( $instance ) {
if ( 'custom_field' === $instance->get_name() ) {
$field_value = get_post_meta( get_the_ID(), 'my_custom_field', true );
$instance->set_raw_data( $field_value );
}
} );
このコードは、現在の投稿のカスタムフィールドの値を特定のダイナミックタグに設定します。
この関数のアクションでの使用可能性
アクション名 | 使用例 |
---|---|
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 |