sinanisler logo

Basic SEO Setting for Homepage

This snippet will add seo setting for homepage in Customizer.


//
// 		Basic SEO Setting for Homepage
// 

function sinn_customize_register( $wp_customize ) {
    // Add a section for the SEO settings
    $wp_customize->add_section('seo_settings', array(
        'title'    => __('SEO Settings', 'mytheme'),
        'priority' => 120,
    ));

    // Add setting for SEO title
    $wp_customize->add_setting('seo_home_title', array(
        'default'        => '',
        'sanitize_callback' => 'sanitize_text_field', // Sanitize the input
    ));

    // Add control for SEO title
    $wp_customize->add_control('seo_home_title', array(
        'label'      => __('Home SEO Title', 'mytheme'),
        'section'    => 'seo_settings',
        'type'       => 'text',
    ));

    // Add setting for SEO description
    $wp_customize->add_setting('seo_home_description', array(
        'default'        => '',
        'sanitize_callback' => 'sanitize_textarea_field', // Sanitize the input
    ));

    // Add control for SEO description
    $wp_customize->add_control('seo_home_description', array(
        'label'      => __('Home SEO Description', 'mytheme'),
        'section'    => 'seo_settings',
        'type'       => 'textarea',
    ));
}
add_action('customize_register', 'sinn_customize_register');

function sinn_seo_title( $title ) {
    if (is_front_page()) {
        $seo_title = get_theme_mod('seo_home_title', '');
        if (!empty($seo_title)) {
            return $seo_title;
        }
    }
    return $title;
}
add_filter('pre_get_document_title', 'sinn_seo_title');

function sinn_add_seo_description() {
    if (is_front_page()) {
        $seo_description = get_theme_mod('seo_home_description', '');
        if (!empty($seo_description)) {
            echo '<meta name="description" content="' . esc_attr($seo_description) . '">' . PHP_EOL;
        }
    }
}
add_action('wp_head', 'sinn_add_seo_description');



Leave the first comment