Skip to content

Instantly share code, notes, and snippets.

@alanpilloud
Created July 25, 2016 11:38
Show Gist options
  • Save alanpilloud/42ecd08737a022c0d453a5b5a92d5f47 to your computer and use it in GitHub Desktop.
Save alanpilloud/42ecd08737a022c0d453a5b5a92d5f47 to your computer and use it in GitHub Desktop.
Add admin dashboard widgets for each .md file found in theme root.
<?php
/**
* Add admin dashboard widgets for each .md file found in theme root.
*
* reads the *.md markdown files at the root of the theme and display a widget for each .md in the admin dashboard
* <!> Requires the class Parsedown https://github.com/erusev/parsedown
* <!> includes classes/Parsedown.php
*/
add_action('wp_dashboard_setup', function(){
$themeDir = get_template_directory();
$markdownFiles = glob($themeDir.'/*.md');
// Is there any .md files ?
if (!empty($markdownFiles)) {
include $themeDir.'/classes/Parsedown.php';
$Parsedown = new Parsedown();
// Add a widget for each .md file found
foreach ($markdownFiles as $k => $file) {
wp_add_dashboard_widget(
'website_documentation_'.$k, //slug
str_replace('.md', '', ucfirst(basename($file))), //name
function() use ($file, $Parsedown) { //content
$text = file_get_contents($file);
echo $Parsedown->text($text);
}
);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment