Below, you’ll find a detailed guide on how to add the Simple Social Buttons Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Simple Social Buttons Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Simple Social Buttons Plugin and the shortcodes it provides:
"Simple Social Media Share Buttons is a user-friendly plugin for everyone. Enhance your website's reach with its easy social sharing options. Just a click, and your content is shared across various platforms!"
- [SSB]
Simple Social Buttons [SSB] Shortcode
The Simple Social Buttons shortcode is a tool used to customize the appearance and functionality of social sharing buttons on a WordPress site. It allows users to specify the theme, alignment, button size, and order of the social buttons. The shortcode also provides options to show or hide the total count and individual share counts. It supports various social platforms including Facebook, Twitter, LinkedIn, Reddit, WhatsApp, and Email. The shortcode is highly flexible, making it easy to integrate social sharing buttons that fit the style and requirements of any website.
Shortcode: [SSB]
Parameters
Here is a list of all possible SSB shortcode parameters and attributes:
counter
– shows or hides the share count, accepts true or falseshow_total
– displays or hides the total share count, accepts true or falsealign
– sets the alignment of the buttons, accepts left, right, or centeredorder
– sets the display order of the social media buttonslike_button_size
– sets the size of the like button, accepts small or largetheme
– sets the style of the buttons, accepts theme1, theme2, theme3, theme4, Flat, Circle, or Official
Examples and Usage
Basic example – Displaying social buttons with the default settings.
[SSB /]
Advanced examples
Using the shortcode to display social buttons with a specific theme, order of buttons, alignment, and button size.
[SSB theme='Flat' order='twitter,linkedin,fbshare' align='center' like_button_size='large' /]
Using the shortcode to display social buttons with counter and total count enabled.
[SSB counter='true' show_total_count='true' /]
Using the shortcode to display social buttons with a specific theme and alignment, and counter enabled.
[SSB theme='Circle' align='right' counter='true' /]
PHP Function Code
In case you have difficulties debugging what causing issues with [SSB]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'SSB', array( $this, 'short_code_content' ) );
Shortcode PHP function:
function short_code_content( $atts ) {
/*
counter = true,false
show_total = true,false
align = left ,right,centered,
order = googleplus,twitter,pinterest,fbshare,linkedin,reddit,whatsapp,viber,fblike,messenger,email
like_button_size = small or large , Default small
theme
theme1 = sm-round
theme2 = simple-round
theme3 = round-txt
theme4 = round-btm-border
Flat = flat-button-border
Circle = round-icon
Official = simple-icons
*/
$selected_theme = shortcode_atts(
array(
'theme' => '',
'order' => null,
'align' => '',
'counter' => 'false',
'show_total_count' => 'false',
'like_button_size' => 'small',
),
$atts
);
$themes = array(
'theme1' => 'sm-round',
'theme2' => 'simple-round',
'theme3' => 'round-txt',
'theme4' => 'round-btm-border',
'Flat' => 'flat-button-border',
'Circle' => 'round-icon',
'Official' => 'simple-icons',
);
if ( key_exists( $selected_theme['theme'], $themes ) ) {
foreach ( $themes as $key => $value ) {
if ( $selected_theme['theme'] == $key ) {
$theme = $themes[ $key ];
}
}
} else {
$theme = $this->selected_theme;
}
if ( null !== $selected_theme['order'] && '' !== $selected_theme['order'] ) {
$selected_theme['order'] = array_flip( array_merge( array( 0 ), explode( ',', $selected_theme['order'] ) ) );
} else {
$selected_theme['order'] = $this->selected_networks;
}
// if ( isset( $this->selected_position['inline'] ) ) {
// Show Total at the end.
if ( $selected_theme['show_total_count'] == 'true' ) {
$show_total = true;
} else {
$show_total = false;
}
if ( empty( $selected_theme['align'] ) ) {
$align_class = $this->_get_settings( 'inline', 'icon_alignment', 'left' );
} else {
$align_class = $selected_theme['align'];
}
$extra_class = 'simplesocialbuttons_inline simplesocialbuttons-align-' . esc_html( $align_class );
// if ( $this->inline['share_counts'] ) {
if ( $selected_theme['counter'] == 'true' ) {
$show_count = true;
$extra_class .= ' ssb_counter-activate';
} else {
$show_count = false;
}
// set fb like button size
$like_button_size = $selected_theme['like_button_size'];
$extra_class .= ' simplesocialbuttons-inline-' . $this->_get_settings( 'inline', 'animation', 'no-animation' );
$extra_option = array(
'class' => $extra_class,
'theme' => $theme,
'like-button-size' => esc_html( $like_button_size ),
'position' => 'shortcode',
);
$ssb_buttons_code = $this->generate_buttons_code( $selected_theme['order'], $show_count, $show_total, $extra_option );
// }
return $ssb_buttons_code;
}
/**
* Add Meta Tags.
*
* @access public
* @since 2.0.9
* @version 3.1.0
* @return string
*/
public function add_meta_tags() {
// isset added if some one updagrate 10 3.1.0 he have not saving this setting save in database.
if ( isset( $this->extra_option['ssb_og_tags'] ) && '1' !== $this->extra_option['ssb_og_tags'] ) {
return;
}
$og_tag = '';
$og_tag .= PHP_EOL . '<!-- Open Graph Meta Tags generated by Simple Social Buttons ' . $this->pluginVersion . ' -->' . PHP_EOL;
if ( $this->og_get_title() ) {
$og_tag .= '<meta property="og:title" content="' . get_the_title() . ' - ' . get_bloginfo( 'name' ) . '" />' . PHP_EOL;
}
if ( $this->og_get_description() ) {
$og_tag .= '<meta property="og:description" content="' . $this->og_get_description() . '" />' . PHP_EOL;
}
$og_tag .= '<meta property="og:url" content="' . get_permalink() . '" />' . PHP_EOL;
if ( $this->og_get_blog() ) {
$og_tag .= '<meta property="og:site_name" content="' . $this->og_get_blog() . '" />' . PHP_EOL;
}
$og_tag .= $this->get_og_image();
$og_tag .= '<meta name="twitter:card" content="summary_large_image" />' . PHP_EOL;
if ( $this->og_get_description() ) {
$og_tag .= '<meta name="twitter:description" content="' . $this->get_excerpt_by_id( get_the_id() ) . '" />' . PHP_EOL;
}
if ( $this->og_get_title() ) {
$og_tag .= '<meta name="twitter:title" content="' . get_the_title() . ' - ' . get_bloginfo( 'name' ) . '" />' . PHP_EOL;
}
$og_tag .= $this->generate_twitter_image();
echo apply_filters( 'ssb_og_tag', $og_tag );
}
/**
* Get title for open graph / meta description.
*
* @access public
* @since 1.0.0
* @return string
*/
public function og_get_title() {
return get_the_title() . ' - ' . get_bloginfo( 'name' );
}
/**
* Get description for the Open Graph / meta descriptoin.
*
* @access public
* @since 1.0.0
* @return string
*/
public function og_get_description() {
return $this->get_excerpt_by_id( get_the_id() );
}
/**
* Get blog name for Open graph / meta descripton.
*
* @access public
* @since 1.0.0
* @return void
*/
public function og_get_blog() {
return get_bloginfo( 'name' );
}
/**
* Get the excerpt
*
* @param int $post_id
*
* @access public
* @since 2.0.9
* @return string
*/
public function get_excerpt_by_id( $post_id ) {
if ( ! $post_id ) {
return;
}
// Check if the post has an excerpt
if ( has_excerpt() ) {
$excerpt_length = apply_filters( 'excerpt_length', 35 );
return trim( wp_strip_all_tags( get_the_excerpt() ) );
}
$the_post = get_post( $post_id ); // Gets post ID
$the_excerpt = $the_post->post_content; // Gets post_content to be used as a basis for the excerpt
$excerpt_length = 60; // Sets excerpt length by words
$the_excerpt = strip_tags( strip_shortcodes( $the_excerpt ) ); // Strips tags and images
$words = explode( ' ', $the_excerpt, $excerpt_length + 1 );
if ( count( $words ) > $excerpt_length ) {
array_pop( $words );
$the_excerpt = implode( ' ', $words );
}
return trim( wp_strip_all_tags( $the_excerpt ) );
}
/**
* Get meta tage Image from content.
*
* @param string $post
* @since 2.0.10
* @return string
*/
public function get_content_images( $post ) {
if ( ! $post ) {
return;
}
$content = $post->post_content;
$images = '';
if ( preg_match_all( '`<img [^>]+>`', $content, $matches ) ) {
foreach ( $matches[0] as $img ) {
if ( preg_match( '`src=(["\'])(.*?)\1`', $img, $match ) ) {
$images .= '<meta property="og:image" content="' . $match[2] . '" />' . PHP_EOL;
}
}
}
return $images;
}
/**
* Get the featured image / meta for open graph.
*
* @access public
* @since 2.0.10
* @return string Meta tag og:image for meta
*/
public function generate_og_image() {
$_post_id = (int) get_the_ID();
if ( has_post_thumbnail( $_post_id ) ) {
return '<meta property="og:image" content="' . wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ) . '" />' . PHP_EOL;
}
return $this->get_content_images( get_post( $_post_id ) );
}
/**
* Get Open Graph image.
*
* @access public
* @since 2.0.10
* @return mixed
*/
public function get_og_image() {
$image = $this->generate_og_image();
if ( $image ) {
return $image;
}
}
/**
* Get the featured image for Twitter.
*
* @access public
* @since 2.0.10
* @return mixed
*/
public function generate_twitter_image() {
$_post_id = (int) get_the_ID();
if ( has_post_thumbnail( $_post_id ) ) {
return '<meta property="twitter:image" content="' . wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ) . '" />' . PHP_EOL;
}
return $this->get_twitter_content_images( get_post( $_post_id ) );
}
/**
* Get Image from content for Twitter.
*
* @param object $post
*
* @access public
* @since 2.0.10
* @return mixed
*/
public function get_twitter_content_images( $post ) {
if ( ! $post ) {
return;
}
$content = $post->post_content;
$images = '';
if ( preg_match_all( '`<img [^>]+>`', $content, $matches ) ) {
foreach ( $matches[0] as $img ) {
if ( preg_match( '`src=(["\'])(.*?)\1`', $img, $match ) ) {
$images .= '<meta property="twitter:image" content="' . $match[2] . '" />' . PHP_EOL;
}
}
}
return $images;
}
/**
* User to convert http to https or vice versa.
*
* @param $url
*
* @access public
* @since 2.0.12
* @return srting
*/
public function http_or_https_resolve_url( $url ) {
$arr_parsed_url = parse_url( $url );
if ( ! empty( $arr_parsed_url['scheme'] ) ) {
if ( 'http' === $arr_parsed_url['scheme'] ) {
return $url = str_replace( 'http', 'https', $url );
} elseif ( 'https' === $arr_parsed_url['scheme'] ) {
return $url = str_replace( 'https', 'http', $url );
}
}
}
/**
* Convert url http to https or vice versa.
*
* @param $permalink
*
* @access public
* @since 2.0.14
* @return mixed
*/
public function http_or_https_link_generate( $permalink ) {
foreach ( $this->arrKnownButtons as $social_name ) {
if ( ! ssb_is_network_has_counts( $social_name ) ) {
continue; }
$url = $this->http_or_https_resolve_url( $permalink );
// get alt hurl to cover http or https issue
$_alt_share_links[ $social_name ] = call_user_func( 'ssb_' . $social_name . '_generate_link', $url );
}
return $_alt_share_links;
}
/**
* Function use to remove button names on excerpt if above the content selecetd .
*
* @param string $text already excertp set.
* @param int $num_words word in excerpt.
* @param string $more more srting apprend in last of the excerpt sring.
* @param string $original_text original text of the string.
* @return string $text string of excerpt;
*
* @access public
* @since 2.0
* @return string
*/
public function on_excerpt_content( $text, $num_words, $more, $original_text ) {
if ( empty( $text ) ) {
return $text;
}
try {
// this will not show any warning if html not valid .
libxml_use_internal_errors( true );
// xamp
$dom = new DOMDocument();
if ( function_exists( 'mb_convert_encoding' ) ) {
$dom->loadHTML( mb_convert_encoding( $original_text, 'HTML-ENTITIES', 'UTF-8' ) );
} else {
// see @ https://stackoverflow.com/questions/11974008/alternative-to-mb-convert-encoding-with-html-entities-charset
$dom->loadHTML( htmlspecialchars_decode( utf8_decode( htmlentities( $original_text, ENT_COMPAT, 'utf-8', false ) ) ) );
}
$xpath = new DOMXPath( $dom );
$xpath->registerNamespace( 'h', 'http://www.w3.org/1999/xhtml' );
// using a loop in case there are multiple occurrences
foreach ( $xpath->query( "//div[contains(@class, 'simplesocialbuttons')]" ) as $node ) {
$paragraph_node = $node->parentNode;
// echo '<pre>';
// print_r( $paragraph_node );
$node->parentNode->removeChild( $node );
}
$text = wp_strip_all_tags( $dom->saveHTML() );
} catch ( Exception $e ) {
// echo 'Caught exception: ', $e->getMessage(), "\n";
$text = $text;
}
/*
* wp_trim_words() for more detail
* translators: If your word count is based on single characters (e.g. East Asian characters),
* enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
* Do not translate into your own language.
*/
if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
preg_match_all( '/./u', $text, $words_array );
$words_array = array_slice( $words_array[0], 0, $num_words + 1 );
$sep = '';
} else {
$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
$sep = ' ';
}
if ( count( $words_array ) > $num_words ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $more;
} else {
$text = implode( $sep, $words_array );
}
return $text;
}
/**
* Check is ssb on with position and post type.
*
* @param string $position location where you want to check ssb on/off.
*
* @access public
* @version 2.0.21
* @return boolean
*/
public function is_ssb_on( $position ) {
$is_ajax_callback = true;
if ( ! in_array( $this->get_post_type(), $this->_get_settings( $position, 'posts', array() ) ) ) {
$is_ajax_callback = false;
}
return $is_ajax_callback;
}
/**
* Action link on plugin page
*
* @param array $actions
* @return array
*
* @access public
* @since 2.1.4
* @return array
*/
public function filter_plugin_action_links( $actions_links ) {
$settings_link = '<a href="' . admin_url( 'admin.php?page=simple-social-buttons' ) . '">' . __( 'Settings', 'simple-social-buttons' ) . '</a>';
array_unshift( $actions_links, $settings_link );
if ( ! class_exists( 'Simple_Social_Buttons_Pro' ) ) {
$pro_link = sprintf( esc_html__( '%1$s %3$s Upgrade Pro %4$s %2$s', 'simple-social-buttons' ), '<a href="https://wpbrigade.com/wordpress/plugins/simple-social-buttons/" target="_blank">', '</a>', '<span class="simple-social-buttons-pro-link">', '</span>' );
array_push( $actions_links, $pro_link );
}
return $actions_links;
}
/**
* Register Block.
*
* @since 2.0.0
* @version 3.2.0
* @return void
*/
public function ssb_register_block() {
if ( ! function_exists( 'register_block_type' ) ) { // Backward compitablity check
return;
}
register_block_type(
'ssb/shortcode',
[
'render_callback' => array( $this, 'ssb_shortcode_block_callback' ),
'attributes' => array(
'theme' => array(
'type' => 'string',
'default' => 'simple-icons',
),
'counter' => array(
'type' => 'boolean',
'default' => false,
),
'showTotalCount' => array(
'type' => 'boolean',
'default' => false,
),
'align' => array(
'type' => 'string',
'default' => '',
),
'order' => array(
'type' => 'string',
'default' => 'fbshare,twitter,linkedin',
),
'likeButtonSize' => array(
'type' => 'string',
'default' => 'small',
),
'alignment' => array(
'type' => 'string',
'default' => 'left',
),
),
]
);
register_block_type(
'ssb/click-to-tweet',
[
'render_callback' => array( $this, 'ssb_click_to_tweet_block_callback' ),
'attributes' => array(
'theme' => array(
'type' => 'string',
'default' => 'twitter-round',
),
'tweet' => array(
'type' => 'string',
'default' => '',
),
'front' => array(
'type' => 'string',
'default' => '',
),
'IncludePageLink' => array(
'type' => 'boolean',
'default' => true,
),
'IncludeVia' => array(
'type' => 'boolean',
'default' => true,
),
'showTweetButton' => array(
'type' => 'boolean',
'default' => true,
),
'align' => array(
'type' => 'string',
'default' => '',
),
),
]
);
}
/**
* Call for ssb block
*
* @param array $attr register attributes.
* @since 3.0.0
* @return void
*/
public function ssb_shortcode_block_callback( $attr ) {
$themes = array(
'sm-round' => 'theme1',
'simple-round' => 'theme2',
'round-txt' => 'theme3',
'round-btm-border' => 'theme4',
'flat-button-border' => 'Flat',
'round-icon' => 'Circle',
'simple-icons' => 'Official',
);
if ( array_key_exists( $attr['theme'], $themes ) ) {
foreach ( $themes as $key => $value ) {
if ( $attr['theme'] == $key ) {
$theme = $themes[ $key ];
}
}
} else {
$theme = $this->selected_theme;
}
$attr['counter'] = $attr['counter'] ? 'true' : 'false';
$attr['showTotalCount'] = $attr['showTotalCount'] ? 'true' : 'false';
$theme = "theme='{$theme}'";
$order = "order='{$attr['order']}'";
$counter = "counter='" . $attr['counter'] . "'";
$alignemnt = "align='{$attr['alignment']}'";
$like_button_size = "like_button_size='{$attr['likeButtonSize']}'";
$show_total_count = "show_total_count='{$attr['showTotalCount']}'";
$align = esc_html( $attr['align'] );
return "<div class='align$align'> [SSB $theme $order $counter $alignemnt $like_button_size $show_total_count] </div>";
}
/**
* Call for click to tweet block.
*
* @param array $attr register attributes.
* @since 3.2.0
* @return void
*/
public function ssb_click_to_tweet_block_callback( $attr ) {
// var_dump( $theme );
$attr['IncludePageLink'] = ! $attr['IncludePageLink'] ? 'true' : 'false';
$attr['showTweetButton'] = ! $attr['showTweetButton'] ? 'true' : 'false';
$attr['IncludeVia'] = $attr['IncludeVia'] ? 'true' : 'false';
$theme = "theme='{$attr['theme']}'";
$front = "front='{$attr['front']}'";
$tweet = "tweet='{$attr['tweet']}'";
$hide_link = "hide_link='{$attr['IncludePageLink']}'";
$hide_button = "hide_button='{$attr['showTweetButton']}'";
$include_via = "include_via='{$attr['IncludeVia']}'";
$align = esc_html( $attr['align'] );
return "<div class='align$align'> [SBCTT $theme $front $tweet $hide_link $hide_button $include_via] </div>";
}
}
Code file location:
simple-social-buttons/simple-social-buttons/simple-social-buttons.php
Conclusion
Now that you’ve learned how to embed the Simple Social Buttons Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply