SEO PHP Intermediate

Add Open Graph Tags Without a Plugin

Last updated: May 6, 2026

When a page on your site gets shared on Facebook, LinkedIn, Slack, or any other platform that reads Open Graph metadata, the platform uses those tags to determine the title, description, and image shown in the preview. Without Open Graph tags, platforms fall back to guessing, often pulling the wrong image or showing a generic description. This snippet adds the essential Open Graph tags to every singular post and page on your WordPress site, no plugin required.

The Code

Add this to your functions.php or a site-specific plugin. It hooks into wp_head with a priority of 5 to ensure it runs before most other head injections.

How It Works

The snippet only runs on singular pages, posts, pages, and custom post types, by checking is_singular() at the top and returning early for archives, homepages, and other non-singular templates.

For the title, it uses get_the_title() which returns the post title as stored in the database. For the description, it checks whether the post has a manual excerpt set first. If it does, that excerpt is used, this lets you write a custom social description per post just by filling in the excerpt field. If no excerpt is set, it falls back to trimming the first 30 words of the post content.

The image is pulled using get_the_post_thumbnail_url() at the large size. If no featured image is set, the image tag is simply omitted rather than outputting an empty value, which could cause platform errors.

All values are escaped appropriately: esc_attr() for content attributes and esc_url() for URLs. This keeps the output safe and standards-compliant.

When to Use This

This snippet works well for sites that don’t use a full SEO plugin, or that use a lightweight option like SlimSEO that may not include Open Graph support. It gives you full control over the output without the overhead of a large dependency.

It’s also a good starting point if you want to build a custom Open Graph implementation, for example, adding og:image:width and og:image:height, or outputting Twitter Card tags alongside the OG tags.

Things to Keep in Mind

If you already have an SEO plugin active that outputs Open Graph tags, do not add this snippet, you’ll end up with duplicate meta tags, which can confuse social platforms. Check your page source for existing og: properties before implementing.

Facebook and other platforms cache Open Graph data aggressively. After adding this snippet, use the Facebook Sharing Debugger tool to force a cache refresh on any URLs you want to test. The preview won’t update automatically on posts that have already been shared.

functions.php
add_action( 'wp_head', function() {
    if ( ! is_singular() ) return;

    global $post;
    setup_postdata( $post );

    $title       = get_the_title( $post->ID );
    $description = has_excerpt( $post->ID )
        ? get_the_excerpt( $post->ID )
        : wp_trim_words( get_the_content( null, false, $post ), 30, '...' );
    $url         = get_permalink( $post->ID );
    $image       = get_the_post_thumbnail_url( $post->ID, 'large' );
    $site_name   = get_bloginfo( 'name' );

    echo '<meta property="og:title" content="' . esc_attr( $title ) . '">\n';
    echo '<meta property="og:description" content="' . esc_attr( $description ) . '">\n';
    echo '<meta property="og:url" content="' . esc_url( $url ) . '">\n';
    echo '<meta property="og:type" content="article">\n';
    echo '<meta property="og:site_name" content="' . esc_attr( $site_name ) . '">\n';
    if ( $image ) {
        echo '<meta property="og:image" content="' . esc_url( $image ) . '">\n';
    }
}, 5 );

Built by Nahnu Plugins

Need something more powerful than a snippet?

Our commercial plugins go further, built for serious WordPress sites with full support, updates, and documentation included.

Browse All Plugins →

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only