Title: php function in add_shortcode
Last modified: August 25, 2020

---

# php function in add_shortcode

 *  Resolved [dirkidv](https://wordpress.org/support/users/dirkidv/)
 * (@dirkidv)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-function-in-add_shortcode/)
 * I would like to make a shortcode to display a trackinglist of an album. This 
   data is stored in meta key “_product_attributes”. The PHP snippet works when 
   I test it in my functions.php, but I don’t find the right way to make it work
   as a shortcode, to be used in Gutenberg blocks, shortcode blocks…. Anybody knows
   how I can make it as a registered add_shortcode()? Here under is my PHP snippet,
   that works:
 *     ```
       add_action( 'woocommerce_single_product_summary', 'display_tracklist', 20 );
       function display_tracklist(){
           global $product;
   
           // Get the tracklist field value
           $custom_field = get_post_meta( $product->get_id(), '_product_attributes', false );
           // Display
           if( ! empty($custom_field) ){
               $tracks = $custom_field[0]['Tracklisting']['value'];
               echo '<span class="track">' . nl2br($tracks) . '</span><br>';
           }
       }
       ```
   
    -  This topic was modified 5 years, 9 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).

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

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-function-in-add_shortcode/#post-13312079)
 * What is the problem?
 * Shortcodes should not echo. Tey return a string. So change yours to “return” 
   instead of “echo”.
 *  Thread Starter [dirkidv](https://wordpress.org/support/users/dirkidv/)
 * (@dirkidv)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-function-in-add_shortcode/#post-13312564)
 * “The problem is that my website crashes if I make something as this even with“
   return”:
 * add_shortcode( ‘ps_tracklisting’, ‘display_tracklist’ );
    function display_tracklist(){
   global $product;
 *  // Get the tracklist field value
    $custom_field = get_post_meta( $product->get_id(),‘
   _product_attributes’, false ); // Display if( ! empty($custom_field) ){ $tracks
   = $custom_field[0][‘Tracklisting’][‘value’]; } return ‘<span class=”track”>’ .
   nl2br($tracks) . ‘</span><br>’; }
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-function-in-add_shortcode/#post-13312584)
 * Define “crash”? Are you getting a PHP error?
 * Enable wp_debug and wp_debug_log and after an error, look at wp-content/debug.
   log to see if anything gets logged there. [https://wordpress.org/support/article/debugging-in-wordpress/](https://wordpress.org/support/article/debugging-in-wordpress/)
 *  Thread Starter [dirkidv](https://wordpress.org/support/users/dirkidv/)
 * (@dirkidv)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-function-in-add_shortcode/#post-13312649)
 * Thank you, Steven!
    I also kept the (test-)PHP-snippet in my functions.php, so
   the error mentioned it was declared twice. Sorry for my inconvenience, I will
   keep enabling debug-mode througout this development. Have a nice evening!
 *  Thread Starter [dirkidv](https://wordpress.org/support/users/dirkidv/)
 * (@dirkidv)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-function-in-add_shortcode/#post-13312691)
 * But the shortcode doesn’t do what it should do: this is the debug.log:
 * PHP Fatal error: Uncaught Error: Call to a member function get_id() on null in/
   Users/BigBalloo/www/wordpress.test/wp-content/themes/konte-child/functions.php:
   254
    Stack trace: #0 /Users/BigBalloo/www/wordpress.test/wp-includes/shortcodes.
   php(343): display_tracklist(”, ”, ‘ps_tracklisting’) #1 [internal function]: 
   do_shortcode_tag(Array) #2 /Users/BigBalloo/www/wordpress.test/wp-includes/shortcodes.
   php(218): preg_replace_callback(‘/\\[(\\[?)(wpv\\-w…’, ‘do_shortcode_ta…’, ‘\
   n<div class=”wp…’) #3 /Users/BigBalloo/www/wordpress.test/wp-includes/class-wp-
   hook.php(287): do_shortcode(‘\n<div class=”wp…’) #4 /Users/BigBalloo/www/wordpress.
   test/wp-includes/plugin.php(206): WP_Hook->apply_filters(‘\n<div class=”wp…’,
   Array) #5 /Users/BigBalloo/www/wordpress.test/wp-includes/rest-api/endpoints/
   class-wp-rest-posts-controller.php(1683): apply_filters(‘the_content’, ‘<!– wp:
   toolset…’) #6 /Users/BigBalloo/www/wordpress.test/wp-includes/rest-api/endpoints/
   class-wp-rest-posts-controller.php(505): WP_REST_Posts_Controller->prep in /Users/
   BigBalloo/www/wordpress.test/wp-content/themes/konte-child/functions.php on line
   254 Any ideas?
 *  Thread Starter [dirkidv](https://wordpress.org/support/users/dirkidv/)
 * (@dirkidv)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/php-function-in-add_shortcode/#post-13312922)
 * I found it. It’s ok.

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

The topic ‘php function in add_shortcode’ is closed to new replies.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 2 participants
 * Last reply from: [dirkidv](https://wordpress.org/support/users/dirkidv/)
 * Last activity: [5 years, 9 months ago](https://wordpress.org/support/topic/php-function-in-add_shortcode/#post-13312922)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
