From WordPress post to Facebook easily and without plugins

When you run a blog, you’ll often run a Facebook page too where to share your posts like I am doing with my official Facebook page. Or maybe you don’t – although you SHOULD – but you want links people share on Facebook from your blog to appear nice and well formed. There are dozens of plugins doing this job pretty well, but I strongly prefer to write my own code to perform easy tasks rather than relying on third party software. Follow me and you’ll create your WordPress Open Graph meta generator in 15 lines of code. The Open Graph protocol enables any web page to become a rich object in a social graph. For instance, this is used on Facebook to allow any web page to have the same functionality as any other object on Facebook. Facebook integrated Open Graph in 2010 allowing to decide how a third-party page is shown on Facebook when it’s shared by including some Open Graph meta tags in the <head> part of the website’s code. Moving From theory to real world examples, to turn your web pages into graph objects, you need to add some basic metadata to your page, but before let me show you a page which will help you a lot during the process of creating your Open Graph tags: https://developers.facebook.com/tools/debug/og/object/ It’s the official Facebook Open Graph debugger which will show you how your page will look once shared on Facebook. og:title – The title of your object as it should appear within the graph, which is your post/page title og:type – The type of your object, mostly “article” if you run a blog. og:image – An image URL which should represent your object within the graph, typically the featured image og:url – The URL of your post og:description – A one to two sentence description of your article, like the excerpt. og:site_name – The name of your site How can we add these meta tags into your blog <head> section? By adding this function into your functions.php file located in your theme folder:
function facebooktag(){
     global $post;
     if(is_singular()){
          echo "<meta property=\"og:title\" content=\"" . get_the_title() . "\"/&gt;\n";
          echo "<meta property=\"og:type\" content=\"article\"/&gt;\n";
          echo "<meta property=\"og:url\" content=\"" . get_permalink() . "\"/&gt;\n";
          echo "<meta property=\"og:description\" content=\"" . get_the_excerpt() . "\"/&gt;\n";
          echo "<meta property=\"og:site_name\" content=\"" . esc_html(get_bloginf("name")) . "\"/&gt;\n";
          if(!has_post_thumbnail($post-&gt;ID)){ 
               echo "<meta property=\"og:image\" content=\"http://www.emanueleferonato.com/images/emanuele_feronato_logo.png\"/&gt;\n";
          }
          else{
               $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post-&gt;ID), "medium");
               echo "<meta property=\"og:image\" content=\"" . esc_attr($thumbnail_src[0]) . "\"/&gt;\n";
          }     
     }
}
add_action("wp_head", "facebooktag");
The code is quite intuitive, just notice I used get_the_excerpt function rather than the_excerpt because get_the_excerpt returns the excerpt without any <p> tag. And here you go with your post nicely shared on Facebook.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers