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

概要

elementor/document/after_save アクションは、Elementorのドキュメントが保存された後に実行されるフックです。このフックを使用することで、ユーザーがページを保存した際に特定の処理を実行することが可能です。このアクションは以下のような機能実装に使われることがよくあります。

  1. 保存時にカスタムメタデータの追加
  2. コンテンツ分析やSEOのチェック
  3. バックアップデータの保存
  4. インターネット上での通知サービスへの連携
  5. ユーザーへの青信号電子メールの送信
  6. トラッキングツールへの情報送信

構文

add_action('elementor/document/after_save', 'your_callback_function', 10, 2);

パラメータ

  • $document: 保存されたElementorドキュメントのインスタンス。
  • $data: 保存されたデータ配列。

戻り値

このアクションには戻り値はありません。

使用可能なプラグインとワードプレスのバージョン

  • 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: カスタムメタデータの追加

このコードは、Elementorのドキュメントが保存された後に、カスタムメタデータを追加します。

add_action('elementor/document/after_save', function($document) {
    $meta_key = '_custom_meta_key';
    $meta_value = 'custom value';

    $post_id = $document->get_post_id();
    update_post_meta($post_id, $meta_key, $meta_value);
});

サンプルコード 2: バックアップデータの保存

このサンプルでは、ドキュメントが保存されるたびにバックアップを作成します。

add_action('elementor/document/after_save', function($document) {
    $post_id = $document->get_post_id();
    $content = $document->get('settings');

    // バックアップ作成
    $backup_key = '_backup_' . $post_id;
    update_post_meta($post_id, $backup_key, serialize($content));
});

サンプルコード 3: SEOチェックの実行

このコードは、Elementorのドキュメントが保存された後にSEOチェックを行います。

add_action('elementor/document/after_save', function($document) {
    $post_id = $document->get_post_id();

    // SEOチェック(仮)
    if (check_seo_score($post_id) < 80) {
        // 貧弱なSEOスコアに関する通知を送信
        notify_user($post_id);
    }
});

サンプルコード 4: コンテンツ分析の実行

このコードは、保存時にコンテンツ分析を行います。

add_action('elementor/document/after_save', function($document) {
    $content = $document->get('settings')['content'];

    // 簡単なコンテンツ分析(仮)
    analyze_content($content);
});

サンプルコード 5: ユーザーへの電子メール送信

このコードは、ドキュメントの保存が行われた後にユーザーに通知メールを送信します。

add_action('elementor/document/after_save', function($document) {
    $post_id = $document->get_post_id();
    $user_email = get_the_author_meta('user_email', $document->get_author_id());

    wp_mail($user_email, 'Your document has been saved!', 'The document with ID ' . $post_id . ' has been successfully saved.');
});

これらのサンプルコードはすべて、フックelementor/document/after_saveを活用し、様々な動作を他の機能と組み合わせるために使用されています。

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


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