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.

Topic mu-plugins mysql php plugins Wordpress

Category Web


Your custom get_the_post_thumbnail_src() function returns a string and not an array, hence in the following code, $sb_thumb is not an array:

$sb_thumb = get_the_post_thumbnail_src(get_the_post_thumbnail());

So the PHP notice in question occurred because you used $sb_thumb[0] in the following code, which appears twice in your wpvkp_social_buttons() function:

$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.'&media='.$sb_thumb[0].'&description='.$sb_title;

And to get rid of the notice, you just need to replace the above $sb_thumb[0] with $sb_thumb.


However, there is actually a WordPress function that you can (and I would) use to get the featured image URL — get_the_post_thumbnail_url().

So for example, you could simply do like so to get the URL that your get_the_post_thumbnail_src() function would return:

$sb_thumb = get_the_post_thumbnail_url();

And remember, get_the_post_thumbnail_url() returns a URL (i.e. a string) or false if no image is available.


why it's displaying in the pages? It is supposed to display only in the posts.

In your code, you used is_singular() without specifying the first (and only) parameter ($post_types), and if you look at the documentation, it stated that:

If the $post_types parameter is specified, this function will additionally check if the query is for one of the Posts Types specified.

So if you want the social sharing buttons to show only on single post pages, i.e. only if the post type is post, then you would use is_singular( 'post' ) and not just is_singular(), like so:

// Replace this:
if(is_singular() || is_home()){

// with this:
if(is_singular( 'post' ) || is_home()){

Additional Notes

  • You should also encode the media value in the Pinterest URL — urlencode( $sb_thumb ).

  • What is the $maisURL = $current_url; for? Maybe you should just remove those variables?

  • You made a typo here: add_action( 'plugins_loaded', 'wpvkp_social_buttons' ); — the second parameter should be myplugin_muload_textdomain.

    But I supposed that's just a typo in the question?


Update

In response to your comments:

I'm getting a "Fatal error: Cannot redeclare get_the_post_thumbnail_url()

I couldn't implement these: urlencode( $sb_thumb ) get_the_post_thumbnail_url()

As for the fatal error, I don't know what you did, but as I said in my comment, WordPress already defined the function (which means it's a core function) and it's loaded in both wp-admin and front-end/non-admin side of the site, so you don't need to manually load the function and do not create a global function with the same name.

And as for the implementation:

  • Step 1:

    // Replace this:
    $sb_thumb = get_the_post_thumbnail_src(get_the_post_thumbnail());
    
    // With this:
    $sb_thumb = get_the_post_thumbnail_url();
    
  • Step 2:

    Replace this part:

    if(!empty($sb_thumb)) {
        $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.'&media='.$sb_thumb[0].'&description='.$sb_title;
    }
    else {
        $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.'&description='.$sb_title;
    }
    
    // Based on popular demand added Pinterest too
    $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.'&media='.$sb_thumb[0].'&description='.$sb_title;
    

    with this:

    if(!empty($sb_thumb)) {
        $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url. // wrapped for brevity
            '&media='.urlencode( $sb_thumb ).'&description='.$sb_title;
    }
    else {
        $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.'&description='.$sb_title;
    }
    

    or a simpler one:

    $media        = $sb_thumb ? '&media=' . urlencode( $sb_thumb ) : '';
    $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.$media.'&description='.$sb_title;
    

So just make the above replacements in your code and that's all. :)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.