Skip to content

Instantly share code, notes, and snippets.

@VadimSVB
Created March 21, 2019 13:01
Show Gist options
  • Save VadimSVB/d8b0f1cab9edf7c04c683b59f0e316f0 to your computer and use it in GitHub Desktop.
Save VadimSVB/d8b0f1cab9edf7c04c683b59f0e316f0 to your computer and use it in GitHub Desktop.

Revisions

  1. Basilis Kanonidis created this gist Apr 14, 2016.
    159 changes: 159 additions & 0 deletions schema.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,159 @@
    /**
    * Schema.org addtions for better SEO
    * @param string Type of the element
    * @return string HTML Attribute
    */

    function get_schema_markup($type, $echo = false) {

    if (empty($type)) return false;

    $attributes = '';
    $attr = array();

    switch ($type) {
    case 'body':
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/WebPage';
    break;

    case 'header':
    $attr['role'] = 'banner';
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/WPHeader';
    break;

    case 'nav':
    $attr['role'] = 'navigation';
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/SiteNavigationElement';
    break;

    case 'title':
    $attr['itemprop'] = 'headline';
    break;

    case 'sidebar':
    $attr['role'] = 'complementary';
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/WPSideBar';
    break;

    case 'footer':
    $attr['role'] = 'contentinfo';
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/WPFooter';
    break;

    case 'main':
    $attr['role'] = 'main';
    $attr['itemprop'] = 'mainContentOfPage';
    if (is_search()) {
    $attr['itemtype'] = 'https://schema.org/SearchResultsPage';
    }

    break;

    case 'author':
    $attr['itemprop'] = 'author';
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/Person';
    break;

    case 'person':
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/Person';
    break;

    case 'comment':
    $attr['itemprop'] = 'comment';
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/UserComments';
    break;

    case 'comment_author':
    $attr['itemprop'] = 'creator';
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/Person';
    break;

    case 'comment_author_link':
    $attr['itemprop'] = 'creator';
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/Person';
    $attr['rel'] = 'external nofollow';
    break;

    case 'comment_time':
    $attr['itemprop'] = 'commentTime';
    $attr['itemscope'] = 'itemscope';
    $attr['datetime'] = get_the_time('c');
    break;

    case 'comment_text':
    $attr['itemprop'] = 'commentText';
    break;

    case 'author_box':
    $attr['itemprop'] = 'author';
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/Person';
    break;

    case 'video':
    $attr['itemprop'] = 'video';
    $attr['itemtype'] = 'https://schema.org/VideoObject';
    break;

    case 'audio':
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/AudioObject';
    break;

    case 'blog':
    $attr['itemscope'] = 'itemscope';
    $attr['itemtype'] = 'https://schema.org/Blog';
    break;

    case 'name':
    $attr['itemprop'] = 'name';
    break;

    case 'url':
    $attr['itemprop'] = 'url';
    break;

    case 'email':
    $attr['itemprop'] = 'email';
    break;

    case 'job':
    $attr['itemprop'] = 'jobTitle';
    break;

    case 'post_time':

    $attr['itemprop'] = 'datePublished';
    $attr['datetime'] = get_the_time('c', $args['id']);
    break;

    case 'post_title':
    $attr['itemprop'] = 'headline';
    break;

    case 'post_content':
    $attr['itemprop'] = 'text';
    break;
    }

    foreach ($attr as $key => $value) {
    $attributes.= $key . '="' . $value . '" ';
    }

    if ($echo) {
    echo $attributes;
    }
    else {
    return $attributes;
    }
    }