Advertisement
0utsidethebox

Adding Open Graph to Wordpress

Apr 21st, 2016
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. /* Open graph
  2. https://www.elegantthemes.com/blog/tips-tricks/how-to-add-open-graph-tags-to-wordpress
  3. http://stackoverflow.com/a/14359678
  4. http://wordpress.stackexchange.com/a/141136
  5. http://telegram.me/webpagebot
  6. */
  7.  
  8. function t_opengraph() {
  9.     global $post;
  10.     $post_thumbnail_id = get_post_thumbnail_id($post->ID);
  11.     $post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
  12.      if(is_single()) {
  13.         if($excerpt = $post->post_excerpt) {
  14.             $excerpt = strip_tags($post->post_excerpt);
  15.             $excerpt = str_replace("", "'", $excerpt);
  16.         } else {
  17.             $excerpt = get_bloginfo('description');
  18.         }
  19.         ?>
  20.  
  21.     <meta property="og:title" content="<?php echo the_title(); ?>"/>
  22.     <meta property="og:description" content="<?php echo $excerpt; ?>"/>
  23.     <meta property="og:image" content="<?php echo $post_thumbnail_url; ?>"/>
  24.  
  25. <?php
  26.     } else {
  27.         return;
  28.     }
  29. }
  30. add_action('wp_head', 't_opengraph', 5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement