How to render media shortcodes e.g [[fid:1234]] I've written a custom database query to pull out taxonomy data in a specific way, the problem is embedded media shortcodes are outputting the shortcode strings as they stand in the database, rather than the content they represent.

e.g:  [["fid:"1234"]] instead of &lt;a href="/link-to-pdf/file.pdf"&gt;pdf&lt;/a&gt;.

The taxonomy body field can contain:


Embedded nodes ([[nid:1]])
links to other content (node/nid) 
embedded media etc: pdfs ([["fid":"6247" "other_props":....]])


I've written regex to parse out the first 2 bullet points, and call _node_embed_replacements() &amp; drupal_get_path_alias() respectively to insert the correct content for them, but i'm having a hard time working out where media module renders embedded shortcodes such as [["fid":"6247"]].
I found the following load_file() and field_view_file() functions which worked a treat for me. I bundled them with a regex search and replace.

Rough abstract taken from my code not tested but the theory remains the same.

preg_match_all('/\[\[{"fid": ?"([0-9]+)".*?\]\]/', $tax_body, $matches);
foreach ($matches[1] as $index = &gt; $fid) {
  $file = file_load($fid); //load the file
  $content = file_view_file($file); // turn it into a render arrray
  $replacement= drupal_render($content); // render it into html string
  str_replace($matches[0][$index], $replacement, $tax_body);
}


Perform a regex for [[{"fid":(???) ..] where the ??? is the file id that is captured in the $matches[1].

Loop through all found instances, lookup the file markup, and then do a search and replace on the entire string replacing the full shortcodes (matches[0]) with the replacement markup.