save_post action not firing due to old page_template meta data
-
I have some functionality hooked to the save_post action that wasn’t working for a user on a particular WordPress Page. After some digging I found that wp_insert_post() was returning before getting to do_action( ‘save_post’…. because of the following logic:
(wp-includes/post.php line 3187…)
if ( !empty($page_template) && 'page' == $data['post_type'] ) { $post->page_template = $page_template; $page_templates = wp_get_theme()->get_page_templates( $post ); if ( 'default' != $page_template && ! isset( $page_templates[ $page_template ] ) ) { if ( $wp_error ) return new WP_Error('invalid_page_template', __('The page template is invalid.')); else return 0; } update_post_meta($post_ID, '_wp_page_template', $page_template); }
Turns out the user had assigned a page template to the page when using a previous theme, but my theme doesn’t support page templates. In this case, the $page_template variable above is set to ‘template-grid-gallery.php’, which is not a template file in the current theme. Consequently, the function returns zero and never reaches the save_post action (several lines below).
Is there some way to explicitly turn off page template support in a theme so that WordPress doesn’t ever check for a page template. Otherwise it seems like the save_post action would never fire if the page has a page template assigned, but is using a theme without page template support. Obviously I could just delete the _wp_page_template meta data for that page, but I’d like to protect against this kind of problem for other users in the future.
- The topic ‘save_post action not firing due to old page_template meta data’ is closed to new replies.