title: Call custom function inside shortcode
body: <p>I have a function to display a list of files from a given directory as links. I then have a shortcode to display the function, but I can't figure out how to make the shortcode work without using inline php to call the function. The shortcode only works right now because I have another third-party plugin that allows inline php in the WordPress text editor. </p>
  
  <p>Here's the function:</p>
  
  <pre><code>function showFiles($path){
     if ($handle = opendir($path))
     {
         $up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
  
         while (false !== ($file = readdir($handle)))
         {
             if ($file != "." &amp;&amp; $file != "..")
             {
                 $fName  = $file;
                 $file   = $path.'/'.$file;
                 if(is_file($file)) {
                     echo "&lt;li&gt;&lt;a href='/".$file."' target=_blank&gt;".$fName."&lt;/a&gt;&lt;/li&gt;";
                 }
             }
      }
         closedir($handle);
     }    
  }
  </code></pre>
  
  <p>And here's a pared down version of the shortcode function I'm using:</p>
  
  <pre><code>function sc_showfiles( $atts, $content = null ) {
           extract( shortcode_atts( array(
           'type' =&gt; '',
       ), $atts ) );
  
       $showfiles = '&lt;ul&gt;&lt;?php showFiles(\'files/'.$type.'files/'.do_shortcode($content).'\'); ?&gt;&lt;/ul&gt;';
       return $showfiles;
  }
  add_shortcode('showfiles', 'sc_showfiles');
  </code></pre>
  
  <p>The content area allows the user to enter a shortcode with a user generated meta so the directory it's pulling from will match the logged in user. </p>
  
  <p>I've tried about six different things, and haven't been able to achieve this without calling the showFiles function using inline php. I came close once using:</p>
  
  <pre><code>$files = showFiles('files/'.$type.'files/'.do_shortcode($content).);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>$showfiles = '&lt;ul&gt;'.$files.'&lt;/ul&gt;';
   return $showfiles;
  </code></pre>
  
  <p>but that threw the list of files at the top of the page, I assume because the original showFiles function is echoing the html output. But if I change echo to return in the top function I get nothing. </p>
  
  <p>//////////////////// FINAL WORKING CODE ///////////////////////////</p>
  
  <p>With thanks to FaddishWorm for his help.</p>
  
  <pre><code>function showFiles($path){
     if ($handle = opendir($path))
     {
         $up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
          $thefiles .= '&lt;span class="checkmarks"&gt;&lt;ul&gt;';
         while (false !== ($file = readdir($handle)))
         {
             if ($file != "." &amp;&amp; $file != ".." &amp;&amp; $file !== "index.html")
             {
                 $fName  = $file;
                 $file   = $path.'/'.$file;
                 if(is_file($file)) {
                     $thefiles .= "&lt;li&gt;&lt;a href='/".$file."' target=_blank&gt;".$fName."&lt;/a&gt;&lt;/li&gt;";
                 }
             }
      }
         closedir($handle);
          $thefiles .= '&lt;/ul&gt;&lt;/span&gt;';
         return $thefiles;
     }    
  }
  
  function sc_showfiles( $atts, $content = null ) {
           extract( shortcode_atts( array(
           'type' =&gt; '',
       ), $atts ) );
  $thefiles = showFiles('files/'.$type.'files/'.do_shortcode($content));  
       return $thefiles;
  }
  add_shortcode('showfiles', 'sc_showfiles');
  </code></pre>