When I select a template for a page from the “Templates” selectbox, the selected item gets âvalue=selectedâ only after I hit save draft/publish/update.
<option value='template-gallery.php' selected='selected'>Gallery</option>
How can the option receive the value âselectedâ without the need of save/publish/update hit button? (like on widgets page: they get saved without page refresh)
Thank you!
This would have to be fully coded and tested, but I think the roadmap is:
Deregister the Default Meta Box and Make a Custom One
@MikeSchinkel answer to this question is very detailed and thoughtful: Adding Page Attributes Metabox and Page Templates to the Posts Edit Page?
In that case, it is for duplicating the Pages Template Meta Box into Posts.
This other Q&A ( How To Change Custom Taxonomy To Radio Buttons ) provides an example of replacing the Taxonomies Meta Box.
Add Ajax to the Custom Templates Meta Box
In this answer, there’s an example of custom meta box that runs an Ajax call, that in turn makes a database query to modify an attachment parent.
To do the Page Template live switch, it would be a matter of modifying the table
wp_postmeta
where the current template value is stored.The
meta_key
name is_wp_page_template
and its value the template file name.