true ), 'names' );
foreach ( $post_types as $post_type ) {
add_meta_box(
's1-seo-meta-box',
__( 'S1 SEO: Title & Description', 's1-seo-meta' ),
array( $this, 'render_meta_box' ),
$post_type,
'normal',
'high'
);
}
}
/**
* Render the meta box HTML.
*
* @param WP_Post $post Current post object.
*/
public function render_meta_box( $post ) {
wp_nonce_field( 's1_seo_meta_save', 's1_seo_meta_nonce' );
$seo_title = get_post_meta( $post->ID, self::META_TITLE, true );
$seo_desc = get_post_meta( $post->ID, self::META_DESC, true );
?>
post_type );
if ( ! current_user_can( $post_type_obj->cap->edit_post, $post_id ) ) {
return;
}
// Save title.
if ( isset( $_POST['s1_seo_title'] ) ) {
update_post_meta(
$post_id,
self::META_TITLE,
sanitize_text_field( wp_unslash( $_POST['s1_seo_title'] ) )
);
}
// Save description.
if ( isset( $_POST['s1_seo_description'] ) ) {
update_post_meta(
$post_id,
self::META_DESC,
sanitize_textarea_field( wp_unslash( $_POST['s1_seo_description'] ) )
);
}
}
/**
* Static helper: retrieve SEO title for a given post.
*
* @param int $post_id Post ID.
* @return string
*/
public static function get_title( $post_id ) {
return (string) get_post_meta( $post_id, self::META_TITLE, true );
}
/**
* Static helper: retrieve SEO description for a given post.
*
* @param int $post_id Post ID.
* @return string
*/
public static function get_description( $post_id ) {
return (string) get_post_meta( $post_id, self::META_DESC, true );
}
}