1

I've created a php script to add keywords to my posts and pages. My question is about the wordpress index page. Is there a way to add the <meta name="keywords"> tag also to the home of my wordpress installation? I didn't set a static page, this because I've created a custom theme for my website.

  • How are you adding meta tags to your posts and pages - writing them in a wp_head hook? Won't the same mechanism work for the front page? Are you having trouble detecting that you're on the front page, or storing / building the list of keywords to write, or something else? – Rup Mar 26 at 13:12
  • I'm using the add_action('wp_head', array($this, 'my_function'), 1); , so basically it's the default wordpress hook to manipulate the head content. I'm not checking if the page is homepage but I'm only using the is_single() and is_page() functions to echo the <meta name="keywords"> tag. The tags are the standard tags that is possible to add within every post or page if the taxonomy is registered. – newbWPdev Mar 26 at 13:21
  • You should be able to use is_front_page() to detect the homepage, though if you're using meta keywords for SEO, I'd suggest reading up on why most people discourage it. There are some use cases where they can be helpful, such as when you have a custom search engine that picks them up. – WebElaine Mar 26 at 13:26
  • I know about, but the problem is that I can't set the keywords for the index page like other pages, this because the index.php file is part of my custom template. – newbWPdev Mar 26 at 13:28
  • "because the index.php file is part of my custom template" - why does that matter? Do you mean you're not calling wp_head in your template? – Rup Mar 26 at 14:18
1

I guess you use metadata to store keywords for other pages. One way if to use some page for the source of homepage keywords.

Other is to add theme option for home page keywords:

https://blog.templatetoaster.com/wordpress-settings-api-creating-theme-options/

Afterwards:

add_action('wp_head', 'my_wp_head'));
function my_wp_head() {
    if ( is_front_page() ) {
        $keywords = get_option('homepage_keywords');
        echo '<meta name="keywords" value="' . esc_attr($keywords) . '">';
    }
}
  • I'm not using metadata, but the default tag that an user can add to every post, I've enabled them also for pages. I will look at the documentation link you've suggested. – newbWPdev Mar 27 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.