questions

Add meta tag on header if url is equal to something in functions.php

Rate this post

I need to add a meta tag on the header if the url is equal to “https://example.com/listing/” but I need to do it in functions.php

Here’s the code:

add_action( 'wp_head', 'wpse_71899_archive_filter', 5);
global $wp;
$current_url = home_url(add_query_arg(array(), $wp->request));

function wpse_71899_archive_filter()

if ($current_url == 'https://example.com/listing/') 
{
    echo '<meta name="robots" content="noindex">';
}

But WP returns an error as I save the functions.php

“syntax error, unexpected ‘if’ (T_IF), expecting ‘{‘”

What am I doing wrong here? Or is it all wrong?

 

✔️Solution:

 function addMeta(){
   $current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 
   "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

   if($current_url == 'https://example.com/listing/') {
     echo '<meta name="robots" content="noindex">';
   }

 }
 add_action('wp_head','addMeta');

it should work.

Leave a Reply

Your email address will not be published.

Back to top button