For a plugin I'm working, I've added a button that needs to be either disabled or show a warning if the current editing post is dirty, that is, if anything has been modified and not saved.

Looking at what's in /wp-includes/js/autosave.js, I've found that:

window.onbeforeunload = function(){
    var editor = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, compareString;

    if ( editor && ! editor.isHidden() ) {
        if ( editor.isDirty() )
            return autosaveL10n.saveAlert;
    } else {
        if ( fullscreen && fullscreen.settings.visible ) {
            compareString = wp.autosave.getCompareString({
                post_title: $('#wp-fullscreen-title').val() || '',
                content: $('#wp_mce_fullscreen').val() || '',
                excerpt: $('#excerpt').val() || ''
        } else {
            compareString = wp.autosave.getCompareString();

        if ( compareString != autosaveLast )
            return autosaveL10n.saveAlert;

(look at the declaration of 'editor' in second line of that code)

I've tried to blindly copy that line and put it in my javascript that handle the 'click' event of this button, but the 'editor' variable is set to null, because, I suppose, 'tinymce' is not instantiated yet.

The way I'm trying to implement this it's pretty similar to that:

jQuery(document).ready(function ($) {

function set_element_as_source_open() {
    var editor = typeof(tinymce) !== 'undefined' ? tinymce.activeEditor : false;

    if (editor && !editor.isHidden()) {
        if (editor.isDirty()) {
            alert('Please save this post before proceeding.');
    } else {
        // code here ....

Now, I've the feeling I'm taking the wrong approach, but looking on the web I didn't manage to find any solution for that.

Actually I also think that by just checking if tinymce is dirty is not enough: what if I change other post elements, such as the title, taxonomies, custom fields, etc?

  • One major thing to note here is that this function is checking the current values against the last content that was autosaved. If you don't have anything that's saved in the variable autosaveLast you're not going to have a good time. – MikeNGarrett Jan 24 '14 at 4:08
  • When do you execute your function? I called it on the jQuery ready event and it seems to do work fine. Have you tried binding it to the click event of your button? – Jan Beck Jan 24 '14 at 5:06
  • @JanBeck I've added the implementation code in my question. Ad you can see I do the same. I've also added 'autosave' as a dependency when enqueuing the script, and the script gets enqueued, therefore the script 'autosave' is loaded before mine. – Andrea Sciamanna Jan 24 '14 at 8:16
  • @MikeNGarrett could you please details your comment? I'm not sure I'm understanding it. – Andrea Sciamanna Jan 24 '14 at 8:19
  • if ( compareString != autosaveLast ) is checking your current content vs the last autosave content. Even if autosave is a dependency that doesn't mean it's actually saving anything... unless I'm not understanding the scenario. – MikeNGarrett Jan 24 '14 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.