Skip to content

Instantly share code, notes, and snippets.

@JacquesvanWyk
Last active April 19, 2018 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacquesvanWyk/26463d0e5b18cf18b10372f090bd7f0f to your computer and use it in GitHub Desktop.
Save JacquesvanWyk/26463d0e5b18cf18b10372f090bd7f0f to your computer and use it in GitHub Desktop.

Share you Wordpress post with facebook using correct post url

If you struggle to get the correct url for your share button here is a fix.

Getting Started

I assume here you know how to get the button up and running if you dont please just go to https://developers.facebook.com/docs/plugins/share-button and follow instructions

Code to get the correct url shared

We need to just make function calling the correct url when clicking share so go to your functions.php file and add this code:

function myblog_post_by_slug($the_slug, $post_type = "page"){ $args = array( 'name' => $the_slug, 'post_type' => $post_type, 'post_status' => 'publish', 'numberposts' => 1 ); $my_page = get_posts($args)[0]; return $my_page; }

This function will help get the slug of your current post now all you need to do is change the data-href(you get this in step 3 on facbebook developers) to this:

data-href="<?php echo myblog_post_by_slug('map'); ?>"

Hope this helped if you got problems leave comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment