Support » Plugin: Amazon Link » Why does this display the shortcode but not the original way?

  • Resolved g12mcgov

    (@g12mcgov)


    I currently have a custom template in which I’m extracting the shortcodes from the post and attempting to embed them in a custom post format.

    I’ve tried this:

    echo do_shortcode('[amazon asin=1442229012&template=iframe image]');

    And this will not display the widget. However, this will:

    echo apply_filters( 'the_content',' [amazon asin=1442229012&template=iframe image] ');

    This StackOverflow post (which is where I found the solution), said this is because it’s not a “true” shortcode. Any idea what this means?

    https://wordpress.org/plugins/amazon-link/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author paulstuttard

    (@paulstuttard)

    Hi,

    Hi the plugin does not us the shortcode API as it does not provide enough control needed for the plugin.

    For your purposes you might want to use the plugin’s ‘public’ API to process the shortcode:

    amazon_shortcode

    For example:

    <?php if (function_exists(amazon_shortcode)) echo amazon_shortcode("asin=1442229012&template=iframe image"); ?>

    This also stops the code producing errors if you uninstall the plugin.

    Hope this helps,

    Paul

    Thread Starter g12mcgov

    (@g12mcgov)

    Paul,

    This certainly helps. However, one last question…

    Is there anyway to extract the Amazon Link shortcode from a post’s content? I’ve attempted using the built in wordpress function get_shortcode_regex() along with doing some pattern matching, but to no avail and my guess it because it’s not “technically” using the shortcode API.

    Is there an easy way to extract it? I’d love to be able to extract it then do the below:

    echo amazon_shortcode($shortcode)

    Plugin Author paulstuttard

    (@paulstuttard)

    Hi,

    Not sure if it’s the most efficient method but you could use the plugin’s internal regex:

    $awlfw->get_regex()

    instead of get_shortcode_regex()?

    Paul

    Thread Starter g12mcgov

    (@g12mcgov)

    Well… this works, although I have no idea why/how. But you certainly got me in the right direction.

    $content = get_the_content();
    				if (function_exists(amazon_shortcode)) {
    					$pattern = $awlfw->get_regex('amazon');
    					// If our regex returns a match
    					if (preg_match($pattern, $content, $matches)) {
    						$shortcode = $matches[0];
    						echo apply_filters( 'the_content', $shortcode);
    						get_template_part( 'template-parts/content', 'books-store' );
    					}
    				}
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Why does this display the shortcode but not the original way?’ is closed to new replies.