Skip to content

clubdeuce/wp-share-this

Repository files navigation

Scrutinizer Code Quality

WP Share-This

This library allows for easy implementation of sharing links using the ShareThis. Please note, this library is in no way affiliated with ShareThis or its subsidiaries.

Installation

The easiest method for including this library is via Composer. Simply run the follow command in your project root (assuming that is where your composer.json is located):

composer require clubdeuce/wp-share-this

This library will be autoloaded, assuming you include vendor/autoload.php in your project.

You can also download this library and include it manually.

Usage

  • Register service(s): WP_Share_This::register_service( 'twitter' ). The full list of supported services can be found here.
  • Initialize the library: WP_Share_This::initialize().
  • Render the links: WP_Share_This::the_sharing_links().

Methods

Method Description Since
register_id( string $share_this_id ) Specify the ShareThis user id for tracking 0.0.1
register_service( string $service_name, array $params ) Specify a service to be included. You can pass parameters specific to this service (see below for a list of parameters). 0.0.1
the_sharing_links( null/array $params ) Output the sharing links. You can pass parameters specific to this block of links (see below for a list of parameters). 0.0.1
use_og( bool $use ) Specify whether to include the OpenGraph information in the <head> of the document. Default: true. 0.0.2

Parameters

The following parameters can be specified for the sharing links on a per-service basis (not all items apply to every service):

Name Description
url Specifies URL (can be shortened URL) that you would like shared. Default: current global $post permalink.
short_url Specifies a shortened URL that can be used in conjunction with the data-url. Default: null
title Specifies title that you would like shared. Defaults to current global $post title.
image Specifies link to image you would like displayed in the shared content. Default: current global $post featured image if present.
description Specifies summary text/description you wish to share. Default: current global $post excerpt.
username Specifies your Twitter username.
message Specifies the body copy of e-mail shares. Default: current global $post excerpt.
share_count Specify whether to include the share count for this individual link item. Default: true.

Examples:

To register services with unique parameters:

WP_Share_This::register_service( 'twitter', array(
    'username'    => 'myTwitterHandle',
    'share_count' => false,
) );

WP_Share_This::register_service( 'facebook', array(
    'image' => 'https://example.com/image.png',
);

WP_Share_This::register_service( 'email', array(
    'message' => __( 'Look at this really interesting article I found!', 'txt_domain' ),
) );  

To render the links with overrides to the defaults:

WP_Share_This::the_sharing_links( array(
    'share_count' => false,
) );

To render sharing links for a post other than the current global $post item:

WP_Share_This::the_sharing_links( array(
    'post' => get_post( $post_id ),
) );

Filters

This library exposes a number of filters:

Filter Description Parameters Since
wpst_link_classes_$service This filter allows you to specify classes for each service link, _e.g. add_filter( 'wpst_classes_twitter', 'my_function' ) array classnames 0.0.1
wpst_og_url Allows you to filter the URL exposed via OpenGraph string URL 0.0.1
wpst_og_title Allows you to filter the title exposed via OpenGraph string title 0.0.1
wpst_og_description Allows you to filter the description exposed via OpenGraph string description 0.0.1
wpst_og_image Allows you to filter the image exposed via OpenGraph string image URL 0.0.1
wpst_link_text Allows you to filter the text displayed for the sharing link (defaults to the service name) string text 0.0.1

About

A WP Library for custom ShareThis buttons

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages