WordPress.org

Forums

WP Bitly
[resolved] Getting just the url (3 posts)

  1. alevalentini
    Member
    Posted 1 year ago #

    the wpbitly_shortlink(); function returns a whole <a href...>...</a> element. What if I need just the shortened url?

    https://wordpress.org/plugins/wp-bitly/

  2. Mark Waterous
    Member
    Plugin Author

    Posted 1 year ago #

    You have two options depending on what you want to do, and how you're going about doing it.

    The less likely but still available first option is to filter the_shortlink; there are four arguments passed in total, including the formatted link, the shortlink itself, the text (for the formatted link) and the title of the page the shortlink references. You could easily hook into this and return just the $shortlink (the second argument).

    The other way is to call wpbitly_get_shortlink() directly. Argument one should be the current permalink (though you can pass this as empty so long as you get argument two correct), and the second argument is the post ID you want the shortlink for. For example;

    <?php
    /* this will return the shortlink for a post/page with the ID of 12 */
    echo wpbitly_get_shortlink('', 12);
    
    /* You can also pass it the permalink in case something fails retrieving or generating the shortlink */
    $post_id = get_the_ID();
    $permalink = get_permalink($post_id);
    $shortlink = wpbitly_get_shortlink($permalink, $post_id);
    ?>
  3. alevalentini
    Member
    Posted 1 year ago #

    Perfect. That made the trick. Thank you! Great plugin!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP Bitly
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic