WordPress.org

Ready to get started?Download WordPress

Forums

WP Open Graph Meta
How to change og meta for homepage? (2 posts)

  1. hiboy
    Member
    Posted 1 year ago #

    Hi,

    I just started using your plugin along with all in one seo, i realise all my post and pages, og meta tag was inserted. However ,the home page/front page ,which i set display recent 10 post, there are no og meta tag, is it possible if i insert my own og meta tag for the home page?How do i achieve it?
    If i insert the following in my header.php, it will appear in all pages.
    <meta property="og:title" content="" />
    <meta property="og:url" content="" />
    <meta property="og:description" content="" />
    <meta property="og:site_name" content="" />
    <meta property="og:image" content="" />

    http://wordpress.org/extend/plugins/wp-open-graph-meta/

  2. tommygeorge
    Member
    Posted 1 year ago #

    I know you posted this four months ago, but just in case you're still wondering (or someone else stumbled across this site) here's how I solved the issue for one site:

    I altered the plugin code in wp-content/plugins/wp-open-graph-meta/wp-open-graph-meta.php. I changed the function add_elements() from this:

    public function add_elements()
        {
            if (is_singular()) {
                the_post();
    
                $this->_metas['og:title'] = $this->_get_title();
                $this->_metas['og:type'] = is_single() ? 'article' : 'website';
                $this->_metas['og:url'] = get_permalink();
    
                $this->_metas['og:description'] = $this->_get_description();
                $this->_metas['og:site_name'] = strip_tags(get_bloginfo('name'));
                $this->_metas['og:locale'] = strtolower(str_replace('-', '_', get_bloginfo('language')));
    
                $this->_add_image();
                $this->_add_post_tags();
    
                $this->_output();
    
                rewind_posts();
            }
        }

    To be this:

    public function add_elements()
        {
            if (is_singular()) {
                the_post();
    
                $this->_metas['og:title'] = $this->_get_title();
                $this->_metas['og:type'] = is_single() ? 'article' : 'website';
                $this->_metas['og:url'] = get_permalink();
    
                $this->_metas['og:description'] = $this->_get_description();
                $this->_metas['og:site_name'] = strip_tags(get_bloginfo('name'));
                $this->_metas['og:locale'] = strtolower(str_replace('-', '_', get_bloginfo('language')));
    
                $this->_add_image();
                $this->_add_post_tags();
    
                $this->_output();
    
                rewind_posts();
            }
            elseif ( is_home() )  // Added by @tommygeorge:
            {
                $this->_metas['og:title'] = strip_tags(get_bloginfo('name'));
                $this->_metas['og:type'] = 'website';
                $this->_metas['og:url'] = home_url();
    
                $this->_metas['og:description'] = strip_tags(get_bloginfo('description'));
                $this->_metas['og:site_name'] = strip_tags(get_bloginfo('name'));
                $this->_metas['og:locale'] = strtolower(str_replace('-', '_', get_bloginfo('language')));
    
                $this->_output();
            }
        }

    As you should be able to see, I simply added an elseif condition, and altered the tags I wanted to show up on the front page. It may need further modification to be more friendly with some SEO plugins, or to work properly with a static-page front page. This currently works with a front page that shows posts.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.