WordPressのthe_title()はurlencodeされていないのでget_the_title()をurlencodeしようねという話

photo by mkhmarketing

結果としてはこんな感じで…

「"」が「”」になっちゃうのはご愛嬌。

こんな感じでオレオレTwitterシェアボタンを作ってたんだけど、どうも良くない。「"」が含まれるタイトルがTwitterシェアのtextにセットされると動かないのだ。当たり前といえば当たり前。

<a class="twitter_btn" href="http://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink(); ?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');return false;"><img src="<? echo get_stylesheet_directory_uri(); ?>/images/BtnTwitter.png" width="64px" height="64px" alt="Twitter" /></a>

答え(早い)

echo urlencode(get_the_title());

つまりこうですね。

<a class="twitter_btn" href="http://twitter.com/share?text=<?php echo urlencode(get_the_title()); ?>&url=<?php the_permalink(); ?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');return false;"><img src="<? echo get_stylesheet_directory_uri(); ?>/images/BtnTwitter.png" width="64px" height="64px" alt="Twitter" /></a>

そんじゃまた。

WordPress 仕事の現場でサッと使える! デザイン教科書 (Webデザイナー養成講座)

WordPress 仕事の現場でサッと使える! デザイン教科書 (Webデザイナー養成講座)

© 2020 時藤屋 all rights reserved.