Point-1: it’s not clear where you are getting $order_id
from. I’m assuming it’s in the Shortcode
.
Point-2: You don’t need to. use ob_*
functions for returning Shortcode
replacement content, simply returning is enough.
Assuming other parts of your CODE is correct, below is the possible CODE that should work:
-
If the shortcode is implemented in theme’s
functions.php
file:/** * Add Shortcode */ function tracking_shortcode( $attrs ) { $attrs = shortcode_atts( array( 'order_id' => '' ), $attrs ); if( empty( $attrs['order_id'] ) ) return ''; $order = new WC_Order( $attrs['order_id'] ); // I've followed your code, but may be even the above line is not needed. // may be you can just use $attrs['order_id'] directly to get tracking code return get_post_meta( $order->id, '_shipping_tracking_code', true ); } add_shortcode( 'tracking_code', 'tracking_shortcode' );
-
If the shortcode is implemented using a plugin:
/** * Add Shortcode */ function tracking_shortcode( $attrs ) { $attrs = shortcode_atts( array( 'order_id' => '' ), $attrs ); if( empty( $attrs['order_id'] ) ) return ''; $order = new WC_Order( $attrs['order_id'] ); return get_post_meta( $order->id, '_shipping_tracking_code', true ); } /** * Initiate Shortcode */ function tracking_shortcode_init() { add_shortcode( 'tracking_code', 'tracking_shortcode' ); } add_action('init', 'tracking_shortcode_init');
Learn more about shortcode
from WordPress doc.