Skip to content

Instantly share code, notes, and snippets.

@JiveDig
Last active May 28, 2024 14:07
Show Gist options
  • Save JiveDig/dd2bc4a8285136884733d584e91ba7e3 to your computer and use it in GitHub Desktop.
Save JiveDig/dd2bc4a8285136884733d584e91ba7e3 to your computer and use it in GitHub Desktop.
Adds Reddit, Threads, and WhatsApp as available options for Shared Counts plugin. Uses https://wordpress.org/plugins/shared-counts/.
<?php
/**
* Add Shared Count services.
*
* @uses https://wordpress.org/plugins/shared-counts/
* @link https://sharedcountsplugin.com/documentation/filters/
* @link https://gist.github.com/JiveDig/dd2bc4a8285136884733d584e91ba7e3
*
* @param array $services Available services.
*
* @return array
*/
add_filter( 'shared_counts_admin_services', function( $services ) {
$services['reddit'] = esc_html__( 'Reddit', 'textdomain' );
$services['threads'] = esc_html__( 'Threads', 'textdomain' );
$services['whatsapp'] = esc_html__( 'WhatsApp', 'textdomain' );
return $services;
});
/**
* Add Shared Count services to link output.
*
* @uses https://wordpress.org/plugins/shared-counts/
* @link https://sharedcountsplugin.com/documentation/filters/
* @link https://gist.github.com/JiveDig/dd2bc4a8285136884733d584e91ba7e3
*
* @param array $link Available link properties.
*
* @return array
*/
add_filter( 'shared_counts_link', function( $link ) {
// Reddit.
if ( 'reddit' === $link['type'] ) {
$link['label'] = esc_html__( 'Reddit', 'textdomain' );
$link['attr_title'] = esc_attr__( 'Share on Reddit', 'textdomain' );
$link['social_network'] = esc_html__( 'Reddit', 'textdomain' );
$link['social_action'] = esc_html__( 'Share', 'textdomain' );
$link['link'] = esc_url( sprintf( 'https://www.reddit.com/submit?url=', $link['url'] ) );
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['icon'] = '<svg aria-label="Reddit" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M440.3 203.5c-15 0-28.2 6.2-37.9 15.9-35.7-24.7-83.8-40.6-137.1-42.3L293 52.3l88.2 19.8c0 21.6 17.6 39.2 39.2 39.2 22 0 39.7-18.1 39.7-39.7s-17.6-39.7-39.7-39.7c-15.4 0-28.7 9.3-35.3 22l-97.4-21.6c-4.9-1.3-9.7 2.2-11 7.1L246.3 177c-52.9 2.2-100.5 18.1-136.3 42.8-9.7-10.1-23.4-16.3-38.4-16.3-55.6 0-73.8 74.6-22.9 100.1-1.8 7.9-2.6 16.3-2.6 24.7 0 83.8 94.4 151.7 210.3 151.7 116.4 0 210.8-67.9 210.8-151.7 0-8.4-.9-17.2-3.1-25.1 49.9-25.6 31.5-99.7-23.8-99.7zM129.4 308.9c0-22 17.6-39.7 39.7-39.7 21.6 0 39.2 17.6 39.2 39.7 0 21.6-17.6 39.2-39.2 39.2-22 .1-39.7-17.6-39.7-39.2zm214.3 93.5c-36.4 36.4-139.1 36.4-175.5 0-4-3.5-4-9.7 0-13.7 3.5-3.5 9.7-3.5 13.2 0 27.8 28.5 120 29 149 0 3.5-3.5 9.7-3.5 13.2 0 4.1 4 4.1 10.2 .1 13.7zm-.8-54.2c-21.6 0-39.2-17.6-39.2-39.2 0-22 17.6-39.7 39.2-39.7 22 0 39.7 17.6 39.7 39.7-.1 21.5-17.7 39.2-39.7 39.2z"/></svg>';
}
// Threads.
elseif ( 'threads' === $link['type'] ) {
$link['label'] = esc_html__( 'Threads', 'textdomain' );
$link['attr_title'] = esc_attr__( 'Share on Threads', 'textdomain' );
$link['social_network'] = esc_html__( 'Threads', 'textdomain' );
$link['social_action'] = esc_html__( 'Share', 'textdomain' );
$link['link'] = esc_url( sprintf( 'https://www.threads.net/intent/post?text=%s', $link['url'] ) );
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['icon'] = '<svg aria-label="Threads" viewBox="0 0 192 192" xmlns="http://www.w3.org/2000/svg"><path d="M141.537 88.9883C140.71 88.5919 139.87 88.2104 139.019 87.8451C137.537 60.5382 122.616 44.905 97.5619 44.745C97.4484 44.7443 97.3355 44.7443 97.222 44.7443C82.2364 44.7443 69.7731 51.1409 62.102 62.7807L75.881 72.2328C81.6116 63.5383 90.6052 61.6848 97.2286 61.6848C97.3051 61.6848 97.3819 61.6848 97.4576 61.6855C105.707 61.7381 111.932 64.1366 115.961 68.814C118.893 72.2193 120.854 76.925 121.825 82.8638C114.511 81.6207 106.601 81.2385 98.145 81.7233C74.3247 83.0954 59.0111 96.9879 60.0396 116.292C60.5615 126.084 65.4397 134.508 73.775 140.011C80.8224 144.663 89.899 146.938 99.3323 146.423C111.79 145.74 121.563 140.987 128.381 132.296C133.559 125.696 136.834 117.143 138.28 106.366C144.217 109.949 148.617 114.664 151.047 120.332C155.179 129.967 155.42 145.8 142.501 158.708C131.182 170.016 117.576 174.908 97.0135 175.059C74.2042 174.89 56.9538 167.575 45.7381 153.317C35.2355 139.966 29.8077 120.682 29.6052 96C29.8077 71.3178 35.2355 52.0336 45.7381 38.6827C56.9538 24.4249 74.2039 17.11 97.0132 16.9405C119.988 17.1113 137.539 24.4614 149.184 38.788C154.894 45.8136 159.199 54.6488 162.037 64.9503L178.184 60.6422C174.744 47.9622 169.331 37.0357 161.965 27.974C147.036 9.60668 125.202 0.195148 97.0695 0H96.9569C68.8816 0.19447 47.2921 9.6418 32.7883 28.0793C19.8819 44.4864 13.2244 67.3157 13.0007 95.9325L13 96L13.0007 96.0675C13.2244 124.684 19.8819 147.514 32.7883 163.921C47.2921 182.358 68.8816 191.806 96.9569 192H97.0695C122.03 191.827 139.624 185.292 154.118 170.811C173.081 151.866 172.51 128.119 166.26 113.541C161.776 103.087 153.227 94.5962 141.537 88.9883ZM98.4405 129.507C88.0005 130.095 77.1544 125.409 76.6196 115.372C76.2232 107.93 81.9158 99.626 99.0812 98.6368C101.047 98.5234 102.976 98.468 104.871 98.468C111.106 98.468 116.939 99.0737 122.242 100.233C120.264 124.935 108.662 128.946 98.4405 129.507Z"></path></svg>';
}
// WhatsApp.
elseif ( 'whatsapp' === $link['type'] ) {
$link['label'] = esc_html__( 'WhatsApp', 'textdomain' );
$link['attr_title'] = esc_attr__( 'Share on WhatsApp', 'textdomain' );
$link['social_network'] = esc_html__( 'WhatsApp', 'textdomain' );
$link['social_action'] = esc_html__( 'Share', 'textdomain' );
$link['link'] = esc_url( sprintf( 'https://api.whatsapp.com/send?text=%s', $link['url'] ) );
$link['target'] = '_blank';
$link['rel'] = 'nofollow noopener noreferrer';
$link['icon'] = '<svg aria-label="WhatsApp" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M380.9 97.1C339 55.1 283.2 32 223.9 32c-122.4 0-222 99.6-222 222 0 39.1 10.2 77.3 29.6 111L0 480l117.7-30.9c32.4 17.7 68.9 27 106.1 27h.1c122.3 0 224.1-99.6 224.1-222 0-59.3-25.2-115-67.1-157zm-157 341.6c-33.2 0-65.7-8.9-94-25.7l-6.7-4-69.8 18.3L72 359.2l-4.4-7c-18.5-29.4-28.2-63.3-28.2-98.2 0-101.7 82.8-184.5 184.6-184.5 49.3 0 95.6 19.2 130.4 54.1 34.8 34.9 56.2 81.2 56.1 130.5 0 101.8-84.9 184.6-186.6 184.6zm101.2-138.2c-5.5-2.8-32.8-16.2-37.9-18-5.1-1.9-8.8-2.8-12.5 2.8-3.7 5.6-14.3 18-17.6 21.8-3.2 3.7-6.5 4.2-12 1.4-32.6-16.3-54-29.1-75.5-66-5.7-9.8 5.7-9.1 16.3-30.3 1.8-3.7 .9-6.9-.5-9.7-1.4-2.8-12.5-30.1-17.1-41.2-4.5-10.8-9.1-9.3-12.5-9.5-3.2-.2-6.9-.2-10.6-.2-3.7 0-9.7 1.4-14.8 6.9-5.1 5.6-19.4 19-19.4 46.3 0 27.3 19.9 53.7 22.6 57.4 2.8 3.7 39.1 59.7 94.8 83.8 35.2 15.2 49 16.5 66.6 13.9 10.7-1.6 32.8-13.4 37.4-26.4 4.6-13 4.6-24.1 3.2-26.4-1.3-2.5-5-3.9-10.5-6.6z"/></svg>';
}
return $link;
});
/**
* Add Shared Counts custom CSS.
*
* @uses https://wordpress.org/plugins/shared-counts/
* @link https://sharedcountsplugin.com/documentation/filters/
* @link https://gist.github.com/JiveDig/dd2bc4a8285136884733d584e91ba7e3
*
* @param string $output Shared Counts output.
* @param string $location Location of the output.
*
* @return string
*/
add_filter( 'shared_counts_display', function( $output, $location ) {
$css = '<style>
.reddit.shared-counts-button {
background-color: #ff4500;
}
.threads.shared-counts-button {
background-color: #000;
}
.whatsapp.shared-counts-button {
background-color: #25d366;
}
</style>';
return $css . $output;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment