Opened 11 years ago
Last modified 6 years ago
#24865 new enhancement
Allow plugins to hook into autosave and include field data from the autosave with the package.
Reported by: | nerrad | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Autosave | Keywords: | needs-refresh |
Focuses: | Cc: |
Description
Why?
Autosaves currently only save "blessed" data (post_title and post_description?). Plugin authors may want to add their own data in the autosave package so if they hook into save_post
they can handle/save that data as needed.
How this works
Metaboxes are generated with an id already on the page. So what we do is add an extra parameter to the add_meta_box()
function (do_autosave
bool) so that when a plugin uses add_meta_box() they can set that flag.
We set a javascript object that contains the ids of the metaboxes that have had that flag set and then wp.autosave.getPostData
in autosave.js
has been modified to loop through the metaboxes matching those ids and return the fields in those metaboxes as an object attached to the data param.
Added a new jquery plugin (serializefullarray) as it will handle nested pseudo arrays set as the "name" values in inputs (i.e. somedata[one][two]) and create proper objects from them.
Attachments (3)
Change History (17)
#3
@
11 years ago
Looks pretty clean except for the console.log(), would be nice to get this included!
#4
@
11 years ago
dang of course I missed that console.log I noticed something in one of the comment docs too. Gonna put up a fix for both in a couple minutes
#6
@
11 years ago
didn't realize the patch didn't include the jquery serializefullarray library. As far as I can tell there are no restricting licenses on that library. It's very handy for parsing form data that is referenced via nested associated array into js objects.
#8
@
11 years ago
Good idea. Before that autosave would need a more general "overhaul". It should use heartbeat for transport, parts of it haven't been used in ages, other parts need updating. For example: saving tags, may need to JSON encode the form data, differences when autosaving a draft and a published post, etc.
#9
@
11 years ago
Would an overhaul be needed before this gets added though? Imo this could be added and plugins can interact now. The impact of future changes would be minimal because the object containing the metabox ids could just be carried forward...
#11
@
10 years ago
It appears this is still not incorporated in WP 3.9.1.
If the above solution is not going to make it into WP, at least refactor autosave.js
to use 'this.getPostData' rather than 'getPostData' etc. What's the point of exporting
it in the window.wp.autosave structure if changing it has no effect?
What I want to do a plugin script is something like:
var as_getPostData = window.wp.autosave.getPostData; window.wp.autosave.getPostData = function() { var ret = as_getPostData.apply( this/*?*/, arguments ); // append my metabox input fields return ret; };
As an alternative, this works fine:
$(document).on('before-autosave', function(event,data) { var x = {}; $(".postbox input, .postbox select").each( function(i,el) { el = $(el); x[ el.attr('name') ] = el.val(); }); $.extend( data, x ); } );
..except that 'getCompareString' cannot be overriden to include changes in meta values.
#12
@
10 years ago
- Keywords needs-refresh added; has-patch removed
- Milestone changed from Awaiting Review to Future Release
Related: #22601.
at #wcsf contributor day south-side 4th table.