Jake Bohall
Jake Bohall

Reputation: 1

Replace Shortcodes with HTML in Wordpress

I'm working on a WordPress site that has been using a plugin to grab amazon product images using a shortcode.

You simply insert the asin of a book into a shortcode, like this:

[amazon template=image&asin=B00FYY53B8]

When the post loads, the shortcode is converted into the actual image HTML using the URL from amazon.... so the example above would return

http://ecx.images-amazon.com/images/I/71kIifYTccL._SL1500_.jpg

I'm using another custom plugin to build out the content for posts, and so far am just using this shortcode as part of the post content. I would like to do some other things, such as set the featured image, etc. and need that URL.

I've tried

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

But it doesn't return anything. Is there a way to "execute" shortcodes in a plugin and save the output?

Ultimately, I would also like to scale this functionality so I can replace other/old shortcodes with their HTML output, and save it back to the post, essentially eliminating the use of some shortcodes in posts.

Upvotes: 0

Views: 3763

Answers (2)

user11720680
user11720680

Reputation:

You can include this code in functions.php. And call this function in content-singular.php or the similar file in your theme.

 function updatesc_database( $post ) {
 if ( empty($post) ) global $post;
 if ( empty($post) || ! isset($post->post_content) ) return false;
 $content = $post->post_content;
 $shortc1="amazon template=image&asin";
 $shortc2="B00FYY53B8]"; //insert the number variable here
 $shortwh=$shortc1.$shortc2;
 $replace = do_shortcode($shortwh);
 $content = str_replace( $shortwh, $replace, $content );   
 return $content;
 }

Upvotes: 0

HoangHieu
HoangHieu

Reputation: 2840

I have a demo for add a short code in wordpress. I hope it help.

add_action('plugins_loaded','MOD_load');

function MOD_load() {
     short_enable_form();
}

function short_enable_form(){
    if(!function_exists('add_shortcode')) {
                   return;
    }
    add_shortcode('form_mod' , array(&$this, 'out_put_form'));
}

public function out_put_form(){         
    return 'HTML TEXT';
}

Upvotes: 0

Related Questions