Sharing: Advanced Examples

Programatically

Reserved IDs

This is the old method from v1.x. It is still supported.

You can programatically set the options to a variable and Pipit Sharing will use them for your sharing links.

PerchSystem::set_var('sharing_twitter_desc', $pageTitle);

You can also use the each option (not applicable to all functions) and set the options you want in the closure:

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

You don’t have to use the each option. As long as you set the variables before the template with the perch:sharing tags are rendered:

// $author_username = get author's twitter username
// $pinterest_media = URL of an image to use when sharing on Pinterest

PerchSystem::set_var('sharing_twitter_via', $author_username);
PerchSystem::set_var('sharing_pinterest_media', $pinterest_media);
perch_content_custom('My region', [
    'template'=>'sharing_links.html',
]);

Tag attributes

added to v2.0

You no longer have to use the reserved IDs. Use whatever ID you want and then use it in your tag attributes:

PerchSystem::set_var('title', $pageTitle);
<perch:sharing id="twitter" desc="{title}">

Manually set the item URL

added to v1.2

By default Pipit Sharing uses the URL of the current page. So if you add a sharing link to a blog post page /blog/my-post, it will get the URL of that page. If you add sharing links to your posts in the blog listing page /blog, it will use the URL of that page by default rather than the actual post URL.

This is a regular blog listing:

perch_blog_custom([
    'template' => 'post_in_list.html',
    'sort' => 'postDateTime',
    'sort-order' => 'DESC',
    'count' => 9,
    'paginate' => true,
]);

v1.x

Since v1.2 you can change this behaviour.

If you want to add sharing links to each post in post_in_list.html, you need to set sharing_item_url:

perch_blog_custom([
    'template' => 'post_in_list.html',
    'sort' => 'postDateTime',
    'sort-order' => 'DESC',
    'count' => 9,
    'paginate' => true,
    'each' => function($item) {
        $item['sharing_item_url'] = $item['postURL'];
        return $item;
    }
]);

And it is the same for other types of items. Here’s another example using Runway Collections:

If you have a Collection in which each item has a slug field and the item URL is /events/{slug}, you would use this:

perch_collection('Events', [
    'template' => 'event_in_list.html',
    'each' => function($item){
        $item['sharing_item_url'] = '/events/' . $item['slug'];
        return $item;
    }
]);

v2.x

You can specify this directly in the tag template with the url attribute:

<perch:sharing id="twitter" url="{postURL}">

By default the Sharing app prefix the domain of your website to the value or url. You can hide it if you don’t need it:

<perch:sharing id="twitter" url="https://example.com" include-domain="false">

Doing it yourself

While hardcoding isn’t an “advanced” way to handle things, you can always hardcode an option if you know the correct parameter to use:

<a href="<perch:sharing id="twitter">&via=username&hashtags=pipits,perch">Share on Twitter</a>

This also means you can make the value of the parameters editable and take control of things yourself. Perhaps a new parameter can be used in some sharing links and you can’t wait for Pipit Sharing to add support for it. In this case you need to make sure it’s in the correct format yourself.

<a href="<perch:sharing id="twitter" />&param=<perch:content id="param_value" type="text" urlencod="true" />" target="_blank">Share on Twitter</a>
link