概要
elementor/template-library/after_get_source_data
は、Elementorのテンプレートライブラリからソースデータを取得した後に発動するフィルタです。このフィルタを使用することで、取得したデータに対してカスタマイズや追加の処理を行うことができます。これにより、開発者は自分のニーズに応じた柔軟な機能を実装することが可能になります。このフィルタは、主に以下の目的で使用されます。
- テンプレートデータのカスタマイズ
- データのフィルタリングや加工
- セットアップ時に追加情報の挿入
- 特定条件下でのデータ条件付き表示
- カスタムタイプのテンプレートの追加
- テンプレートの取得方法の変更
構文
add_filter('elementor/template-library/after_get_source_data', 'your_custom_function');
パラメータ
$data
– 取得したテンプレートデータの配列$source
– データソースの識別子
戻り値
フィルタを通過した後のテンプレートデータの配列が返されます。
使用可能なプラグイン・ワードプレスのバージョン
- 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_filter('elementor/template-library/after_get_source_data', function($data, $source) {
if ($source === 'default') {
$data['new_key'] = 'New Value'; // 新しいキーと値の追加
}
return $data;
});
このサンプルは、デフォルトのテンプレートソースから取得したデータに新しいキーと値を追加します。
サンプル2: データのフィルタリング
add_filter('elementor/template-library/after_get_source_data', function($data, $source) {
if ($source === 'custom') {
return array_filter($data, function($item) {
return $item['status'] === 'active'; // アクティブな項目のみを返す
});
}
return $data;
});
このコードは、カスタムソースから取得したデータからアクティブな項目のみをフィルタリングして返します。
サンプル3: 条件付き表示の実装
add_filter('elementor/template-library/after_get_source_data', function($data, $source) {
if ($source === 'special') {
foreach ($data as &$item) {
if ($item['id'] === '123') {
$item['visible'] = true; // IDが123のアイテムを表示
} else {
$item['visible'] = false; // 他のアイテムは非表示
}
}
}
return $data;
});
このサンプルでは、特定の条件に基づいてアイテムの可視性を制御します。
サンプル4: カスタムデータの追加
add_filter('elementor/template-library/after_get_source_data', function($data, $source) {
if ($source === 'custom_source') {
$data[] = ['name' => 'Custom Item', 'type' => 'custom']; // カスタムアイテムを追加
}
return $data;
});
このサンプルは、カスタムソースから取得したデータに新しいカスタムアイテムを追加します。
サンプル5: ログの記録
add_filter('elementor/template-library/after_get_source_data', function($data, $source) {
error_log(print_r($data, true)); // 取得したデータをログに記録
return $data;
});
このコードは、取得したデータをエラーログに出力します。デバッグに便利です。