duanpa1980 2013-10-07 16:52
浏览 63
已采纳

Facebook分享错误

I'm trying to create a facebook share button in each of my post, and the share content will be dynamic, which mean I will be able to customize its thumbnail, title and description for each of the post.

below is the code that I use(I'm using advance custom field plugin in wordpress by the way):

<a onClick="window.open('http://www.facebook.com/sharer.php?s=100&amp;p[title]=<?php the_field(videotitle); ?>&amp;p[summary]=<?php the_field(video_description); ?>&amp;p[url]=<?php echo get_permalink(); ?>&amp;p[images][0]=http://img.youtube.com/vi/<?php the_field(youtube_thumb); ?>/maxresdefault.jpg','sharer','toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)" rel="nofollow"></a>

///////////////////////////////////////////////////////////////////////////////////////////////////////

Below is the php that will echo out my content from my CMS:

<?php the_field(videotitle); ?>
<?php the_field(video_description); ?>
<?php echo get_permalink(); ?>

The code works fine, but I noticed when I enter the the title/description too long or use special characters in my post the button stop working.

How should I overcome this? I'm still very new to php, please explain in layman's term if possible and thank you in advance.

  • 写回答

1条回答 默认 最新

  • duanpiyao2734 2013-10-07 17:35
    关注

    The problem is most likely caused by passing in unescaped special characters into a direct javascript call.

    Right now, you have the following javascript executing when the link is clicked:

    window.open('http://www.facebook.com/sharer.php?s=100&amp;p[title]=<?php the_field(videotitle); ?>&amp;p[summary]=<?php the_field(video_description); ?>&amp;p[url]=<?php echo get_permalink(); ?>&amp;p[images][0]=http://img.youtube.com/vi/<?php the_field(youtube_thumb); ?>/maxresdefault.jpg','sharer','toolbar=0,status=0,width=548,height=325');
    

    You are passing in several PHP variables, which may alter the format of your javascript. For example, let's say the_field(videotitle); returns Maria's Video. If you note, your string has a quote in it due to Maria's.

    Now, you if pass this title into your javascript, you're going to have an un-escaped quote, causing a JS error, because it will output like this:

    ... [title]=Maria's Video ...
    

    To address this, you must format out PHP output to ensure that it will not affect the JS code. In my example, you can encode the outputted strings using the urlencode function included with PHP, like this:

    <?php urlencode(get_the_field(videotitle)); ?>
    

    Just remember that passing PHP variables into javascript CAN alter the syntax of your javascript function. If the final javascript function contains syntax errors caused by the PHP output, it will not run.

    You can see the javascript errors on the page you are debugging by hitting F12 in your browser and viewing the Console tab.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?