我在WP博客中使用了Smart Syntax插件。它突出了利用谷歌美化的语法。
语法突出显示与我的自定义CSS规则完美配合,但似乎在主页上查看帖子时,应用CSS到我的帖子的Javascript是不适用的。
这是插件functions.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);
}
这里smart_syntax.php
:
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);
没有帮助。
`
答案 0 :(得分:1)
脚本未在主页上加载的原因是因为该插件有条件地在单个帖子和页面上加载仅脚本。
在function smart_syntax_prettify_script()
内,您可以从此声明中删除is_singular()
:
if (is_singular() && !empty($matches[0]) && isset($matches[0])) {
所以它写着:
if (!empty($matches[0]) && isset($matches[0])) {
重要提示
您将编辑插件本身,因此建议您以某种方式对其进行分叉。否则,当插件作者发布更新时,它将覆盖您的更改并将其还原。
您也可以联系插件作者并要求他们创建一个选项,以允许您设置插件应加载的页面类型。