I swear to god…..!!! Every time I ask a question here I seem to find the answer….
I missed the verification:
echo '<input type="hidden" name="meta_box_template_picker_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
Below is the complete code.
If you think you can use this please do.
If you wish to use it but don’t know how:
1. Create a seperate .php
file and name it e.g. custom_template_picker.php
2. Add the complete code below and save this in the theme folder.
3. Open functions.php
and add this anywhere before ?>
=> include 'custom_template_picker.php';
4. In functions.php
also add the following to diable the current attributes metabox:
function my_remove_meta_boxes() {
remove_meta_box( 'pageparentdiv' , 'page' , 'normal' );
}
add_action( 'admin_menu', 'my_remove_meta_boxes' );
- In the code on line 19,20 and 21 are template filenames that you wish to see. Change these if you wish to only see some templates.
- Right now the output of the templates you get to see are
radio
buttons. You can change theecho
on line 22 to whatever you wish the output should be. -
Upload these files to the server and you should be set.
<?php // Add the Meta Box function add_meta_box_template_picker() { add_meta_box( 'meta_box_template_picker', // $id 'Template Picker', // $title 'show_meta_box_template_picker', // $callback 'page', // $page 'normal', // $context 'high'); // $priority } add_action('add_meta_boxes', 'add_meta_box_template_picker'); // The Callback function show_meta_box_template_picker() { echo '<input type="hidden" name="meta_box_template_picker_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />'; $templates = get_page_templates(); foreach ( $templates as $template_name => $template_filename ) { if ($template_filename == 'template1.php' || $template_filename == 'template2.php' || $template_filename == 'template3.php' ){ echo "<input type="radio" name="_wp_page_template" value="$template_filename" />$template_name<br />"; }else{ echo ''; } } echo get_post_meta(249, '_wp_page_template', true); } // Save the Data function save_custom_meta($post_id) { global $templates, $post; // post of template $post_id = $post->ID; // verify nonce if (!wp_verify_nonce($_POST['meta_box_template_picker_nonce'], basename(__FILE__))) return $post_id; // check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; // check permissions if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) return $post_id; } elseif (!current_user_can('edit_post', $post_id)) { return $post_id; } // loop through fields and save the data $old = get_post_meta($post_id, '_wp_page_template', true); $new = $_POST['_wp_page_template']; if ($new && $new != $old) { update_post_meta($post_id, '_wp_page_template', $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, '_wp_page_template', $old); } } // end foreach add_action('save_post', 'save_custom_meta'); ?>
************Any suggestions/improvements are welcome***************
I hope I can help someone with this