Skip to content

Instantly share code, notes, and snippets.

@WordPress-Handbuch
Last active September 4, 2019 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WordPress-Handbuch/af4c9e0c5ffbbf4d8aedece71cc7c35d to your computer and use it in GitHub Desktop.
Save WordPress-Handbuch/af4c9e0c5ffbbf4d8aedece71cc7c35d to your computer and use it in GitHub Desktop.
PHP/HTML blog snippet for proper WordPress HTML meta tag integration for social network sharing
<?php
if (is_single())
{
$title = htmlspecialchars( strip_tags( get_the_title( $post->ID ) ) );
$description = htmlspecialchars( strip_tags( get_the_excerpt( $post->ID ) ) );
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$image = $thumbnail[0];
$type = 'article';
} elseif (is_page()) {
$title = htmlspecialchars( strip_tags( get_the_title( $post->ID ) ) );
$thispage = get_post( $post->ID );
$description = htmlspecialchars( wp_trim_words( $thispage->post_content, 65 ) );
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$image = $thumbnail[0];
$type = 'article';
} elseif (is_category()) {
$title = htmlspecialchars( strip_tags( get_the_archive_title() ) );
$description = htmlspecialchars( strip_tags( get_the_archive_description() ) );
$image = 'https://ihredomain/ihrvorschaubild.jpg';
$type = 'blog';
}
if (is_front_page()) {
$title = htmlspecialchars( strip_tags( get_bloginfo( 'name' ) ) ) . ' – ' . htmlspecialchars( strip_tags( get_bloginfo( 'description' ) ) );
$description = htmlspecialchars( strip_tags( get_bloginfo( 'description' ) ) );
$image = 'https://ihredomain/ihrvorschaubild.jpg';
$type = 'blog';
}
?>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="twitter:site" content="@<?php echo strip_tags( get_bloginfo( 'name' ) ); ?>" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="<?php echo $title; ?>" />
<meta name="twitter:description" content="<?php echo $description; ?>" />
<meta name="twitter:image" content="<?php echo $image; ?>" />
<meta name="twitter:url" content="<?php the_permalink(); ?>" />
<meta property="og:site_name" content="<?php echo strip_tags( get_bloginfo( 'name' ) ); ?>" />
<meta property="og:title" content="<?php echo $title; ?>" />
<meta property="og:description" content="<?php echo $description; ?>" />
<meta property="og:type" content="<?php echo $type; ?>" />
<meta property="og:image" content="<?php echo $image; ?>" />
<meta property="og:url" content="<?php the_permalink(); ?>" />
<link rel="image_src" href="<?php echo $image; ?>" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment