Forum
Social media share buttons
Hi, I'm trying to create facebook and twitter share buttons using code:
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php perch_page_url(); ?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=no,scrollbars=no,height=400,width=600');return false;">Share</a>
<a href="https://twitter.com/home?status=<?php perch_blog_post_field($post_slug, 'postTitle'); ?> <?php perch_page_url(); ?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=no,scrollbars=no,height=400,width=600');return false;">Share</a>
I also used:
http%3A//www.domain.com/<?php echo PerchSystem::get_page(); ?>
instead of
<?php perch_page_url(); ?>
but rather than the current blog page url I'm getting blog/post.php shared. Any idea why?
Hello Robert,
That's because this is the actual page URL. You need to use something like this to get the full URL:
Then you also need to encode the URL since you are passing it as URL parameter.
You can use Pipit Sharing to generate sharing links. You can add the links in your Perch template instead of having them with PHP.
Your template would like like:
And to pass the post title to your tweet, you can use something like this:
Hi Hussain,
Thanks for that. I've installed Pipit Sharing and added the links to post.html. The result when sharing with twitter is: https:////blog/2018-04-04-Title. I don't really understand what and where to configure as I don't know PHP.
You don't have to change any PHP.
Log in to Perch and go to Settings. You'll find a field labelled Website URL. Add your website URL in there and the app will use it.
This is mentioned in the app documentation: https://grabapipit.com/pipits/apps/sharing/docs/#configuration
Oh, thanks for that! I didn't realise that the settings referred to Perch settings and was looking for them in Pipits. Great app!