Title: wordpress custom shortcode not working
Last modified: January 28, 2017

---

# wordpress custom shortcode not working

 *  [anagam2016](https://wordpress.org/support/users/anagam2016/)
 * (@anagam2016)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/)
 * Hi I had created a custom billing field shipping tracking code to add shipping
   tracking number to the order in the backend and send this info by woocommerce
   order email to customer automatically returning this value as short code in html.
   But it doesnt work. What is wrong? Please help.
 *     ```
       /**
            * Add Shortcode 
            */
           function tracking_shortcode( ) {
              $order = new WC_Order( $order_id );
              $shipping_tracking_code = get_post_meta( $order->id, '_shipping_tracking_code', true );
              ob_start();
                 return $shipping_tracking_code;
              return ob_get_clean();
           }
           add_shortcode( 'tracking_code', 'tracking_shortcode' );
       ```
   

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/#post-8710439)
 * you have two `return`s there. that’s not right. I think you mean to use “echo”
   for the first one.
 *  Thread Starter [anagam2016](https://wordpress.org/support/users/anagam2016/)
 * (@anagam2016)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/#post-8710483)
 * Hi,
    Thanks for your response. I tried it, it doesn’t work anyway I am trying
   to get this displayed with these three options. In all cases no result
 * 1:<?php _e( do_shortcode( ‘[tracking_code]’ ) ); ?>
    2:[tracking_code] 3:<?php
   echo $shipping_tracking_code; ?>
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/#post-8710499)
 * I’d write it as
 *     ```
       function tracking_shortcode( ) {
              $order = new WC_Order( $order_id );
              $shipping_tracking_code = get_post_meta( $order->id, '_shipping_tracking_code', true );
              return "Your shipping code is: " . $shipping_tracking_code;
           }
           add_shortcode( 'tracking_code', 'tracking_shortcode' );
       ```
   
 * Does whatever you’re using to send the email handle shortcodes?
 *  Thread Starter [anagam2016](https://wordpress.org/support/users/anagam2016/)
 * (@anagam2016)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/#post-8710536)
 * I tried this.
    Now it returns only ‘Your shipping code is:, The value of $shipping_tracking_code
   is not returned. Btw i m trying to get this in woocommerce email
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/#post-8710554)
 * > The value of $shipping_tracking_code is not returned.
 * So that indicates that there’s something wrong with your call to get_post_meta.
   That’s where you need to focus your attention or on the previous call, which 
   may not be returning anything useful in $order.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/#post-8710573)
 * Email content does not get echoed, it is compiled into a variable and included
   in wp_mail() parameters. Any solution that uses a variant of echo will fail. 
   What you need to do is add your content to that variable that contains the other
   mail content. I’m pretty sure Woocommerce provides a filter for this, but I’m
   not certain about what it’s called.
 * It can be found by digging through WC code, which can be a frustrating, though
   informative process. If you ask at the dedicated [WC support forum](https://wordpress.org/support/plugin/woocommerce)
   someone should be able to tell you.
 * BTW, I know you were simply fishing for a solution, but your use of the _e() 
   translation function is so wrong in so many ways it makes me cringe 🙂 I mention
   this not to be critical, but for the sake of anyone else reading who doesn’t 
   know better.
 *  Thread Starter [anagam2016](https://wordpress.org/support/users/anagam2016/)
 * (@anagam2016)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/#post-8710574)
 *     ```
       <?php
       /**
        * Add Promo code text to the Order emails
        */
   
       add_action( 'woocommerce_email_before_order_table', 'add_tracking_code', 20);
   
       function add_tracking_code( $order, $is_admin_email ) {
                 $shipping_tracking_code = get_post_meta( $order->id, '_shipping_tracking_code', true );
                 $url = 'http://www.rapido.bg/information/tracking-tovaritelnitsa';
                 $order = new WC_Order( $order_id );
                 if($order->status === 'shipped' ) {	  
                 ob_start(); 
       	  ?>
       <h3 style="text-align: center;"><?php _e( 'Your order tracking no', 'my-plugin' ); ?></h3>
       <div>
       <p style="text-align: justify;"><?php _e( 'Your order has been shipped from our warehouse thru courier service Rapido. Waybill no is: &nbsp; ', 'my-plugin' ); ?><font size="3" color="red"><b><?php echo $shipping_tracking_code; ?></b></font></p>
       <p><a href="<?php echo $url; ?>" ><?php _e( 'Track your shipment', 'my-plugin' ); ?></a></p>
       </div>
       	  <?php
       	  echo ob_get_clean();  
                 } else {
                 echo '&nbsp;';
                 }
       }
   
       ?>
       ```
   
 * Above is the snippet which works well in customer note email
 * Can’t understand why it doesn’t work here, I mean cannot get the valipue of $
   shipping_tracking_code
 *  Thread Starter [anagam2016](https://wordpress.org/support/users/anagam2016/)
 * (@anagam2016)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/#post-8710589)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/)
    Hi, thanks, will do
   the home work. Sorry, I am just a beginner with no practical knowledge of coding.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘wordpress custom shortcode not working’ is closed to new replies.

## Tags

 * [custom shortcode](https://wordpress.org/support/topic-tag/custom-shortcode/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 8 replies
 * 3 participants
 * Last reply from: [anagam2016](https://wordpress.org/support/users/anagam2016/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/wordpress-custom-shortcode-not-working/#post-8710589)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
