Yair Shachar
Yair Shachar

Reputation: 55

How to insert <meta> tag in the <head> area of a specific page

I want to add a meta tag for my site description inside the head area of a specific page, before the body tag opens. The site is built with WordPress & Elementor.

I don't want to add it globally like in the theme>>header option here: https://wordpress.org/support/topic/how-to-inset-code-the-closing-tag/

I'm looking for another solution besides editing the header.php file in a way that applies for all the theme pages. If I can do so in this file, and control which pages/posts the added meta tag will apply to, so that will be good.

What is my solution?

This can be the relevant code at functions.php

function envo_shop_setup() {

// Theme lang.
load_theme_textdomain('envo-shop', get_template_directory() . '/languages');

// Add Title Tag Support.
add_theme_support('title-tag');

// Register Menus.
register_nav_menus(
    array(
        'main_menu' => esc_html__('Main Menu', 'envo-shop'),
        'main_menu_right' => esc_html__('Main Menu Right', 'envo-shop'),
        'main_menu_cats' => esc_html__('Categories Menu', 'envo-shop'),
    )
);

// some templete conditions...

endif;

function envo_shop_pingback_header() {
    if (is_singular() && pings_open()) {
        printf('<link rel="pingback" href="%s">' . "\n", esc_url(get_bloginfo('pingback_url')));
    }
}

add_action('wp_head', 'envo_shop_pingback_header');

Upvotes: 2

Views: 2218

Answers (1)

Diego
Diego

Reputation: 1716

If the theme you are using is nothing really weird and special you should have a call like this:

<?php wp_head(); ?>

before the tag. If that's the case you can use:

add_action( 'wp_head',function (){
    // you can pass Page ID, title, slug, or array of such to check against. Default empty.
    if(is_page("mypage")){
        
    }
} );

This would put what you need only where you want them to appear using the conditionals tags (see https://codex.wordpress.org/Conditional_Tags)

Upvotes: 2

Related Questions