-
-
Save APIDevClass/95786e3acfc4fa40a01e03806d43abb5 to your computer and use it in GitHub Desktop.
Add Open graph meta tags and twitter card tags to wordpress theme functions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function fb_opengraph() { | |
| global $post; | |
| if(is_single()) { | |
| if(has_post_thumbnail($post->ID)) { | |
| $img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium'); | |
| } else { | |
| $img_src = get_stylesheet_directory_uri() . '/img/opengraph_image.jpg'; | |
| } | |
| if($excerpt = $post->post_excerpt) { | |
| $excerpt = strip_tags($post->post_excerpt); | |
| $excerpt = str_replace("", "'", $excerpt); | |
| } else { | |
| $excerpt = get_bloginfo('description'); | |
| } | |
| ?> | |
| <meta property="og:title" content="<?php echo the_title(); ?>"/> | |
| <meta property="og:description" content="<?php echo $excerpt; ?>"/> | |
| <meta property="og:type" content="article"/> | |
| <meta property="og:url" content="<?php echo the_permalink(); ?>"/> | |
| <meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/> | |
| <meta property="og:image" content="<?php echo $img_src[0]; ?>"/> | |
| <meta name="twitter:card" content="summary_large_image" /> | |
| <meta name="twitter:url" content="<?php echo the_permalink(); ?>" /> | |
| <meta name="twitter:title" content="<?php echo the_title(); ?>" /> | |
| <meta name="twitter:description" content="<?php echo $excerpt; ?>" /> | |
| <meta name="twitter:image:src" content="<?php echo $img_src[0]; ?>" /> | |
| <meta name="twitter:site" content="21organic" /> | |
| <?php | |
| } else { | |
| return; | |
| } | |
| } | |
| add_action('wp_head', 'fb_opengraph', 5); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment