diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 45f821cf9..0b700b46e 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -755,6 +755,37 @@ function validate_current_theme() { return true; } +function get_page_templates() { + $themes = get_themes(); + $theme = get_current_theme(); + $templates = $themes[$theme]['Template Files']; + $page_templates = array(); + + foreach ($templates as $template) { + $template_data = implode('', file(ABSPATH . $template)); + preg_match("|Template Name:(.*)|i", $template_data, $name); + preg_match("|Description:(.*)|i", $template_data, $description); + + $name = $name[1]; + $description = $description[1]; + + if (! empty($name)) { + $page_templates[trim($name)] = basename($template); + } + } + + return $page_templates; +} + +function page_template_dropdown($default = '') { + $templates = get_page_templates(); + foreach (array_keys($templates) as $template) : + if ($default == $templates[$template]) $selected = " selected='selected'"; + else $selected = ''; + echo "\n\t"; + endforeach; +} + function parent_dropdown($default = 0, $parent = 0, $level = 0) { global $wpdb; $items = $wpdb->get_results("SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = $parent AND post_status = 'static' ORDER BY menu_order"); diff --git a/wp-admin/edit-page-form.php b/wp-admin/edit-page-form.php index a94f8afe8..734d908d5 100644 --- a/wp-admin/edit-page-form.php +++ b/wp-admin/edit-page-form.php @@ -49,9 +49,9 @@ window.onload = focusit;
- +
@@ -79,6 +79,22 @@ edCanvas = document.getElementById('content');

+ +
+ + + + + + +
+
+
+
+ diff --git a/wp-admin/edit-pages.php b/wp-admin/edit-pages.php index 8dc093112..fd5e9cf37 100644 --- a/wp-admin/edit-pages.php +++ b/wp-admin/edit-pages.php @@ -68,6 +68,7 @@ if ($user_level > 0) { $ping_status = get_settings('default_ping_status'); $post_pingback = get_settings('default_pingback_flag'); $post_parent = 0; + $page_template = 'default'; include('edit-page-form.php'); } diff --git a/wp-admin/post.php b/wp-admin/post.php index aa5b217a9..99d80ffcd 100644 --- a/wp-admin/post.php +++ b/wp-admin/post.php @@ -200,6 +200,8 @@ case 'post': if ($post_status = 'static') { generate_page_rewrite_rules(); + + add_post_meta($post_ID, '_wp_page_template', $_POST['page_template'], true); } exit(); @@ -233,6 +235,8 @@ case 'edit': $post_parent = $postdata->post_parent; if ($post_status == 'static') { + $page_template = get_post_meta($post_ID, '_wp_page_template', true); + include('edit-page-form.php'); } else { include('edit-form-advanced.php'); @@ -438,6 +442,10 @@ $now_gmt = current_time('mysql', 1); if ($post_status = 'static') { generate_page_rewrite_rules(); + + if ( ! update_post_meta($post_ID, '_wp_page_template', $_POST['page_template'])) { + add_post_meta($post_ID, '_wp_page_template', $_POST['page_template'], true); + } } do_action('edit_post', $post_ID); diff --git a/wp-blog-header.php b/wp-blog-header.php index 7abdbe958..f6bbe4fd9 100644 --- a/wp-blog-header.php +++ b/wp-blog-header.php @@ -215,10 +215,9 @@ if ($pagenow == 'index.php') { $wp_did_template_redirect = true; include("$wp_template_dir/single.php"); exit; - } else if (is_page() && - file_exists("$wp_template_dir/page.php")) { + } else if (is_page() && file_exists(get_page_template())) { $wp_did_template_redirect = true; - include("$wp_template_dir/page.php"); + include(get_page_template()); exit; } else if (is_category() && file_exists("$wp_template_dir/category.php")) { diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 0cfa04061..c63b68ceb 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -447,11 +447,15 @@ AND meta_key = '$key' AND meta_value = '$value'"); return true; } -function get_post_meta($post_id, $key) { +function get_post_meta($post_id, $key, $single = false) { global $wpdb, $post_meta_cache; if (isset($post_meta_cache[$post_id][$key])) { - return $post_meta_cache[$post_id][$key]; + if ($single) { + return $post_meta_cache[$post_id][$key][0]; + } else { + return $post_meta_cache[$post_id][$key]; + } } $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N); @@ -463,12 +467,25 @@ function get_post_meta($post_id, $key) { } } - return $values; + if ($single) { + if (count($values)) { + return $values[0]; + } else { + return ''; + } + } else { + return $values; + } } function update_post_meta($post_id, $key, $value, $prev_value = '') { global $wpdb, $post_meta_cache; + if(! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key += '$key' AND post_id = '$post_id'") ) { + return false; + } + if (empty($prev_value)) { $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE meta_key = '$key' AND post_id = '$post_id'"); @@ -1820,6 +1837,26 @@ function get_template_directory() { return $template; } +function get_page_template() { + global $wp_query; + + $id = $wp_query->post->ID; + $template_dir = get_template_directory(); + $default = "$template_dir/page.php"; + + $template = get_post_meta($id, '_wp_page_template', true); + + if (empty($template) || ($template == 'default')) { + return $default; + } + + if (file_exists("$template_dir/$template")) { + return "$template_dir/$template"; + } + + return $default; +} + // Borrowed from the PHP Manual user notes. Convert entities, while // preserving already-encoded entities: function htmlentities2($myHTML) {