Mmm Simple File List Shortcode

Below, you’ll find a detailed guide on how to add the Mmm Simple File List Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Mmm Simple File List Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Mmm Simple File List Plugin and the shortcodes it provides:

Plugin Icon
Mmm Simple File List

"Mmm Simple File List is a WordPress plugin that conveniently organizes all your files. With its user-friendly interface, managing and sharing documents, images, and other files becomes a breeze."

★★★★☆ (19) Active Installs: 2000+ Tested with: 5.9.8 PHP Version: false
Included Shortcodes:
  • [MMFileList]

Mmm Simple File List [MMFileList] Shortcode

The MMFileList shortcode is designed to list files from a specified directory. It provides various options like sorting files by name or date, limiting the number of files displayed, and filtering files based on their extensions. The shortcode also supports custom formatting and can strip unwanted characters from file names.

Shortcode: [MMFileList]

Parameters

Here is a list of all possible MMFileList shortcode parameters and attributes:

  • folder – Path of the folder to list files from
  • format – How to format the list (li, li2, img, custom, table, comma)
  • types – File types to include in the list
  • class – CSS class to apply to the list
  • limit – Maximum number of files to list
  • orderby – How to sort the files (name or date)
  • order – Order to sort files (asc or desc)
  • target – Target attribute for file links
  • prettyname – Whether to remove special characters from file names
  • regexstrip – Regex pattern to remove from file names
  • regexreplace – What to replace stripped patterns with
  • regexfilter – Regex pattern to filter files by
  • regexfilterinclusive – Whether to include or exclude files matching the filter
  • dateformat – How to format file modification dates
  • headings – Headings to use in table format
  • removesize – Whether to remove file size from the list
  • removeextension – Whether to remove file extension from the list
  • usecwd – Whether to use the current working directory as the base directory

Examples and Usage

Basic example – Display a list of all files in a specific folder.

[MMFileList folder="myFolder"]

Advanced examples

Display a list of all PDF files in a specific folder, ordered by date in descending order.

[MMFileList folder="myFolder" types="pdf" orderby="date" order="desc"]

Display a list of all files in a specific folder, but limit the output to 5 files only.

[MMFileList folder="myFolder" limit="5"]

Display a list of all files in a specific folder, with the filenames replaced by a pretty name (removes underscores, dashes, and the file extension).

[MMFileList folder="myFolder" prettyname="true"]

Display a list of all files in a specific folder, with the file size and file extension removed from the output.

[MMFileList folder="myFolder" removesize="true" removeextension="true"]

Display a list of all files in a specific folder, with a custom regex to strip certain parts of the filename.

[MMFileList folder="myFolder" regexstrip="/[0-9]/" regexreplace=""]

Display a list of all files in a specific folder, but only include files that match a certain regex filter.

[MMFileList folder="myFolder" regexfilter="/\.pdf$/" regexfilterinclusive="true"]

Display a list of all files in a specific folder, but exclude files that match a certain regex filter.

[MMFileList folder="myFolder" regexfilter="/\.pdf$/" regexfilterinclusive="false"]

Display a list of all files in a specific folder, with a custom date format for the file modification date.

[MMFileList folder="myFolder" dateformat="F j, Y, g:i a"]

Display a list of all files in a specific folder, with the output formatted as a table.

[MMFileList folder="myFolder" format="table"]

Display a list of all files in a specific folder, with the output formatted as a comma-separated list.

[MMFileList folder="myFolder" format="comma"]

PHP Function Code

In case you have difficulties debugging what causing issues with [MMFileList] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'MMFileList', array(&$this, 'ListFiles') );

Shortcode PHP function:

function ListFiles($atts, $content="")
    {   
        //Strip any empty <p> tags
        //Credit goes to: https://gist.github.com/jlengstorf/5370457
        $content = $this->copter_remove_crappy_markup($content);

        extract( shortcode_atts( array(
        'folder' => '',
        'format' => 'li',
        'types' => '',
        'class' => '',
        'limit' => '-1',
        'orderby' => 'name', //name or date
        'order' => "asc",
        'target' => '',
        'prettyname' => false,
        'regexstrip' => '',
        'regexreplace' => ' ',
        'regexfilter' => '',
        'regexfilterinclusive' => false,
        'dateformat' => 'Y-m-d H:i:s',
        'headings' => '',
        'removesize' => false,
        'removeextension' => false,
        'usecwd' => false
        ), $atts ) );
        
        $this->removesize = $removesize;
        $this->removeextension = $removeextension;

        $folder = $this->_check_for_slashes($folder);

        $baseDir = wp_upload_dir(); //Base Upload Directory
        $dir = $baseDir['path'] . '/' . $folder;
        $outputDir = $baseDir['url'] . '/' . $folder; //ex. http://example.com/wp-content/uploads/2010/05/../../cats

        if ($usecwd) {
            $dir = getcwd() . '/'.  $folder;
            $outputDir = $folder;
        }
        
        $typesToList = array_filter(explode(",", $types));

        $output = "";

        $files = is_dir($dir);

        if (!$files)
        {
            $output .= sprintf('<div class="mmm-warning">The folder "%s" was not found at: "%s".', $dir, $outputDir);
        }
        else
        {
            $files = scandir($dir);

            $list = array();

            if ($orderby == "date")
            {
                $files = array_reverse($this->rearrange_files_by_date($dir . "/", $files));
            }

            if ($order == "desc")
            {
                $files = array_reverse($files);
            }

			if ($regexfilter != "") {
                $filteredfiles = array();

                foreach($files as $file)
                {
					$filterMatch = preg_match($regexfilter, $file) == 1;
                    if ($filterMatch && $regexfilterinclusive != false) {
                        array_push($filteredfiles, $file);
                    }
                    elseif (!$filterMatch && $regexfilterinclusive == false) {
						array_push($filteredfiles, $file);
                    }
                }
                $files = $filteredfiles;
            }
			
            foreach($files as $file)
            {
                $path_parts = pathinfo($file);

                if (isset($path_parts['extension'])) //check for folders - don't list them
                {
                    $extension = $path_parts['extension'];
                    
                    if($file != '.' && $file != '..')
                    {
                        if(!is_dir($dir.'/'.$file))
                        {
                            $filename = $file;
                            if ($prettyname != false)
                            {
                                $filename = $this->_regex_remove($file, '/-|_| |\.[a-zA-Z0-9]*$/');
                            } elseif ($this->removeextension != false) {
                                $filename = $this->_regex_remove($file, '/\.[a-zA-Z0-9]*$/');
                            }

                            if ($regexstrip != "")
                            {
                                $filename = $this->_regex_remove($filename, $regexstrip, $regexreplace);
                            }

                            $modifiedDate = date($dateformat, filemtime($dir . "/" . $file));

                            $file = array("name" => $filename, "ext" => $extension, "date" => $modifiedDate, "url" => $outputDir . "/" . $file, "size" => $this->human_filesize(filesize($dir . '/' . $file)));
                            
                            //If we are looking for specific types then only list those types, otherwise list everything
                            if (count($typesToList) > 0)
                            {
                                if (in_array($extension, $typesToList))
                                {
                                    array_push($list, $file);
                                }
                            }
                            else
                            {
                                array_push($list, $file);
                            }
                        }
                    }
                }
            }
            
            if (is_numeric($limit))
            {
                if ($limit > 0)
                {
                    $list = array_slice($list, 0, $limit);
                }
            }

            if ($target != '')
            {
                $target = 'target="' . $target . '"';
            }

            if (count($list) == 0)
            {
                $output .= sprintf('<div class="mmm-warning">No files (of extension(s): "%s") found in: %s </div>', $types, $outputDir);
            }
            else
            {
                $formatAtts = array("class" => $class, "target" => $target);

                $size = '<span class="filesize"> ({size})</span>';
                $titlesize = '({size})';
                if ($this->removesize != false) {
                    $size = '';
                    $titlesize = '';
                }

                switch($format){
                    case 'li':
                        $output = $this->_MakeUnorderedList($list, $content, $formatAtts);
                        break;
                    case 'li2':
                        $output = $this->_MakeUnorderedList($list,
                                    "<a href=\"{url}\"{target}><span class=\"filename\">{name}</span>{$size} <span class=\"dateModified\">{date}</span> <span class=\"extension mm-{ext}\">{ext}</span></a>",
                                    $formatAtts);
                        break;
                    case 'img':
                        $listTemplate = '<ul class="%s">%s</ul>';

                        if ($content == "")
                        {
                            $content = $this->_AddFileAttsToTemplate("<a href=\"{url}\"{target}><img src=\"{url}\" class=\"{class}\" title=\"{name} {$titlesize}\" /></a>", $formatAtts);
                        }

                        $output = $this->_MakeUnorderedList($list, $content, $formatAtts);
                    break;
                    case 'custom':
                        $output = $this->_OutputList($list, $content, $formatAtts);
                    break;
                    case 'table':
                        $output = $this->_MakeTabularLIst($list, $content, $formatAtts, $headings);
                    break;
                    case 'comma':
                        $output = $this->_MakeCommaDelimitedList($list);
                    break;
                    default:
                        $output = $this->_MakeUnorderedList($list, $content, $formatAtts);
                    break;
                }
            }
        }
        
        return $output;
    }

Code file location:

mmm-file-list/mmm-file-list/mm_filelist_plugin.php

Conclusion

Now that you’ve learned how to embed the Mmm Simple File List Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *