sinanisler logo

WordPress Built in SEO Keyword, Meta Desc Fields, No Plugin SEO

add this to your functions.php and you will have keywords and meta description fields in your posts and pages and post types.

this fields will render in <head> if they filled.

If you have very minimalistic website and don’t want SEO plugin here is your code have fun with it.

 

 





/**
 * Add SEO meta fields to posts, pages, and custom post types
 */
function custom_add_seo_meta_fields() {
    // Add meta fields for posts
    add_meta_box('custom_seo_fields', 'SEO Meta Fields', 'custom_render_seo_meta_fields', 'post', 'normal', 'high');
    
    // Add meta fields for pages
    add_meta_box('custom_seo_fields', 'SEO Meta Fields', 'custom_render_seo_meta_fields', 'page', 'normal', 'high');
    
    // Add meta fields for custom post types
    $custom_post_types = get_post_types(array('_builtin' => false));
    foreach ($custom_post_types as $post_type) {
        add_meta_box('custom_seo_fields', 'SEO Meta Fields', 'custom_render_seo_meta_fields', $post_type, 'normal', 'high');
    }
}
add_action('add_meta_boxes', 'custom_add_seo_meta_fields');

/**
 * Render SEO meta fields
 */
function custom_render_seo_meta_fields($post) {
    // Retrieve current values
    $keywords = get_post_meta($post->ID, 'seo_keywords', true);
    $description = get_post_meta($post->ID, 'seo_description', true);
    
    // Output HTML for meta fields
    wp_nonce_field('custom_save_seo_meta_fields', 'custom_seo_meta_nonce');
    ?>
    <p>
        <label for="seo_keywords">Keywords:</label><br>
        <textarea name="seo_keywords" id="seo_keywords" rows="3"><?php echo esc_textarea($keywords); ?></textarea>
    </p>
    <p>
        <label for="seo_description">Meta Description:</label><br>
        <textarea name="seo_description" id="seo_description" rows="5" maxlength="155"><?php echo esc_textarea($description); ?></textarea>
    </p>
    <?php
}

/**
 * Save SEO meta fields
 */
function custom_save_seo_meta_fields($post_id) {
    // Verify nonce
    if (!isset($_POST['custom_seo_meta_nonce']) || !wp_verify_nonce($_POST['custom_seo_meta_nonce'], 'custom_save_seo_meta_fields')) {
        return;
    }
    
    // Save meta fields
    if (isset($_POST['seo_keywords'])) {
        update_post_meta($post_id, 'seo_keywords', sanitize_textarea_field($_POST['seo_keywords']));
    }
    
    if (isset($_POST['seo_description'])) {
        update_post_meta($post_id, 'seo_description', sanitize_textarea_field($_POST['seo_description']));
    }
}
add_action('save_post', 'custom_save_seo_meta_fields');

/**
 * Enqueue styles and scripts for admin
 */
function custom_enqueue_admin_scripts() {
    wp_enqueue_style('custom-admin-styles', get_template_directory_uri() . '/admin.css');
}
add_action('admin_enqueue_scripts', 'custom_enqueue_admin_scripts');









// Render custom meta keywords and description in the <head>
function custom_render_meta_tags() {
     // Get the current post/page ID
     $current_id = get_queried_object_id();

     // Retrieve the custom meta keywords and description
     $keywords = get_post_meta($current_id, 'seo_keywords', true);
     $description = get_post_meta($current_id, 'seo_description', true);

     if ($keywords) {
          echo '
<!-- SEO FIELDS --!>

<meta name="keywords" content="' . esc_attr($keywords) . '">';
     }

     if ($description) {
          echo '

<meta name="description" content="' . esc_attr($description) . '">

<!-- SEO FIELDS --!>
          ';
     }
}
add_action('wp_head', 'custom_render_meta_tags');


 

Leave the first comment