概要
elementor/{$element_type}/print_template
は、Elementorにおけるカスタムウィジェットやセクションの描画を拡張するためのフックです。このアクションは、特定のElementorウィジェットタイプにカスタムコンテンツを追加する際に特に有用です。主に以下のような機能を実装するために使用されます。
- Elementorウィジェットのカスタムテンプレートの追加
- 特定の条件に基づくウィジェットの表示/非表示設定
- ユーザーのロールに応じたダイナミックコンテンツの表示
- AJAXを使用したウィジェットの追加機能
- スタイルやスクリプトのオプションのカスタマイズ
- セクションやウィジェットの動的データのフィルタリング
構文
add_action( 'elementor/{$element_type}/print_template', 'your_function_name', 10, 2 );
パラメータ
$element_type
: Elementorで使用される要素のタイプ(例:widget
、section
など)。$instance
: ウィジェットのインスタンスデータ。
戻り値
このアクションは、特に戻り値を返すわけではなく、カスタム処理を実行するためのフックです。
使用可能なプラグインバージョン
- Elementor: バージョン 2.0 以降
- WordPress: バージョン 4.7 以降
この関数のアクションでの使用可能性
アクション | 使用例 |
---|---|
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/widget/render_content', function( $widget_content, $widget ) {
if ( 'my_custom_widget' === $widget->get_name() ) {
$widget_content .= '<div>カスタムコンテンツを追加します。</div>';
}
return $widget_content;
}, 10, 2 );
このコードは、特定のウィジェット(my_custom_widget
)のコンテンツにカスタムHTMLを追加します。
サンプルコード 2
add_action( 'elementor/widget/render_content', function( $widget_content, $widget ) {
if ( is_user_logged_in() ) {
$widget_content .= '<div>ようこそ、ユーザー!</div>';
}
return $widget_content;
}, 10, 2 );
このコードは、ログインユーザーにメッセージを表示します。
サンプルコード 3
add_action( 'elementor/shortcode/after_render', function( $rendered_content, $element ) {
if ( $element->get_name() == 'my_widget' ) {
$rendered_content .= '<p>特別なメッセージ</p>';
}
return $rendered_content;
}, 10, 2 );
このコードは、特定のウィジェットの後にカスタムメッセージを追加します。
サンプルコード 4
add_action( 'elementor/widget/render_content', function( $widget_content, $widget ) {
$widget_settings = $widget->get_settings();
if ( $widget_settings['show_message'] ) {
$widget_content .= '<div>表示するメッセージ</div>';
}
return $widget_content;
}, 10, 2 );
このコードは、ウィジェットの設定に基づいてメッセージを表示します。
サンプルコード 5
add_action( 'elementor/widget/render_content', function( $widget_content, $widget ) {
if ( 'custom_section' === $widget->get_name() ) {
// セクションにカスタムスタイルを追加
$widget_content = '<div class="custom-section">' . $widget_content . '</div>';
}
return $widget_content;
}, 10, 2 );
このコードは、特定のセクションにカスタムスタイルを追加しています。