Implement PHP Markdown class in a Wordpress Theme

2019-08-08 19:35发布

问题:

I am trying to implement the php-markdown class in a Wordpress theme.

I am wondering if I am going about this the right way. I have put the Michelf folder into themeroot/_/inc/php/ and in my header.php I have the following:

<?php

    set_include_path(get_include_path().PATH_SEPARATOR.get_template_directory().'/_/inc/php');

    spl_autoload_register(function($class){
        require preg_replace('{\\\\|_(?!.*\\\\)}', DIRECTORY_SEPARATOR, ltrim($class, '\\')).'.php';
    });

    use \Michelf\Markdown;

?>

Then I am trying to use it like so:

<?php echo Markdown::defaultTransform($someText); ?>

Which results in the error:

Fatal error: Class 'Markdown' not found in /directories/.../index.php on line 11

It works if I do the following:

<?php echo Michelf\Markdown::defaultTransform($someText); ?>

Which is odd because the readme itself doesn't require the Michelf?

Just wondering if I am going about including this class the wrong way?

回答1:

I think there is a typo in the docs. You need the Michelf\ because it's namespaced. Look at Markdown.php, line 13, and you'll see.