Skip Navigation

[Resolved] Custom posts do not auto-save

This thread is resolved. Here is a description of the problem and solution.

Problem:
Standard blog posts include periodic auto-saves, but the same doesn't happen with custom posts.

Solution:
There is an option when creating or editing a custom post type to turn on or off post revisions, which are off by default.

You can change them at Toolset > Post Types > your_type > Edit > Sections to display > Revisions

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 8 replies, has 4 voices.

Last updated by Juice Rocket® 6 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#905423

It turns out there is no autosaving enabled. We have lost several hours of work because the custom post types does not have autosaving. Is there a way we can enable this, like WordPress should have as standard? Been trying both Chrome and Firefox, so I guess it`s not a problem with the browser.

#905550

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tom

The autosave settings in WordPress apply to all post types, including custom post types.

You set the options in your wp-config.php file like so:

// Define WP Post Revisions
define('AUTOSAVE_INTERVAL', 30); // seconds between auto-saves
define('WP_POST_REVISIONS', 5); // how many revisions to maintain

I just tested this and it was working as expected.

You can see the draft updates being submitted if you leave the network tab of your browser dev tools open, and can see the post revisions in the wp_posts table.

Note that WordPress does not record copies of custom fields when saving auto-drafts.

#905555

WordPress does not have a "standard" autosave feature, it just creates revisions of Posts and Pages out of the box, something that is not happening for example when you edit Post Types, or Views, or similar.

By default, this is as well turned off when you create single Posts in Post types of Toolset.

But, you can enable revisions.
This is done in Toolset > Post Types > your_type > Edit > Sections to display > Revisions

This should help to create revisions of your writings, which should be what you are used to from native Posts and Pages.

Please let me know if you need more help on this!

#905569

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tom

Sorry, Beda is right, it is off by default when you register a post type, but you can enable it in the settings when editing a post type.

You can modify the number of revisions and interval in wp-config.php as described above.

#905590

Okay, thank you. Revisions has always been enabled, but revisions only comes up when the title has been changed. I guess this is because Editor is not enabled, instead we are using a Editor created from CPT with a custom name. Are there no ways we can enable revisions to affect fields created with Toolset?

#905597

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

It isn't supported by WordPress and hence isn't a feature of Toolset (though I think this is a reasonable feature request and I'm going to propose it).

It is possible to get this to work, but you would be setting up such a system yourself.

There is a good description of what's required here: hidden link

If you know a little PHP it should be possible to implement something like this for your site.

If you don't then you would need to contact a developer to do it for you (we maintain a list of Toolset contractors that you can find in the sidebar, though this is not a Toolset issue as such).

Also, there is a plugin which aims to provide this functionality, though I don't believe it ever got accepted into core: hidden link

I haven't tested it but it might be all you need.

#906017
Skjermbilde 2018-05-30 kl. 08.53.26.png

Thank you Nigel.

I activated the revisions by following the link you gave me and turn the functions into:

function pmr_fields( $fields ) {

	$fields['wpcf-brodtekst'] = 'wpcf-brodtekst';
	return $fields;

}

function pmr_field( $value, $field ) {

	global $revision;
	return get_metadata( 'post', $revision->ID, $field, true );

}

function pmr_restore_revision( $post_id, $revision_id ) {

	$post     = get_post( $post_id );
	$revision = get_post( $revision_id );
	$meta     = get_metadata( 'post', $revision->ID, 'wpcf-brodtekst', true );

	if ( false === $meta )
		delete_post_meta( $post_id, 'wpcf-brodtekst' );
	else
		update_post_meta( $post_id, 'wpcf-brodtekst', $meta );

}

function pmr_save_post( $post_id, $post ) {

	if ( $parent_id = wp_is_post_revision( $post_id ) ) {

		$parent = get_post( $parent_id );
		$meta = get_post_meta( $parent->ID, 'wpcf-brodtekst', true );

		if ( false !== $meta )
			add_metadata( 'post', $post_id, 'wpcf-brodtekst', $meta );

	}

}

add_filter( '_wp_post_revision_field_wpcf-brodtekst', 'pmr_field', 10, 2 );
add_action( 'save_post',                   'pmr_save_post', 10, 2 );
add_action( 'wp_restore_post_revision',    'pmr_restore_revision', 10, 2 );
add_filter( '_wp_post_revision_fields',    'pmr_fields' );

At this point the revisions works, but I am not able to see the revisions in the "preview" page. All I see is the title and not the field. Please see attachment. But when I go back to a version the old version shows up. So it kinda works.

However, I am missing the autosaving feature. Is there no way to active it, since I have activated the revisions now?

#906148

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tom

Auto-saves and revisions work in a similar way in terms of how they are stored in the database and auto-saves should continue to work whether or not post revisions are active, as described here: https://codex.wordpress.org/Revisions

I would double-check in wp_posts whether your auto-saves really are missing or not.

If they are missing, comment out your custom code and see if they return.

Because this is not something Toolset supports there is not much help I can offer in terms of the code you have written I'm afraid.

#1969675

Hi! Was this ever made into a feature? We'd love to be able to have the text auto-save as they type (in case their browser crashes, etc.) Thank you!