Forum

Thread tagged as: Question, Problem, Blog

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?

Robert Fiszer

Robert Fiszer 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Robert,

but rather than the current blog page url I'm getting blog/post.php shared. Any idea why?

That's because this is the actual page URL. You need to use something like this to get the full URL:

$full_url = 'https://example.com' . $_SERVER['REQUEST_URI'];

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:

<!--* Facebook *-->
<a href="<perch:sharing id="facebook" />" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=no,scrollbars=no,height=400,width=600');return false;">Share</a>

<!--* Twitter *-->
<a href="<perch:sharing id="twitter" />" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=no,scrollbars=no,height=400,width=600');return false;">Share</a>

And to pass the post title to your tweet, you can use something like this:

perch_blog_custom([
    'template' => 'post.html',
    'filter' => 'postSlug',
    'match' => 'eq',
    'value' => perch_get('s'),
    'each' => function($item) {
        $item['sharing_twitter_desc'] = $item['postTitle'];
        return $item;
    }
]);

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.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

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!