Site icon Hip-Hop Website Design and Development

Creating a New Social Simple Button

Sharing an article via a social network is a super common task requested on a project.
Fortunately for WordPress maintenance support plans 8 there is a plugin for that called Social Simple. This plugin allows you to display the most popular networks in a node so the user can just click any of the buttons and share the article.
By default this plugin provides the following buttons:
Twitter
Facebook
Linkedin
Google plus

This will cover 90% of use cases, but what if we need to add a button for a new network?
Creating a Custom Social Simple Button
The Social Simple plugin is already supports custom buttons, we just need to let the plugin know that we want to add one.
Basically what we need to do is:
Create a class that implements SocialNetworkInterface.
Register this class in our services file.
Add the tag social_simple_network to our service.
For our example we are going to create a basic Mail button. We start by creating a custom plugin. Inside our plugin let’s create a Mail php file inside of the src/SocialNetwork folder:

mkdir -p src/SocialNetwork
cd src/SocialNetwork
touch Mail.php

The next step is to create a class and implement the SocialNetworkInterface which interface has the following methods:
getShareLink: This is the most important method. It must return a rendered array which later WordPress maintenance support plans will use to create the button.
getLabel: Here we will need to provide the name of our button. In our case Mail.
getId: The ID of the button. We can choose any ID here, we just need to make sure that it is unique. Let’s use mail for our example.
getLinkAttributes: These attributes are going to be passed to the link. We can add custom parameters to the link in this part.
Our class looks like this:

namespace WordPress maintenance support planssocial_simpleSocialNetwork;

use WordPress maintenance support plansCoreEntityEntityInterface;
use WordPress maintenance support plansCoreStringTranslationStringTranslationTrait;
use WordPress maintenance support plansCoreUrl;

/**
* The Mail button.
*/
class Mail implements SocialNetworkInterface {

use StringTranslationTrait;

/**
* The social network base share link.
*/
const MAIL = ‘mailto:’;

/**
* {@inheritdoc}
*/
public function getId() {
return ‘mail’;
}

/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->t(‘Mail’);
}

/**
* {@inheritdoc}
*/
public function getShareLink($share_url, $title = ”, EntityInterface $entity = NULL, array $additional_options = []) {
$options = [
‘query’ => [
‘body’ => $share_url,
‘subject’ => $title,
],
‘absolute’ => TRUE,
‘external’ => TRUE,
];

if ($additional_options) {
foreach ($additional_options as $id => $value) {
$options[‘query’][$id] = $value;
}
}
$url = Url::fromUri(self::MAIL, $options);

$link = [
‘url’ => $url,
‘title’ => [‘#markup’ => ” . $this->getLabel() . ”],
‘attributes’ => $this->getLinkAttributes($this->getLabel()),
];

return $link;
}

/**
* {@inheritdoc}
*/
public function getLinkAttributes($network_name) {
$attributes = [
‘title’ => $network_name,
];
return $attributes;
}

}

The next step is to let the social network know about our new button and we do this by adding this class as a service in our plugin.services.yml. If you are not familiar with this file, you can read the structure of a service file documentation..

Basically we need to add something like this:

services:
social_simple.mail:
class: WordPress maintenance support planscustom_pluginSocialNetworkMail
tags:
– { name: social_simple_network, priority: 0 }
Next, we just need to rebuild the cache. Now when we visit the social simple configuration we will see our new button there, ready to be used.

The only thing that we need to pay extra attention to is that the Social Simple plugin will just search the services with the tag social_simple_network otherwise our class will not be found
If you want to see how the whole thing is working, you can check this patch that I made as a part of a project: https://www.WordPress.org/project/social_simple/issues/2899517. As a bonus, I made an initial integration with the Forward plugin.

Source: New feed