Notice: Uninitialized string offset: 0 in social sharing mu-plugin
I'm getting a Notice: Uninitialized string offset: 0 This is a mu-plugin that is supposed to show social sharing buttons and the only problem is happening with pinterest. Since it's not connected to my own database, I don't know why it's returning that error.
My full code:
?php
// Source: https://wpvkp.com/add-social-media-sharing-buttons-to-wordpress-without-plugin/
// Function to handle the thumbnail request
function get_the_post_thumbnail_src($img)
{
return (preg_match('~\bsrc=([^]++)~', $img, $matches)) ? $matches[1] : '';
}
function wpvkp_social_buttons($content) {
//global $wp;
$current_url = home_url(add_query_arg(array()
//, $wp-request
));
global $post;
if(is_singular() || is_home()){
// Get current page URL
$sb_url = urlencode(get_permalink());
// Get current page title
$sb_title = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$sb_thumb = get_the_post_thumbnail_src(get_the_post_thumbnail());
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$sb_title.'amp;url='.$sb_url.'amp;via=username';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$sb_url;
$whatsappURL = 'whatsapp://send?text='.$sb_title . ' ' . $sb_url;
$linkedInURL = 'https://www.linkedin.com/shareArticle?mini=trueurl='.$sb_url.'amp;title='.$sb_title;
//$instagramURL = 'http://instagram.com/sharer.php?u='. $sb_url . 'description='. $sb_title;
$redditURL = 'https://www.reddit.com/submit?url='. $sb_title . ' ' . $sb_url;
$telegramURL = 'https://telegram.me/share/url?url='. $sb_url . ' ' . $sb_title;
$messengerURL = 'fb-messenger://share/?link='. $sb_url . ' ' . $sb_title;
$maisURL = $current_url;
if(!empty($sb_thumb)) {
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.'amp;media='.$sb_thumb[0].'amp;description='.$sb_title;
}
else {
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.'amp;description='.$sb_title;
}
// Based on popular demand added Pinterest too
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.'amp;media='.$sb_thumb[0].'amp;description='.$sb_title;
// Add sharing button at the end of page/page content
$content .= 'div class=social-boxdiv class=social-btn';
$content .= 'a class=col-1 sbtn s-whatsapp href='. $whatsappURL .' target=_blank rel=nofollowspanWhatsApp/span/a';
$content .= 'a class=col-1 sbtn s-telegram href='. $telegramURL .' target=_blank rel=nofollowspanTelegram/span/a';
$content .= 'a class=col-1 sbtn s-facebook href='.$facebookURL .' target=_blank rel=nofollowspanFacebook/span/a';
$content .= 'a class=col-1 sbtn s-linkedin href='. $linkedInURL .' target=_blank rel=nofollowspanLinkedIn/span/a';
$content .= 'a class=col-1 sbtn s-pinterest href='. $pinterestURL .' data-pin-custom=true target=_blank rel=nofollowspanPinterest/span/a';
$content .= 'a class=col-1 sbtn s-twitter href='. $twitterURL .' target=_blank rel=nofollowspanTwitter/span/a';
$content .= 'a class=col-1 sbtn s-reddit href='.$redditURL.' target=_blank rel=nofollowspanReddit/span/a';
$content .= 'a class=col-1 sbtn s-messenger href='.$messengerURL.' target=_blank rel=nofollowspanMessenger/span/a';
$content .= '/div/div';
return $content;
}else{
// if not a post/page then don't include sharing button
return $content;
}
};
// Enable the_content if you want to automatically show social buttons below your post.
add_filter( 'the_content', 'wpvkp_social_buttons');
// This will create a wordpress shortcode [social].
// Please it in any widget and social buttons appear their.
// You will need to enabled shortcode execution in widgets.
add_shortcode('social','wpvkp_social_buttons');
add_action( 'plugins_loaded', 'wpvkp_social_buttons' );
function myplugin_muload_textdomain() {
load_muplugin_textdomain( 'mu-functions', 'lang' );
}
?
My attempt to fix the problem:
function the_post_thumbnail_src($img = 'thumbnail')
{
function get_the_post_thumbnail_src($img)
{
return (preg_match('~\bsrc=([^]++)~', $img, $matches)) ? $matches[1] : '';
}
}
but it threw another error. By the way, can anyone tell me why it's displaying in the pages? It is supposed to display only in the posts.