php - Facebook share error -
i'm trying create facebook share button in each of post, , share content dynamic, mean able customize thumbnail, title , description each of post.
below code use(i'm using advance custom field plugin in wordpress way):
<a onclick="window.open('http://www.facebook.com/sharer.php?s=100&p[title]=<?php the_field(videotitle); ?>&p[summary]=<?php the_field(video_description); ?>&p[url]=<?php echo get_permalink(); ?>&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 php echo out content cms:
<?php the_field(videotitle); ?> <?php the_field(video_description); ?> <?php echo get_permalink(); ?> the code works fine, noticed when enter the title/description long or use special characters in post button stop working.
how should overcome this? i'm still new php, please explain in layman's term if possible , thank in advance.
the problem caused passing in unescaped special characters direct javascript call.
right now, have following javascript executing when link clicked:
window.open('http://www.facebook.com/sharer.php?s=100&p[title]=<?php the_field(videotitle); ?>&p[summary]=<?php the_field(video_description); ?>&p[url]=<?php echo get_permalink(); ?>&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 passing in several php variables, may alter format of javascript. example, let's the_field(videotitle); returns maria's video. if note, string has quote in due maria's.
now, if pass title javascript, you're going have un-escaped quote, causing js error, because output this:
... [title]=maria's video ... to address this, must format out php output ensure not affect js code. in example, can encode outputted strings using urlencode function included php, this:
<?php urlencode(get_the_field(videotitle)); ?> just remember passing php variables javascript can alter syntax of javascript function. if final javascript function contains syntax errors caused php output, not run.
you can see javascript errors on page debugging hitting f12 in browser , viewing console tab.
Comments
Post a Comment