Thirstyaffiliates Shortcode

Below, you’ll find a detailed guide on how to add the Thirstyaffiliates 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 Thirstyaffiliates Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Thirstyaffiliates Plugin and the shortcodes it provides:

Plugin Icon
ThirstyAffiliates – Affiliate Links, Link Branding, Link Tracking & Marketing Plugin

"ThirstyAffiliates is a comprehensive affiliate marketing tool, offering link branding, tracking, and marketing capabilities. This plugin simplifies the process of managing affiliate links, enhancing your marketing efforts."

★★★★☆ (222) Active Installs: 30000+ Tested with: 6.3.2 PHP Version: 5.6
Included Shortcodes:
  • [thirstylink]

Thirstyaffiliates [thirstylink] Shortcode

The ThirstyAffiliates shortcode enables the creation of affiliate links within WordPress posts. It accepts various attributes to customize the link’s appearance and behavior. It generates a unique link ID, retrieves the link URL, and sets the link text. It also allows for customization of ‘nofollow’ defaults, new window defaults, and CSS classes. If the link ID is invalid, an error message is displayed.

Shortcode: [thirstylink]

Parameters

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

  • ids – multiple affiliate link IDs, separated by commas
  • linkid – single affiliate link ID
  • linktext – text to be displayed for the affiliate link
  • class – CSS class to be added to the affiliate link
  • rel – rel attribute for the affiliate link, such as ‘nofollow’
  • target – target attribute for the affiliate link, such as ‘_blank’
  • title – title attribute for the affiliate link
  • href – URL for the affiliate link

Examples and Usage

Basic example – Displaying an affiliate link using its ID.

[thirstylink linkid=123 /]

Advanced examples

Displaying an affiliate link with custom link text and CSS class.

[thirstylink linkid=123 linktext="Buy Now" class="button"]

Using the shortcode to display an affiliate link with a nofollow relationship, opening in a new window, and a custom title.

[thirstylink linkid=123 rel="nofollow" target="_blank" title="Awesome Product"]

Displaying an affiliate link by referencing multiple IDs. The link will randomly select one of the IDs to display.

[thirstylink ids="123,456,789"]

PHP Function Code

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

Shortcode line:

add_shortcode( 'thirstylink' , array( $this , 'thirstylink_shortcode' ) );

Shortcode PHP function:

function thirstylink_shortcode( $atts , $content = '' ) {

        global $post;

        $post_id = is_a( $post , 'WP_Post' ) ? $post->ID : 0;

        $atts = shortcode_atts( array(
            'ids'       => '',
            'linkid'    => '',
            'linktext'  => '',
            'class'     => '',
            'rel'       => '',
            'target'    => '',
            'title'     => '',
            'href'      => ''
        ), $atts , 'thirstylink' );

        // get all link attributes from $atts
        $link_attributes = array_diff_assoc(
            $atts,
            array(
                'ids'       => $atts[ 'ids' ],
                'linkid'    => $atts[ 'linkid' ],
                'linktext'  => $atts[ 'linktext' ],
            )
        );

        // get the link ID
        if ( ! $atts[ 'linkid' ] ) {

            $ids     = isset( $atts[ 'ids' ] ) ? array_map( 'intval' , explode( ',' , $atts[ 'ids' ] ) ) : array();
            $key     = rand( 0 , count( $ids ) - 1 );
            $link_id = $ids[ $key ];
        } else
            $link_id = (int) $atts[ 'linkid' ];

        $output = '';

        if ( $link_id && get_post_type( $link_id ) == Plugin_Constants::AFFILIATE_LINKS_CPT ) {

            // load thirstylink
            $thirstylink  = new Affiliate_Link( $link_id );
            $uncloak_link = $thirstylink->is( 'uncloak_link' );

            $thirstylink_href = defined( 'ICL_SITEPRESS_VERSION' ) && ! empty( $atts['href'] ) ? $atts['href'] : $thirstylink->get_prop( 'permalink' );

            // get the link URL
            $link_attributes[ 'href' ] = ( $uncloak_link ) ? apply_filters( 'ta_uncloak_link_url' , $thirstylink->get_prop( 'destination_url' ) , $thirstylink ) : $thirstylink_href;

            if ( ! $uncloak_link )
                $link_attributes[ 'href' ] = $this->clean_url( $link_attributes[ 'href' ] );

            // get link text content default if no value is set
            if ( empty( $content ) && $atts[ 'linktext' ] )
                $content = $atts[ 'linktext' ]; // backward compatibility to get the link text content.
            else if ( empty( $content ) )
                $content = $thirstylink->get_prop( 'name' );

            // check for nofollow defaults if no value is set
            if ( empty( $link_attributes[ 'rel' ] ) ) {

                $link_attributes[ 'rel' ] = $thirstylink->is( 'no_follow' ) ? 'nofollow' : '';

                if ( $thirstylink->get_prop( 'rel_tags' ) )
                    $link_attributes[ 'rel' ] = trim( $link_attributes[ 'rel' ] . ' ' . $thirstylink->get_prop( 'rel_tags' ) );
            }

            // check for new window defaults if no value is set
            if ( empty( $link_attributes[ 'target' ] ) ) {

                $link_attributes[ 'target' ] = $thirstylink->is( 'new_window' ) ? '_blank' : '';
            }

            // provide default class value if it is not set
            if ( empty( $link_attributes[ 'class' ] ) ){
                $thirsty_link_class = strpos( $content , '<img ' ) !== false ? 'thirstylinkimg' : 'thirstylink';
                $link_attributes[ 'class' ] = get_option( 'ta_disable_thirsty_link_class' ) !== 'yes' ? $thirsty_link_class : '';

                if ( $thirstylink->get_prop( 'css_classes' ) ) 
                    $link_attributes[ 'class' ] = trim( $link_attributes[ 'class' ] . ' ' . $thirstylink->get_prop( 'css_classes' ) );
            }
                

            // provide default class value if it is not set
            if ( empty( $link_attributes[ 'title' ] ) && get_option( 'ta_disable_title_attribute' ) !== 'yes' )
                $link_attributes[ 'title' ] = $thirstylink->get_prop( 'name' );

            // remove double quote character on title attribute.
            $link_attributes[ 'title' ] = esc_attr( str_replace( '"' , '' , $link_attributes[ 'title' ] ) );

            // add data-link_id attribute if affiliate link is uncloaked.
            if ( $uncloak_link )
                $link_attributes[ 'data-linkid' ] = $link_id;

            // tag links as "nojs" to disable JS redirect for them.
            if ( get_option( 'ta_enable_javascript_frontend_redirect' ) == 'yes' )
                $link_attributes[ 'data-nojs' ] = apply_filters( 'ta_nojs_redirect_attribute' , false , $thirstylink );

            // allow the ability to add custom link attributes
            $link_attributes = apply_filters( 'ta_link_insert_extend_data_attributes' , $link_attributes , $thirstylink , $post_id );

            // Build the link ready for output
            $output .= '<a';

            foreach ( $link_attributes as $name => $value ) {
				// Handle square bracket escaping (used for some addons, eg. Google Analytics click tracking)
				$value   = html_entity_decode( $value );
				$value   = preg_replace( '/&#91;/' , '[' , $value );
				$value   = preg_replace( '/&#93;/' , ']' , $value );
				$output .= ! empty($value) ? ' ' . $name . '="' . trim( esc_attr( $value ) ) . '"' : '';
			}

			$output .= ' data-shortcode="true">' . do_shortcode( $content ) . '</a>';


        } elseif ( current_user_can( 'edit_published_posts' ) )
            $output .= '<span style="color: #0000ff;">' . __( 'SHORTCODE ERROR: ThirstyAffiliates did not detect a valid link id, please check your short code!' , 'thirstyaffiliates' ) . '</span>';
        else
            $output = $content;     

        return $output;
    }

Code file location:

thirstyaffiliates/thirstyaffiliates/Models/Shortcodes.php

Conclusion

Now that you’ve learned how to embed the Thirstyaffiliates 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 *