Bitly's WordPress Shortcode

Below, you’ll find a detailed guide on how to add the Bitly’s WordPress Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Bitly’s WordPress Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Bitly’s WordPress Plugin and the shortcodes it provides:

Plugin Icon
Bitly's WordPress Plugin

"Bitly's WordPress Plugin is a versatile tool that seamlessly integrates with your WordPress site. It efficiently shortens your URLs using Bitly, enhancing link management and tracking. Perfect for efficient online branding."

★★★★✩ (23) Active Installs: 5000+ Tested with: 6.0.6 PHP Version: false
Included Shortcodes:
  • [wpbitly]

Bitly’s WordPress [wpbitly] Shortcode

The WP-Bitly shortcode is designed to generate a shortened URL for a specific post. It retrieves the post ID, applies default attributes, and uses the Bitly API to shorten the link.

Shortcode: [wpbitly]

Parameters

Here is a list of all possible wpbitly shortcode parameters and attributes:

  • text – Defines the hyperlink text for the shortlink
  • title – Sets the title attribute for the shortlink
  • before – Adds custom text before the shortlink
  • after – Adds custom text after the shortlink
  • post_id – Specifies the post ID for generating the shortlink

Examples and Usage

Basic example – The shortcode can be used to generate a Bitly shortlink for a specific post by referencing its post ID.

[wpbitly post_id=1 /]

Advanced examples

1. Using the shortcode to generate a Bitly shortlink with custom text. The shortlink will display the custom text instead of the default Bitly URL.

[wpbitly post_id=2 text="Click here for more information" /]

2. Using the shortcode to generate a Bitly shortlink with custom text and a custom title. The shortlink will display the custom text and the title attribute of the link will be the custom title.

[wpbitly post_id=3 text="Visit our homepage" title="Homepage" /]

3. Using the shortcode to generate a Bitly shortlink with custom text, a custom title, and custom text before and after the link. The shortlink will display the custom text with the custom before and after text surrounding it, and the title attribute of the link will be the custom title.

[wpbitly post_id=4 text="Read the full article" title="Full Article" before="<p>" after="</p>" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [wpbitly] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('wpbitly', array($this,'wpbitly_shortlink'));

Shortcode PHP function:

function wpbitly_shortlink($atts = array())
	{


	    $output = '';

	    $post = get_post();
	    $post_id = (is_object($post) && !empty($post->ID)) ? $post->ID : '';

	    $defaults = array(
	        'text' => '',
	        'title' => '',
	        'before' => '',
	        'after' => '',
	        'post_id' => $post_id
	    );

	    extract(shortcode_atts($defaults, $atts));
	    if (!$post_id) {
	        return $output;
	    }

	    $permalink = get_permalink($post_id);
	    $shortlink = $this->wpbitly_get_shortlink($permalink, $post_id, true);

	    if (empty($text)) {
	        $text = $shortlink;
	    }

	    if (empty($title)) {
	        $title = the_title_attribute(array(
	            'echo' => false
	        ));
	    }

	    if (!empty($shortlink)) {
	        $output = apply_filters('the_shortlink', sprintf('<a rel="shortlink" href="%s" title="%s">%s</a>', esc_url($shortlink), $title, $text), $shortlink, $text, $title);
	        $output = $before . $output . $after;
	    }

	    return $output;
	}

Code file location:

wp-bitly/wp-bitly/includes/class-wp-bitly-shortlink.php

Conclusion

Now that you’ve learned how to embed the Bitly’s WordPress Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *