First install the ACF plugin and create the appropriate fields. Then insert the code below to functions.php.
/* ==========================================================================
DCODEHUB: NATIVE SEO META BOXES (No Yoast Required)
========================================================================== */
// 1. Create the input boxes in the WordPress Editor
add_action('add_meta_boxes', 'dcodehub_add_seo_meta_box');
function dcodehub_add_seo_meta_box() {
$screens = ['post', 'page']; // Shows up on pages and blog posts
foreach ($screens as $screen) {
add_meta_box('dcodehub_seo_box', 'DCodeHub SEO Settings', 'dcodehub_seo_box_html', $screen, 'normal', 'high');
}
}
// 2. The HTML for the input boxes
function dcodehub_seo_box_html($post) {
$title = get_post_meta($post->ID, '_dcodehub_seo_title', true);
$desc = get_post_meta($post->ID, '_dcodehub_seo_description', true);
wp_nonce_field('dcodehub_seo_save', 'dcodehub_seo_nonce');
?>
`
add_filter('pre_get_document_title', 'dcodehub_output_seo_title', 999);
function dcodehub_output_seo_title($title) {
if (is_singular()) {
$custom_title = get_post_meta(get_queried_object_id(), '_dcodehub_seo_title', true);
if (!empty($custom_title)) return $custom_title;
}
return $title;
}
add_action('wp_head', 'dcodehub_output_seo_description', 1);
function dcodehub_output_seo_description() {
if (is_singular()) {
$custom_desc = get_post_meta(get_queried_object_id(), '_dcodehub_seo_description', true);
if (!empty($custom_desc)) {
echo '' . "\n";
}
}
}