PHP:在WordPress博客主页上应用插件

PHP:在WordPress博客主页上应用插件,php,wordpress,Php,Wordpress,我正在我的WP博客中使用智能语法插件。它突出了利用谷歌的美化语法 语法突出显示与我的自定义CSS规则完美配合,但在主页上查看帖子时,似乎没有应用将CSS应用于我帖子的Javascript 下面是插件的函数.php: <?php function smart_syntax_prettyprint($content) { global $post; global $comment; $seeker = "/(<pre)(.+)(<code.+cla

我正在我的WP博客中使用智能语法插件。它突出了利用谷歌的美化语法

语法突出显示与我的自定义CSS规则完美配合,但在主页上查看帖子时,似乎没有应用将CSS应用于我帖子的Javascript

下面是插件的
函数.php

<?php

function smart_syntax_prettyprint($content)
{
    global $post;
    global $comment;
    $seeker     = "/(<pre)(.+)(<code.+class.+[\'\"])([^\'\"]+)([\'\"]>)/i";
    $prettified = '$1 class="prettyprint lang-$4"$2$3$4$5';
    $content    = preg_replace($seeker, $prettified, $content);
    return $content;
}

function smart_syntax_prettify_script()
{
    global $post;
    global $comment;
    $content      = $post->post_content . $comment->comment_content;
    $smart_syntax = get_option('_smart_syntax_options');
    $output       = preg_match_all('/(<pre)(.+)(<code.+class.+[\'"])([^\'"]+)([\'"]>)/i', $content, $matches);
    // find language tags
    if (!empty($matches[0]) && isset($matches[0])) {
        $langs = smart_syntax_remove_dupe_langs($matches[4]);
        foreach ($langs as $lg) {
            if ($lg = 'css') {
                $lang = $lg;
            }
        }
    }
    if (is_singular() && !empty($matches[0]) && isset($matches[0])) {

        if (!empty($lang) && isset($lang)) {
            $suffix = '?lang=' . $lang;
        }

        if (isset($smart_syntax['cdn_prettify']) && $smart_syntax['cdn_prettify'] == true) {

            $source = 'https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js' . $suffix;

        } else {

            $source = SMART_SYNTAX_URL . 'assets/js/src/run_prettify.js' . $suffix;

        }
        wp_enqueue_script('smart-syntax-prettify', $source, null, null, true);
?>
                <script>prettyPrint()</script>
            <?php

        if (isset($smart_syntax['custom_skin']) && $smart_syntax['custom_skin'] == true) {
            wp_enqueue_style('smart-syntax-skin', SMART_SYNTAX_URL . 'assets/css/smart_syntax.css', true, '1.0.0');
        } elseif ($smart_syntax['custom_skin'] != true && $smart_syntax['cdn_prettify'] != true) {
            wp_enqueue_style('smart-syntax-skin', SMART_SYNTAX_URL . 'assets/css/prettify.css', true, '1.0.0');
        }
    }
}

function smart_syntax_remove_dupe_langs($array)
{
    foreach ($array as $lang => $val)
        $sort[$lang] = serialize($val);
    $uni = array_unique($sort);
    foreach ($uni as $lang => $ser)
        $sorted[$lang] = unserialize($ser);
    return ($sorted);
}
function smart_syntax_init() {
    $locale = apply_filters( 'plugin_locale', get_locale(), 'smart_syntax' );
    load_textdomain( 'smart_syntax', WP_LANG_DIR . '/smart_syntax/smart_syntax-' . $locale . '.mo' );
    load_plugin_textdomain( 'smart_syntax', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
    require_once SMART_SYNTAX_PATH . 'includes/functions.php';
    require_once SMART_SYNTAX_PATH . 'includes/admin-menu.php';
}

/**
 * Activate the plugin
 */
function smart_syntax_activate() {
    // First load the init scripts in case any rewrite functionality is being loaded
    smart_syntax_init();

}
register_activation_hook( __FILE__, 'smart_syntax_activate' );

/**
 * Deactivate the plugin
 * Uninstall routines should be in uninstall.php
 */
function smart_syntax_deactivate() {

}
register_deactivation_hook( __FILE__, 'smart_syntax_deactivate' );

// actions
    add_action( 'init', 'smart_syntax_init' );
    add_action('admin_menu','smart_syntax_menu' ); 
    add_action('wp_enqueue_scripts','smart_syntax_prettify_script');

//filters

    add_filter('the_content', 'smart_syntax_prettyprint', 10);
    add_filter('comment_text', 'smart_syntax_prettyprint', 10);
我对PHP知之甚少。执行
add_filter('is_home','smart_syntax_prettyprint',10)没有帮助。

`脚本未加载到主页上的原因是插件仅在单个帖子和页面上有条件地加载脚本

function smart\u syntax\u prettify\u script()
中,您可以从此语句中删除
is\u singular()

if (is_singular() && !empty($matches[0]) && isset($matches[0])) {
所以它是这样写的:

if (!empty($matches[0]) && isset($matches[0])) {
重要提示

您将编辑插件本身,因此建议您以某种方式进行分叉。否则,当插件作者发布更新时,它将覆盖您的更改并将其还原

您还可以联系插件作者,让他们创建一个选项,允许您设置插件应加载的页面类型