public
Created

Check for right WordPress page template on save_post hook

  • Download Gist
wpse46583_save.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<?php
/**
* Plugin Name: Template Check
* Plugin URI: http://wordpress.stackexchange.com/questions/60451/check-for-page-template-on-save-post-hook/
* Description:
* Author: Frank Bültge
* Author URI: http://bueltge.de
* License: GPLv3
*/
 
add_action( 'save_post','wpse46583_save', 10, 2 );
function wpse46583_save( $post_id, $post ) {
// verify this is not an auto save routine.
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
 
// You should check nonces and permissions here
if ( ! current_user_can( 'edit_page', $post_id ) )
return;
// only for debugging, get an list of all templates for pages
var_dump( get_page_templates() );
 
if ( esc_attr( $_REQUEST['page_template'] ) !== 'page-contact.php' ) {
// No page template assigned - do something here.
wp_die( 'wrong template' );
}
return;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.