WordPress Tutorials 1 min read

How to Add Article Schema without a Plugin

Published by Manuel Campos on December 29, 2022 • Updated on November 13, 2024

When it comes to schema markup, the most complicated one is the one that you have to add to all your posts and pages.

Adding schema markup to all posts and past is something that nobody is willing to do manually, especially when you have hundreds and hundreds of posts.

If you add this to your functions.php file, it will add the basic article schema to all your posts.

function add_article_schema_to_posts() {
  if ( is_single() && 'post' == get_post_type() ) {
    $schema = '{
      "@context": "https://schema.org",
      "@type": "Article",
      "mainEntityOfPage": {
        "@type": "WebPage",
        "@id": "' . get_permalink() . '"
      },
      "headline": "' . get_the_title() . '",
      "image": {
        "@type": "ImageObject",
        "url": "' . get_the_post_thumbnail_url() . '"
      },
      "datePublished": "' . get_the_date( 'c' ) . '",
      "dateModified": "' . get_the_modified_date( 'c' ) . '",
      "author": {
        "@type": "Person",
        "name": "' . get_the_author() . '"
      },
      "publisher": {
        "@type": "Organization",
        "name": "' . get_bloginfo( 'name' ) . '",
        "logo": {
          "@type": "ImageObject",
          "url": "' . get_site_icon_url() . '"
        }
      },
      "description": "' . wp_strip_all_tags( get_the_excerpt() ) . '"
    }';
    echo '<script type="application/ld+json">' . $schema . '</script>';
  }
}
add_action( 'wp_head', 'add_article_schema_to_posts' );

Manuel Campos

Manuel Campos

I'm a WordPress enthusiast. I document my journey and provide actionable insights to help you navigate the ever-evolving world of WordPress."

Read Next

Support Honest Reviews

Help keep the reviews coming by using my recommended links.

May earn commission • No extra cost to you