0

I wanted to make a sharing code to social media without using some plugins. i have the code the works on sharing but my problem is how can i seperated it from wordpress. for example how can i extract or get the url each posts, javascript how to seperate it, anyone to seperate it?

Here is the manual code the working. How can i apply this code to all posts???

<a class="social-share__link" href="#" target="_blank" data-href="https://www.facebook.com/sharer/sharer.php?u=https://bptapartments.com/how-renting-an-apartment-works/" onclick="javascript:window.open(this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><i class="fab fa-facebook-f"></i></a>
soveva
  • 3
  • 2

1 Answers1

0

you can make a function in functions.php, preferably into a child theme.

The code could be something like this

add_filter('the_content', 'my_content_add_fb_sharer');
function my_content_add_fb_sharer($content){
    $urlToShare = urlencode(get_permalink());
    $fbShare = '<a class="social-share__link" href="#" target="_blank" data-href="https://www.facebook.com/sharer/sharer.php?u='.$urlToShare.'" onclick="javascript:window.open('."this.dataset.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;\"><i class=\"fab fa-facebook-f\"></i></a>'";

     return $content.$fbShare;
}

This will put the share link after the post content. If you want to be displayed over the content you could do

 return $fbShare.$content

Also, since everything in wordpress is a post, this snippet will make this fb share link appear on ...everything!

Hope it helps!

qubix
  • 16
  • 1
  • Wow the code is GREAT!!! however how can i output multiple return? i mean i want also add twitter sharer. – soveva Apr 30 '23 at 09:30
  • also it is possible to make that shortcode? so i can place anywhere? – soveva Apr 30 '23 at 09:37
  • Yes you can add more variables like $twShare for twitter for example and add it to return like $content.$fbShare.$twShare. But beware the html output! I suggest wrapping all these links in a div with a class like "social-share" to control its appearance with css – qubix Apr 30 '23 at 10:55
  • As for a shortcode you can do it with the add_shortcode('myshares', 'my_content_add_fb_sharer'); and use it like any other shortcode. Remove from the original code the add_filter part though so it would not fire up on posts by default. – qubix Apr 30 '23 at 11:03
  • Can you help me on the twitter sharer because its very different to fb.. Thank you... – soveva Apr 30 '23 at 21:14