概要
elementor/template-library/after_save_template
アクションは、Elementorプラグイン内でテンプレートが保存された後に実行されるフックです。このアクションを使用することで、テンプレートが保存された際のカスタム処理を追加することができます。一般的にこのアクションは次のような場面で利用されます。
- カスタムログ記録の実装
- テンプレートの変更点を追跡
- メール通知の送信
- 再利用可能なコンテンツの作成
- 外部APIへのデータ送信
- データベースの更新やメタ情報の追加
構文
do_action('elementor/template-library/after_save_template', $template_id, $data);
パラメータ
$template_id
: 保存されたテンプレートのID$data
: テンプレートに関連するデータ
戻り値
このアクションは戻り値を持ちません。
使用可能なプラグインのバージョン
- Elementor: 3.X以降
- WordPress: 5.X以降
サンプルコード
サンプルコード 1: テンプレート保存時のログ記録
このコードは、テンプレートが保存されるたびに、保存されたテンプレートのIDをログファイルに記録します。
add_action('elementor/template-library/after_save_template', function($template_id) {
error_log("Template saved: ID " . $template_id);
});
引用元: https://example.com/template-logging
サンプルコード 2: メール通知の送信
テンプレートが保存された際に、管理者にメール通知を送信します。
add_action('elementor/template-library/after_save_template', function($template_id) {
$admin_email = get_option('admin_email');
wp_mail($admin_email, 'テンプレートが保存されました', 'テンプレートID: ' . $template_id);
});
引用元: https://example.com/email-notification
サンプルコード 3: カスタムデータの追加
テンプレートが保存される際に、追加のカスタムメタデータをデータベースに保存します。
add_action('elementor/template-library/after_save_template', function($template_id, $data) {
update_post_meta($template_id, 'custom_meta_key', 'custom_meta_value');
});
引用元: https://example.com/add-custom-meta
サンプルコード 4: 外部APIへのデータ送信
テンプレート保存後に、外部APIにデータを送信します。
add_action('elementor/template-library/after_save_template', function($template_id, $data) {
$response = wp_remote_post('https://example.com/api/track-template', [
'body' => ['template_id' => $template_id],
]);
});
引用元: https://example.com/api-integration
サンプルコード 5: テンプレートの変更履歴を保存
このコードは、新しいテンプレートが保存されると、変更履歴をデータベースに追加します。
add_action('elementor/template-library/after_save_template', function($template_id, $data) {
global $wpdb;
$wpdb->insert('template_history', ['template_id' => $template_id, 'change_date' => current_time('mysql')]);
});
引用元: https://example.com/save-history
この関数のアクションでの使用可能性
アクション名 | 使用例 |
---|---|
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 |