1

I'm trying to add a custom "Tweet this" button to my WordPress posts. I'm sticking to the official Twitter guidelines.

Here is what I'm actually trying to pull of; I don't want the button, I want a custom look - for me it's only text. This is the code I use:

<a href="https://twitter.com/share?url=<?php echo urlencode(get_permalink($post->ID)); ?> &text=<?php the_title(); ?> &via=username&count=horizontal" class="custom-tweet-button">Tweet</a>

Output:

This is the post title via @username

So the problem is that the permalink (<?php the_permalink(); ?>)doesn't get displayed.

I tried:

  • https://twitter.com/share?url=<?php echo urlencode(get_permalink($post->ID)); ?>
  • https://twitter.com/share?url=<?php the_permalink(); ?>

PS: I also tried the standard twitter button, not the custom one, but the permalink doesn't get displayed either.

Does anyone one if something is wrong with my code or if this is a problem since I'm using a local WordPress for development?

0

After sleeping over the problem I decided to the code and tried this:

<a href="https://twitter.com/share?url=&text=<?php the_title(); ?>: <?php echo urlencode(get_permalink($post->ID)); ?> &via=username&count=horizontal" class="custom-tweet-button">Tweet</a>

All I can say is: it works as it should be. Next thing on my agenda is to incorporate a custom url shortener.

PS: It's worth a mention that if you use certain themes, they might use a special %permalink% and %post-title% structure. Be sure to replace the default <?php the_title(); ?> and <?php the_permalink(); ?> with it.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.