Notice: Uninitialized string offset: 0 in social sharing mu-plugin

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. 🙂