[wp-trac] [WordPress Trac] #29269: save_post doesn't fire when unavailable page template saved to page
WordPress Trac
noreply at wordpress.org
Thu Aug 28 10:27:14 UTC 2014
#29269: save_post doesn't fire when unavailable page template saved to page
-------------------------------+-----------------------------
Reporter: matthewdietsche | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Future Release
Component: Posts, Post Types | Version: 2.6
Severity: major | Resolution:
Keywords: 4.1-early | Focuses:
-------------------------------+-----------------------------
Comment (by alley-cat):
I've just discovered the same problem. I have a theme with only one page
template, therefore the Template UI drop down does not appear, and does
not clear the no longer valid page_template.
My solution was to clear the invalid (i.e. no longer exists) page_template
on the edit_form_top hook. This clears the value before the UI is
rendered, as the invalid page_template is carried to the UI and posted
back to wp_insert_post.
My code below for reference.
{{{
// Check the post data before saving
function ac_validate_post_data_page_template($post_id) {
// Page
if ( get_post_type($post_id) == 'page' ) {
// WordPress will prevent post data saving if a page
template has been selected that does not exist
// This is especially a problem when switching to our
theme, and old page templates are in the post data
// Unset the page template if the page doesn't exist to
allow the post to save
// Get the WP template name
$template_file_name =
get_post_meta($post_id,'_wp_page_template',TRUE);
// Check the template exists
if (! ac_page_template_exists($template_file_name) ) {
// Template doesn't exist so remove the data to
allow WP to save
$post = get_post($post_id);
delete_post_meta($post_id, '_wp_page_template');
}
}
}
// Checks if a page template exists
function ac_page_template_exists($template_file_name) {
// Get the templates
$page_templates = wp_get_theme()->get_page_templates();
// Just return if there is an index for our template
return isset( $page_templates[ $template_file_name ] );
}
// Validate post data when the edit for is displayed
add_action('edit_form_top', 'ac_edit_form_top_hook');
function ac_edit_form_top_hook($post) {
ac_validate_post_data_page_template($post->ID);
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/29269#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list