Call custom function inside shortcode
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.
Here's the function:
function showFiles($path){
if ($handle = opendir($path))
{
$up = substr($path, 0, (strrpos(dirname($path."/."),"/")));
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)) {
echo "<li><a href='/".$file."'
target=_blank>".$fName."</a></li>";
}
}
}
closedir($handle);
}
}
And here's a pared down version of the shortcode function I'm using:
function sc_showfiles( $atts, $content = null ) {
extract( shortcode_atts( array(
'type' => '',
), $atts ) );
$showfiles = '<ul><?php
showFiles(\'files/'.$type.'files/'.do_shortcode($content).'\');
?></ul>';
return $showfiles;
}
add_shortcode('showfiles', 'sc_showfiles');
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.
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:
$files = showFiles('files/'.$type.'files/'.do_shortcode($content).);
and
$showfiles = '<ul>'.$files.'</ul>';
return $showfiles;
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.
No comments:
Post a Comment