WP Associate Post R2 Shortcode

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

Before starting, here is an overview of the WP Associate Post R2 Plugin and the shortcodes it provides:

Plugin Icon
WP Associate Post R2

"WP Associate Post R2 is a versatile WordPress plugin designed to seamlessly link and associate related posts, enhancing user engagement and site navigation."

✩✩✩✩✩ () Active Installs: 4000+ Tested with: 6.2.3 PHP Version: 7.0
Included Shortcodes:
  • [WPAP_ID_ABBR]

WP Associate Post R2 [WPAP_ID_ABBR] Shortcode

The WPAP_ID_ABBR shortcode from the wp-associate-post-r2 plugin retrieves data from various services like Amazon, Rakuten, and Yahoo. Based on the ‘service’ attribute, it fetches the relevant data, caches it for future use, and formats it for display.

Shortcode: [WPAP_ID_ABBR]

Parameters

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

  • service – Specifies the service to use for the shortcode
  • id – Unique identifier for the service item
  • type – Type of the service item to display
  • title – Title to display for the service item
  • class – CSS class to apply to the shortcode output
  • search – Keyword to use for searching service items

Examples and Usage

Basic example – This example demonstrates how to use the shortcode with the ‘service’ and ‘id’ parameters. The ‘service’ parameter is used to specify the service, and the ‘id’ parameter is used to specify the ID of the item.

[WPAP_ID_ABBR service="amazon" id="12345" /]

Advanced examples

Displaying an item with a custom title and CSS class. The ‘title’ parameter is used to specify a custom title for the item, and the ‘class’ parameter is used to add a CSS class to the item’s container.

[WPAP_ID_ABBR service="amazon" id="12345" title="My Custom Title" class="my-custom-class" /]

Using the shortcode to search for an item. The ‘search’ parameter is used to specify a search term. The plugin will display the first item that matches the search term.

[WPAP_ID_ABBR service="amazon" search="My Search Term" /]

Using the shortcode to display an item from a different service. The ‘service’ parameter can be used to specify a different service. In this example, the plugin will display an item from Rakuten Ichiba.

[WPAP_ID_ABBR service="RakutenIchiba" id="67890" /]

PHP Function Code

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

Shortcode line:

add_shortcode( WPAP_ID_ABBR, array( $this, 'shortcode' ) );

Shortcode PHP function:

function shortcode( $atts = array() ) {
		$atts = shortcode_atts( array(
			'service' => 'with',
			'id'      => null,
			'type'    => null,
			'title'   => null,
			'class'   => null,
			'search'  => null,
		), $atts );

		if ( ! in_array( $atts['service'], $this->service, true ) || empty( $atts['id'] ) ) {
			return '';
		}

		$is_with = ( isset( $this->service['With'] ) && $atts['service'] === $this->service['With'] );
		if ( $is_with ) {
			$atts['service'] = 'amazon';
		}
		$is_amazon         = ( isset( $this->service['Amazon'] ) && $atts['service'] === $this->service['Amazon'] );
		$is_rakuten_ichiba = ( isset( $this->service['RakutenIchiba'] ) && $atts['service'] === $this->service['RakutenIchiba'] );
		$is_rakuten_books  = ( isset( $this->service['RakutenBooks'] ) && $atts['service'] === $this->service['RakutenBooks'] );

		$amazon_tracking_id = str_replace( '-22', '', $this->option['amazon_tracking_id'] );
		$cache_key = $is_amazon ? 'amazon_' . $amazon_tracking_id . '_' . $atts['id'] : $atts['service'] . '_' . $atts['id'];
		$cached_data = $this->cache->get( $cache_key );
		$data       = null;
		if ( false !== $cached_data ) {
			$data = $cached_data;
		} else {
			if ( $is_amazon ) {
				$data = $this->amazon->item_lookup( $atts['id'] );
			} elseif ( $is_rakuten_ichiba ) {
				$data = $this->rakuten->ichiba_item_lookup( $atts['id'] );
			} elseif ( $is_rakuten_books ) {
				$data = $this->rakuten->books_item_lookup( $atts['id'] );
			}

			if ( isset( $data ) && ! is_wp_error( $data ) ) {
				$this->cache->save( $cache_key, $data );
			} else {
				return '';
			}
		}

		$moshimo_amazon_enable = ! is_null( $this->moshimo ) && $this->moshimo->isset_aid( 'amazon' );
		if ( 'amazon' == $data['Service'] && $moshimo_amazon_enable ) {
			$data['URL'] = $this->moshimo->get_url( 'amazon', $this->moshimo->removal_get_parameters( $data['URL'] ) );
		}

		if ( $is_with ) {
			$with_rakuten_enable = isset( $this->service['RakutenIchiba'], $this->service['RakutenBooks'] );
			$with_yahoo_enable   = ! is_null( $this->yahoo );
			$with_search_keyword = ( is_null( $atts['search'] ) || '' === $atts['search'] ) ? $data['Title'] : $atts['search'];

			if ( $with_rakuten_enable ) {
			    $moshimo_rakuten_enable = ! is_null( $this->moshimo ) && $this->moshimo->isset_aid( 'rakuten' );
				if ( ! $moshimo_rakuten_enable && Amazon::is_books( $data['ProductGroup'] ) && isset( $data['ISBNJAN'] ) ) {
					$with_rakuten_data = $this->cache->get( $this->service['RakutenBooks'] . '_' . $data['ISBNJAN'] );
					if ( false === $with_rakuten_data ) {
						$with_rakuten_data = $this->rakuten->books_item_lookup( $data['ISBNJAN'] );
						if ( ! is_wp_error( $with_rakuten_data ) ) {
							$this->cache->save( $this->service['RakutenBooks'] . '_' . $data['ISBNJAN'], $with_rakuten_data );
						}
					}
				}
				if ( isset( $with_rakuten_data ) && ! is_wp_error( $with_rakuten_data ) ) {
					$data['RakutenType'] = 'books';
					$data['RakutenURL']  = $with_rakuten_data['URL'];
				} else {
					$data['RakutenType'] = 'ichiba';
					$data['RakutenURL']  = $this->rakuten->get_search_url( $with_search_keyword );
					$data['Search']      = $with_search_keyword;
					if ( $moshimo_rakuten_enable ) {
						$rakuten_search_url = $this->rakuten->get_search_url( $with_search_keyword, false );
						$data['RakutenURL'] = $this->moshimo->get_url( 'rakuten', $rakuten_search_url );
					}
				}
			}
			if ( $with_yahoo_enable ) {
				$data['YahooURL'] = $this->yahoo->get_search_url( $with_search_keyword );
				$data['Search']   = $with_search_keyword;
				if ( ! is_null( $this->moshimo ) && $this->moshimo->isset_aid( 'yahoo' ) ) {
					$yahoo_search_url = $this->yahoo->get_search_url( $with_search_keyword, false );
					$data['YahooURL'] = $this->moshimo->get_url( 'yahoo', $yahoo_search_url );
				}
			}
		}

		if ( ! is_null( $atts['title'] ) && '' !== $atts['title'] ) {
			$data['Title'] = $atts['title'];
		}
		if ( ! is_null( $atts['class'] ) && '' !== $atts['class'] ) {
			$data['Class'] = $atts['class'];
		} else {
			$data['Class'] = '';
		}

		if ( isset( $data['Service'], $data['ID'], $data['URL'] ) && 'amazon' == $data['Service'] && ! is_null( $this->bitly ) && ! $moshimo_amazon_enable ) {
			$url_cache = $this->cache->get( 'bitly_' . $data['Service'] . '_' . $amazon_tracking_id . '_' . $data['ID'] );
			if ( false !== $url_cache ) {
				$data['URL'] = $url_cache;
			} else {
				$shorten_result = $this->bitly->shorten( $data['URL'] );
				if ( ! is_wp_error( $shorten_result ) ) {
					$data['URL'] = $shorten_result;
					$this->cache->save( 'bitly_' . $data['Service'] . '_' . $amazon_tracking_id . '_' . $data['ID'], $shorten_result, '1 year' );
				}
			}
		}

		$formatted_data = $this->template->format( $data );
		return $this->template->output( $formatted_data, $atts['type'], $is_with );
	}

Code file location:

wp-associate-post-r2/wp-associate-post-r2/classes/class-main.php

Conclusion

Now that you’ve learned how to embed the WP Associate Post R2 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 *