Elementor 开发者文档

title: "Save Editor Data" post_status: publish comment_status: open taxonomy: category: - elementor-developers-docs post_tag: - Hooks - Src - Repos


Save Editor Data

Elementor has a hook that lets developers run code after the user has saved data in the editor.

Hook Details

Hook Arguments

Argument Type Description
post_id integer The post ID.
editor_data array A list of Elementor elements

Examples

A good example of using this hook is when you want to log user activities. Each time the user saves Elementor data, you can log this activity:

/**
 * Log user activity when the user saves Elementor data.
 *
 * @since 1.0.0
 * @param int   $post_id     The ID of the post.
 * @param array $editor_data The editor data.
 */
function log_saved_elementor_data( $post_id, $editor_data ) {

    // Interact with your activity log plugin
    insert_activity_log_data(
        [
            'action' => 'saved',
            'object_type' => 'Elementor Data',
            'object_id' => $post_id,
            'object_name' => get_the_title( $post_id ),
        ]
    );

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

Another example is when you want to clear the cache each time the user updates a post:

/**
 * Clear old cache when the user saves new Elementor data.
 *
 * @since 1.0.0
 * @param int   $post_id     The ID of the post.
 * @param array $editor_data The editor data.
 */
function clear_cache_when_updating_elementor( $post_id, $data ) {

    if ( get_post_status( $post_id ) != 'publish' ) {
        return;
    }

    // Interact with your caching plugin
    clear_old_cache_data_for_your_post( $post_id );

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