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

概要

elementor/document/save_versionは、Elementorがドキュメント(ページや投稿)のバージョンを保存する際に発火するアクションです。このフックを使用することで、ユーザーが行った変更をトラッキングしたり、独自の処理を施したりすることが可能です。主に以下のような場面で利用されます。

  1. ユーザー変更の履歴管理
  2. 自動バックアップ機能の実装
  3. 他のシステムとのデータ同期
  4. 変更通知の送信
  5. 特定条件下でのカスタム処理実行
  6. 分析やトラッキングの取得と記録

構文

add_action('elementor/document/save_version', 'your_custom_function', 10, 2);

パラメータ

  • $post_id – 保存されるポストのID
  • $data – 保存されるデータの配列

戻り値

このアクションには戻り値はありません。処理を実行するためのフックとして機能します。

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

  • Elementorバージョン: 3.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: 変更履歴の保存

function save_version_history( $post_id, $data ) {
    $history = get_post_meta( $post_id, 'version_history', true ) ?: [];
    $history[] = $data;
    update_post_meta( $post_id, 'version_history', $history );
}
add_action( 'elementor/document/save_version', 'save_version_history', 10, 2 );

このコードは、Elementorでドキュメントが保存されるたびに変更履歴をカスタムフィールドに保存します。これにより、過去のバージョンを追跡することができます。

サンプル2: バージョンに固有の処理を追加

function custom_version_saved_action( $post_id, $data ) {
    if( isset($data['title']) ) {
        error_log('Version saved for post: ' . $data['title']);
    }
}
add_action( 'elementor/document/save_version', 'custom_version_saved_action', 10, 2 );

このコードは、ドキュメントのタイトルをログに記録します。バージョンが保存されるたびに、変更の内容を追跡できます。

サンプル3: 外部APIへの通知

function notify_external_api( $post_id, $data ) {
    $response = wp_remote_post( 'https://example.com/api/notify', [
        'body' => json_encode(['post_id' => $post_id, 'data' => $data]),
        'headers' => ['Content-Type' => 'application/json']
    ]);
}
add_action( 'elementor/document/save_version', 'notify_external_api', 10, 2 );

このコードは、ドキュメントが保存されるたびに外部APIに通知を送信します。このようにして、他のシステムとの連携を行うことができます。

サンプル4: カスタム通知の送信

function send_custom_notification( $post_id, $data ) {
    $user_email = get_post_meta( $post_id, 'author_email', true );
    wp_mail( $user_email, 'Document Saved', 'Your document has been saved successfully.');
}
add_action( 'elementor/document/save_version', 'send_custom_notification', 10, 2 );

このコードは、ドキュメントが保存された際に著者に通知メールを送信します。重要な変更があったときに役立ちます。

サンプル5: カスタムフィールドの更新

function update_custom_field_on_save( $post_id, $data ) {
    update_post_meta( $post_id, '_last_saved_version', time() );
}
add_action( 'elementor/document/save_version', 'update_custom_field_on_save', 10, 2 );

このコードは、ドキュメントが保存されるたびに最後の保存時間をカスタムフィールドに更新します。これにより、いつ最後に変更が行われたかを簡単に追跡できます。

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


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