2017 © Pedro Peláez
 

wordpress-plugin cmb2-wiki

image

nsrosenqvist/cmb2-wiki

  • Saturday, June 30, 2018
  • by nsrosenqvist
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

CMB2 Wiki Field

Integrates a simple but powerful CMB2 field that is not meant to save user input but to display a wiki. Great to use for the landing page that explains the theme when using CMB2 to create a page for theme options., (*1)

$docs = new_cmb2_box(array(
    'id'            => 'docs',
    'title'         => __('Documentation', 'theme'),
));

$wiki = $docs->add_field(array(
    'name' => __('Wiki', 'theme'),
    'id'   => 'wiki',
    'type' => 'wiki',
    'meta' => true, // Display file path and modification date
    'wiki_root' => __DIR__.'/wiki',
    'theme_root' => __DIR__,
    'pre_process' => true, // Enables running PHP code in the file before displaying it
    'files' => [
        __DIR__.'/wiki/Introduction.md',
    ],
));

Add a markdown renderer:, (*2)

use League\CommonMark\Converter;
use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\HtmlRenderer;

use Webuni\CommonMark\TableExtension\TableExtension;
use League\CommonMark\Inline\Element\Link;
use League\CommonMark\Inline\Element\Image;

// Prebuilt classes that fixes relative links within the files
// to be properly linked when displayed in the admin UI
use NSRosenqvist\CMB2\WikiField\Markdown\LinkRenderer;
use NSRosenqvist\CMB2\WikiField\Markdown\ImageRenderer;

// Configure Markdown renderer
add_filter('cmb2_wiki_file_content', function($content, $file, $root) { // cmb2_{field_id}_file_content
    $environment = Environment::createCommonMarkEnvironment();
    $environment->addExtension(new TableExtension());
    $environment->addInlineRenderer(Link::class, new LinkRenderer($file, $root));
    $environment->addInlineRenderer(Image::class, new ImageRenderer($file, $root));

    $converter = new Converter(new DocParser($environment), new HtmlRenderer($environment));
    return $converter->convertToHtml($content);
}, 10, 3);

// Configure markdown file title
add_filter('cmb2_wiki_file_title', function($name, $file) { // cmb2_{field_id}_file_title
    return basename($file, '.md');
}, 10, 2);

The Versions