Times | Meta | SEO
class SEO_ {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'SEO',
__( 'SEO items', 'text_domain' ),
array( $this, 'render_metabox' ),
array( 'attraction', ' bar', ' hotel', ' article', 'restaurant ' ),
'advanced',
'low'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$seo_seo_meta_description = get_post_meta( $post->ID, 'seo_seo_meta_description', true );
// Set default values.
if( empty( $seo_seo_meta_description ) ) $seo_seo_meta_description = 'default';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="SEO_seo_meta_description" class="SEO_seo_meta_description_label">' . __( 'Meta Description', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="SEO_seo_meta_description" name="SEO_seo_meta_description" class="SEO_seo_meta_description_field" placeholder="' . esc_attr__( 'add description', 'text_domain' ) . '">' . $SEO_seo_meta_description . '</textarea>';
echo ' <p class="description">' . __( 'This is the seo friendly description', 'text_domain' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$seo_new_seo_meta_description = isset( $_POST[ 'seo_seo_meta_description' ] ) ? sanitize_text_field( $_POST[ 'seo_seo_meta_description' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'seo_seo_meta_description', $seo_new_seo_meta_description );
}
}
new SEO_;