首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP:使插件在WordPress博客的主页上应用

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

Stack Overflow用户
提问于 2017-12-10 14:11:20
回答 1查看 23关注 0票数 1

我在我的WP博客中使用智能语法插件。它强调使用谷歌的漂亮语法。

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

这是插件的functions.php

代码语言:javascript
复制
<?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);
}

这是smart_syntax.php

代码语言:javascript
复制
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);没有帮助。`

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-10 16:07:02

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

function smart_syntax_prettify_script()中,您可以从该语句中删除is_singular()

代码语言:javascript
复制
if (is_singular() && !empty($matches[0]) && isset($matches[0])) {

因此,它的内容如下:

代码语言:javascript
复制
if (!empty($matches[0]) && isset($matches[0])) {

重要注

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47739684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档