forked from ShawnZeng1996/Memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
executable file
·78 lines (77 loc) · 2.81 KB
/
functions.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* ┌─┐┬ ┬┌─┐┬ ┬┌┐┌┌─┐┌─┐┌┐┌┌─┐ ┌─┐┌─┐┌┬┐
* └─┐├─┤├─┤││││││┌─┘├┤ ││││ ┬ │ │ ││││
* └─┘┴ ┴┴ ┴└┴┘┘└┘└─┘└─┘┘└┘└─┘o└─┘└─┘┴ ┴
*
* @package WordPress
* @Theme Memory
*
* @author [email protected]
* @link https://shawnzeng.com
*/
// 任何添加于主题目录functions文件夹内的php文件都被调用到这里
define('functions', TEMPLATEPATH.'/functions');
IncludeAll( functions );
function IncludeAll($dir){
$dir = realpath($dir);
if($dir){
$files = scandir($dir);
sort($files);
foreach($files as $file){
if($file == '.' || $file == '..'){
continue;
}elseif(preg_match('/.php$/i', $file)){
include_once $dir.'/'.$file;
}
}
}
}
// 添加自定义的Description和Keywords字段面板
$new_meta_boxes = array(
"description" => array(
"name" => "_description",
"std" => "",
"title" => "网页描述:"
),
"keywords" => array(
"name" => "_keywords",
"std" => "",
"title" => "关键字:"
)
);
function new_meta_boxes() {
global $post, $new_meta_boxes;
foreach($new_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
// 自定义字段标题
echo'<h3>'.$meta_box['title'].'</h3>';
// 自定义字段输入框
echo '<textarea cols="60" rows="3" style="width:100%" name="'.$meta_box['name'].'_value">'.$meta_box_value.'</textarea><br />';
}
echo '<input type="hidden" name="memory_metaboxes_nonce" id="memory_metaboxes_nonce" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
}
function create_meta_box() {
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', '自定义文章描述和关键词', 'new_meta_boxes', 'post', 'normal', 'high' );
}
add_meta_box( 'new-meta-boxes', '自定义页面描述和关键词', 'new_meta_boxes', 'page', 'normal', 'high' );
}
function save_postdata( $post_id ) {
global $new_meta_boxes;
if ( !wp_verify_nonce( $_POST['memory_metaboxes_nonce'], plugin_basename(__FILE__) ))
return;
if ( !current_user_can( 'edit_posts', $post_id ))
return;
foreach($new_meta_boxes as $meta_box) {
$data = $_POST[$meta_box['name'].'_value'];
if($data == "")
delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
else
update_post_meta($post_id, $meta_box['name'].'_value', $data);
}
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');