How to Remove Mysterious White Space When URLEncoding get_the_excerpt()
I like to use my own social sharing links instead of the bulky buttons that the various social nets provide. So something like this, for example, for LinkedIn. Note that the various functions below are specific to WordPress.
<a class="icon icon-linkedin icon-replacement mobile-extraneous" href="http://www.linkedin.com/shareArticle?mini=true&url=<?php echo urlencode(get_permalink()); ?>%2F&title=<?php echo urlencode(get_the_title()); ?>&summary=<?php echo urlencode(get_the_excerpt()); ?>" target="_blank">Share on LinkedIn
Looking at the summary
part of the URL that LinkedIn uses, though:
&summary=<?php echo urlencode(get_the_excerpt()); ?>
It will create a ton of white space in your HTML. Like so:
In the words of the immortal Todd Snider, “Baby, you know that idn’t cool.”
Here’s how to fix it
Add this above the line of code near the beginning of this post.
<?php $modified_excerpt = urlencode(get_the_excerpt(), 0, 236);
$modified_excerpt = str_replace(' ', '', $modified_excerpt); ?>
So here we’re setting the excerpt up for success. We do the same thing, url encode it, but then we also remove all white space with the second line.
Then we’ll change up this bit of code:
&summary=<?php echo urlencode(get_the_excerpt()); ?>
With this:
&summary=<?php echo $modified_excerpt; ?>
There you go.
Up Next: How to Make Your Own Custom Post Formats in WordPress