Skip to content

Instantly share code, notes, and snippets.

@bueltge
Created August 1, 2012 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bueltge/3226847 to your computer and use it in GitHub Desktop.
Save bueltge/3226847 to your computer and use it in GitHub Desktop.
Check for right WordPress page template on save_post hook
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment