wp_update_post via ajax from frontend

Setting:

three open Tabs in browser:

Read More

Tab 1: Admin edit post

Tab 2: Frontend view of that post in “editing mode”

Tab 3: Frontend view in “non-editing” mode (default request)

I edit the post on Tab 2 (frontend editing) and save it via AJAX (via wp_update_post). When I refresh this Tab, or Tab 3, I can see the changes.

But when I refresh Tab 1 (WP Backend edit-posts page), I see the old content in the editor, but a new revision was added. When I hard-refresh that page, it works as expected.
(This might be very confusing to editors)

So why does it work only on hard refresh, but not on normal refresh? Is there any kind of caching active? (couldn’t find anything in the codex)

Simlified Ajax save action:

function save_page() {
    $postID = (int)$_POST['postID'];
    $content = $_POST['content'];

    $my_post = array(
        'ID'           => $postID,
        'post_content' => $content
    );

    wp_update_post( $my_post );

    exit;
}

Edit:

In the Backend, on normal refresh:

    global $post;
    echo "<pre>";
    print_r($post->post_content);
    echo "</pre>";

prints a different result content than shown in the editor.

Related posts

2 comments

  1. You can stop post revisions using define('WP_POST_REVISIONS', false); in wp-config.php or try below if it works.

    function save_page() {
        exit( wp_update_post( array(
            'ID'           => absint( esc_attr( $_POST['postID'] ) ),
            'post_type'    =>'your post type'
            'post_content' => esc_attr( $_POST['content'] )
        ) ) );
    }
    
  2. Problem was Textarea autocompleting in Firefox (this problem was only present in FF, as I noticed)

    For now, I have added autocomplete="off" to the textarea created in class-wp-editor.php

    Adding it via JS didn’t work (most likely because FF placed the text into the textarea before my JS was fired)

    EDIT:

    Within WP 3.9 an option will be added to set autocomplete="off" for the editor instance:
    https://core.trac.wordpress.org/ticket/27251

Comments are closed.