If you have ever wondered how you could create custom listing shortcodes to be used in your WP-Lister listing templates then read on… it’s easier than you might think.

Where to start

Since WP-Lister 1.2 each listing template has its own functions.php file – which is different from your WordPress theme’s functions.php. Both work in a similar way, but your theme’s functions.php affects your whole WP site while a listing template’s functions.php affects only your eBay listings and preview.

Why not use your theme’s functions.php? For two good reasons: First custom listing shortcodes should be bundled with the listing template where they are used, so when you duplicate, download or upload listing templates, the required code will always remain attached. Second, if you make a mistake or typo, you won’t shut down your entire site – only the listing preview won’t work.

Simple example: Hello World

To get started, open up your listing templates functions.php (from now on referred just as functions.php). You can use your favorite code editor to edit the file via FTP – but you can just edit the template and scroll down until you see the current functions.php content.

You will find some example code – which is inactive since it’s wrapped in a /* ... */ comment block:

function my_tpl_process_html( $html, $item, $images ) {
  $html = str_replace( '[[my_custom_listing_shortcode]]', 'MY CUSTOM CONTENT', $html );
  return $html;
add_filter( 'wplister_process_template_html', 'my_tpl_process_html', 10, 3 );

Remove the comment wrappers to activate the code and change the shortcode and content to whatever you like. A simple hello world example would look like this:

function my_tpl_process_html( $html, $item, $images ) {
  $html = str_replace( '[[hello_world]]', 'Hello World!', $html );
  return $html;
add_filter( 'wplister_process_template_html', 'my_tpl_process_html', 10, 3 );

Save your template and use [[hello_world]] somewhere in your template. You will see that it gets replaced with “Hello World!”.

However, you wouldn’t want just to replace some text – that doesn’t make much sense as you could write “Hello World!” right in your template.

Usually, you want to fetch some product details from the database, check if it’s empty and if not, prefix it with some label:

Real world example: Special Price

The code below will fetch the product meta field _sale_price, format it using woocommerce_price() and prefix it with “Sale Price: ” – unless it’s empty:

function my_tpl_process_sale_price( $html, $item, $images ) {
  $sale_price = get_post_meta( $item['post_id'], '_sale_price', true );
  $sale_price_html = '';
  if ( $sale_price ) {
    $sale_price_html = 'Sale Price: '.woocommerce_price( $sale_price );
  $html = str_replace( '[[sale_price]]', $sale_price_html, $html );
  return $html;
add_filter( 'wplister_process_template_html', 'my_tpl_process_sale_price', 10, 3 );

Now you can use  [[_sale_price]] anywhere in your template – which will be replaced by the formatted and labeled value, or removed if there is no sale price.

Complex example: Product Reviews

As an example for a more complex shortcode, let’s look at the following code snippet, which shows the latest product reviews in your eBay listing:

function my_tpl_process_product_reviews( $html, $item, $images ) {

    // get all approved comments
    $comments = get_comments( array( 'post_id' => $item['post_id'], 'status' => 'approve' ) );
    if ( empty( $comments ) ) return $html;

    // build reviews html
    $reviews_html  = '<h3>Reviews</h3>';
    $reviews_html .= '<ul class="product_reviews">';
    foreach ($comments as $comment) {
        $reviews_html .= '<li>';
        $reviews_html .= $comment->comment_content;
        // show author and date - remove the next line if you like
        $reviews_html .= '<br><small><i>' . $comment->comment_author . ' on ' . date(get_option('date_format'), strtotime($comment->comment_date) ) . '</small></i>';
        $reviews_html .= '</li>';
    $reviews_html .= '</ul>';

    // replace shortcode with generated html
    $html = str_replace( '[[product_reviews]]', $reviews_html, $html );
add_filter( 'wplister_process_template_html', 'my_tpl_process_product_reviews', 10, 3 );

As you can see, you can just use any WordPress function as you would do in a plugin or any other code snippet. With a a few lines of PHP you can create custom listing shortcodes for almost anything.

Let me know in the comments if you liked this post – or what you would like me to blog about next. 😉