-
Notifications
You must be signed in to change notification settings - Fork 143
/
meta-box-post-seo.php
112 lines (88 loc) · 4.13 KB
/
meta-box-post-seo.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Adds the SEO meta box to the post editing screen for public post types. This feature allows the post author
* to set a custom title, description, and keywords for the post, which will be viewed on the singular post page.
* To use this feature, the theme must support the 'hybrid-core-seo' feature. The functions in this file create
* the SEO meta box and save the settings chosen by the user when the post is saved.
*
* @package HybridCore
* @subpackage Admin
*/
/* Add the post SEO meta box on the 'add_meta_boxes' hook. */
add_action( 'add_meta_boxes', 'hybrid_meta_box_post_add_seo' );
/* Save the post SEO meta box data on the 'save_post' hook. */
add_action( 'save_post', 'hybrid_meta_box_post_save_seo', 10, 2 );
/**
* Adds the post SEO meta box for all public post types.
*
* @since 1.2.0
* @return void
*/
function hybrid_meta_box_post_add_seo() {
/* Get all available public post types. */
$post_types = get_post_types( array( 'public' => true ), 'objects' );
/* Loop through each post type, adding the meta box for each type's post editor screen. */
foreach ( $post_types as $type )
add_meta_box( 'hybrid-core-post-seo', sprintf( __( '%s SEO', hybrid_get_textdomain() ), $type->labels->singular_name ), 'hybrid_meta_box_post_display_seo', $type->name, 'normal', 'high' );
}
/**
* Displays the post SEO meta box.
*
* @since 1.2.0
* @return void
*/
function hybrid_meta_box_post_display_seo( $object, $box ) {
$domain = hybrid_get_textdomain(); ?>
<?php wp_nonce_field( basename( __FILE__ ), 'hybrid-core-post-meta-box-seo' ); ?>
<div class="hybrid-post-settings">
<p>
<label for="hybrid-document-title"><?php _e( 'Document Title:', $domain ); ?></label>
<br />
<input type="text" name="hybrid-document-title" id="hybrid-document-title" value="<?php echo esc_attr( get_post_meta( $object->ID, 'Title', true ) ); ?>" size="30" tabindex="30" style="width: 99%;" />
</p>
<p>
<label for="hybrid-meta-description"><?php _e( 'Meta Description:', $domain ); ?></label>
<br />
<textarea name="hybrid-meta-description" id="hybrid-meta-description" cols="60" rows="2" tabindex="30" style="width: 99%;"><?php echo esc_textarea( get_post_meta( $object->ID, 'Description', true ) ); ?></textarea>
</p>
<p>
<label for="hybrid-meta-keywords"><?php _e( 'Meta Keywords:', $domain ); ?></label>
<br />
<input type="text" name="hybrid-meta-keywords" id="hybrid-meta-keywords" value="<?php echo esc_attr( get_post_meta( $object->ID, 'Keywords', true ) ); ?>" size="30" tabindex="30" style="width: 99%;" />
</p>
</div><!-- .form-table --><?php
}
/**
* Saves the post SEO meta box settings as post metadata.
*
* @since 1.2.0
* @param int $post_id The ID of the current post being saved.
* @param int $post The post object currently being saved.
*/
function hybrid_meta_box_post_save_seo( $post_id, $post ) {
$prefix = hybrid_get_prefix();
/* Verify the nonce before proceeding. */
if ( !isset( $_POST['hybrid-core-post-meta-box-seo'] ) || !wp_verify_nonce( $_POST['hybrid-core-post-meta-box-seo'], basename( __FILE__ ) ) )
return $post_id;
/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );
/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
$meta = array(
'Title' => strip_tags( $_POST['hybrid-document-title'] ),
'Description' => strip_tags( $_POST['hybrid-meta-description'] ),
'Keywords' => strip_tags( $_POST['hybrid-meta-keywords'] )
);
foreach ( $meta as $meta_key => $new_meta_value ) {
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );
/* If there is no new meta value but an old value exists, delete it. */
if ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
}
}
?>