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

概要

elementor/editor/after_save は、Elementorでエディタが編集内容を保存した後にトリガーされるアクションフックです。このフックを使用することで、ユーザーがレイアウトやコンテンツを編集した後の処理を実行することができます。以下は、このフックを使用する際に一般的な機能です。

  1. 保存されたデータのカスタム処理
  2. メール通知の送信
  3. コンテンツの監査ログ作成
  4. 他のAPIとの連携
  5. キャッシュのクリア
  6. 自動バックアップの作成

構文

add_action('elementor/editor/after_save', 'your_function_name', 10, 2);

パラメータ

  • post_id: 保存された投稿のID。
  • data: 保存されたデータの配列。

戻り値

このアクションは戻り値を持ちません。

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

  • Elementor: バージョン 3.0 以降
  • WordPress: バージョン 5.0 以降

サンプルコード

  1. 保存後にカスタムメッセージをログに出力する

    このコードは、Elementorでコンテンツが保存された後にカスタムメッセージをログに書き出します。

    add_action('elementor/editor/after_save', function($post_id, $data) {
       error_log('Elementor content saved for post ID: ' . $post_id);
    });
    

    引用元: https://elementor.com/help/wordpress-hooks-action/

  2. APIにデータを送信する

    保存されたコンテンツを外部APIに送信して処理します。

    add_action('elementor/editor/after_save', function($post_id, $data) {
       $response = wp_remote_post('https://example.com/api/save', [
           'body' => json_encode($data),
           'headers' => ['Content-Type' => 'application/json'],
       ]);
    });
    

    引用元: https://developer.wordpress.org/plugins/hooks/

  3. ユーザーに保存完了メールを送る

    コンテンツが保存された後に、管理者にメールで通知します。

    add_action('elementor/editor/after_save', function($post_id, $data) {
       $user = wp_get_current_user();
       wp_mail($user->user_email, 'Content Saved', 'Your content has been saved successfully.');
    });
    

    引用元: https://www.sitepoint.com/wordpress-email-notifications/

  4. 変更履歴をデータベースに保存する

    コンテンツが保存されるたびに、変更履歴を独自のテーブルに保存します。

    add_action('elementor/editor/after_save', function($post_id, $data) {
       global $wpdb;
       $wpdb->insert('your_changes_table', ['post_id' => $post_id, 'changes' => json_encode($data)]);
    });
    

    引用元: https://codeinwp.com/blog/wordpress-database-tables/

  5. 特定の条件に基づいてキャッシュをクリアする

    投稿タイプが特定のものである場合にキャッシュをクリアします。

    add_action('elementor/editor/after_save', function($post_id, $data) {
       $post_type = get_post_type($post_id);
       if ($post_type === 'your_custom_post_type') {
           // Clear your cache logic here
       }
    });
    

    引用元: https://www.smashingmagazine.com/2020/08/wordpress-caching-plugins/

この関数のアクションでの使用可能性

アクション 使用例
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

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


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