注意:社交分享Mu-plugin中未初始化的字符串偏移量:0

时间:2021-08-04 作者:Brave Warrior

我收到一个通知:未初始化的字符串偏移量:0这是一个mu插件,应该显示社交共享按钮,唯一的问题是pinterest。由于它没有连接到我自己的数据库,我不知道为什么它会返回那个错误。

我的完整代码:

<?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=true&url=\'.$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-box"><div class="social-btn">\';
        
        $content .= \'<a class="col-1 sbtn s-whatsapp" href="\'. $whatsappURL .\'" target="_blank" rel="nofollow"><span>WhatsApp</span></a>\';
        $content .= \'<a class="col-1 sbtn s-telegram" href="\'. $telegramURL .\'" target="_blank" rel="nofollow"><span>Telegram</span></a>\';
        $content .= \'<a class="col-1 sbtn s-facebook" href="\'.$facebookURL .\'" target="_blank" rel="nofollow"><span>Facebook</span></a>\';
        
        $content .= \'<a class="col-1 sbtn s-linkedin" href="\'. $linkedInURL .\'" target="_blank" rel="nofollow"><span>LinkedIn</span></a>\';
        $content .= \'<a class="col-1 sbtn s-pinterest" href="\'. $pinterestURL .\'" data-pin-custom="true" target="_blank" rel="nofollow"><span>Pinterest</span></a>\';
        $content .= \'<a class="col-1 sbtn s-twitter" href="\'. $twitterURL .\'" target="_blank" rel="nofollow"><span>Twitter</span></a>\'; 
        $content .= \'<a class="col-1 sbtn s-reddit" href="\'.$redditURL.\'" target="_blank" rel="nofollow"><span>Reddit</span></a>\';
        $content .= \'<a class="col-1 sbtn s-messenger" href="\'.$messengerURL.\'" target="_blank" rel="nofollow"><span>Messenger</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\' );
}



?>
我尝试修复此问题:

function the_post_thumbnail_src($img = \'thumbnail\')
 {
function get_the_post_thumbnail_src($img)
{
  return (preg_match(\'~\\bsrc="([^"]++)"~\', $img, $matches)) ? $matches[1] : \'\';
}
}
但它又犯了一个错误。顺便问一下,有人能告诉我为什么它会显示在页面上吗?它应该只显示在立柱上。

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

您的自定义get_the_post_thumbnail_src() 作用returns a string and not an array, 因此,在以下代码中,$sb_thumb 不是数组:

$sb_thumb = get_the_post_thumbnail_src(get_the_post_thumbnail());
因此,出现问题的PHP通知是因为您使用了$sb_thumb[0] 在以下代码中wpvkp_social_buttons() 功能:

$pinterestURL = \'https://pinterest.com/pin/create/button/?url=\'.$sb_url.\'&amp;media=\'.$sb_thumb[0].\'&amp;description=\'.$sb_title;
要删除该通知,只需替换上面的$sb_thumb[0] 具有$sb_thumb.

然而,实际上有一个WordPress函数,你可以(我也会)使用它来获取特色图片的URL-get_the_post_thumbnail_url().

例如,您可以简单地这样做,以获取您的get_the_post_thumbnail_src() 函数将返回:

$sb_thumb = get_the_post_thumbnail_url();
记住,get_the_post_thumbnail_url() 返回URL(即字符串),如果没有可用图像,则返回false。

为什么会显示在页面中?它应该只显示在柱子上。

在代码中,您使用is_singular() 不指定第一个(也是唯一一个)参数($post_types), 如果你看看documentation, 它指出:

如果指定了$post\\U types参数,此函数将另外检查查询是否针对指定的post类型之一。

因此,如果您希望社交共享按钮仅显示在单个帖子页面上,即仅当帖子类型为post, 然后你会使用is_singular( \'post\' ) 而不是仅仅is_singular(), 像这样:

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

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

Additional Notes

<您还应该对media Pinterest URL中的值-urlencode( $sb_thumb ).

什么是$maisURL = $current_url; 对于也许你应该删除这些变量?

您在此处输入了错误:add_action( \'plugins_loaded\', \'wpvkp_social_buttons\' ); — 第二个参数应为myplugin_muload_textdomain.

但我想这只是问题中的一个拼写错误?

根据您的评论更新:

我得到一个;致命错误:无法重新声明get\\u the\\u post\\u thumbnail\\u url()

我无法实现这些:urlencode( $sb_thumb )get_the_post_thumbnail_url()

至于致命错误,我不知道你做了什么,但正如我在评论中所说的,WordPress已经定义了这个函数(这意味着它是一个核心函数),并且它在两个wp-admin 和站点的前端/非管理端,因此您不需要手动加载函数,也不需要创建同名的全局函数。

至于实施:

  • 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:

    更换此部件:

    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;
    
    使用此选项:

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

    $media        = $sb_thumb ? \'&amp;media=\' . urlencode( $sb_thumb ) : \'\';
    $pinterestURL = \'https://pinterest.com/pin/create/button/?url=\'.$sb_url.$media.\'&amp;description=\'.$sb_title;
    
因此,只需在代码中进行上述替换即可。:)