I am looking to use Google Link shortener with WordPress. I want to use shorter links to help users post more in social media, bitly is far to expensive. Google gives a massive allowance, which want to use. API key is available but need to implement using php prior WordPress rendering the page.
Current Code
<a rel="nofollow" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php echo urlencode(get_the_title($id)); ?>" title="Share this post on Facebook">
<img src="<?php echo get_template_directory_uri(); ?>/images/facebook.png" border="0" alt="Facebook"/>
</a>
<a rel="nofollow" href="http://twitter.com/home?status=<?php echo urlencode("Currently reading: "); ?><?php the_permalink(); ?>" title="Share this article with your Twitter followers">
<img src="<?php echo get_template_directory_uri(); ?>/images//twitter.png" border="0" alt="Twitter"/>
</a>
<a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" title="Share this on Google+">
<img src="<?php echo get_template_directory_uri(); ?>/images/google_plusone_share.png" border="0" alt="Google+"/>
</a>
To summarise I need a example of how to enforce Google link shortener API while using the in built functions such as:
the_permalink();
Many thanks.
You dont want to hook into
the_permalink
, else every internal page link that uses it will be routed via google, which is obviously a bad idea.Instead create a new function to return the shortened link:
then use in your templates are required:
You’ll need to use
add_filter('the_permalink','your_custom_function');
to customize the output of the permalink.Rough outline of
your_custom_function
:Also see Steve’s Answer why you don’t want to implement it through
the_permalink
Use this class to talk with the google api and the goo.gl shortener service:
Here is a short function to return the goo.gl link