PHP: сделать плагин применить на домашней странице в блоге WordPress
Я использую плагин Smart Syntax в своем блоге WP. Он выделяет синтаксис с использованием Google prettify.
Подсветка синтаксиса прекрасно работает с моими пользовательскими правилами CSS, но кажется, что Javascript, применяющий CSS к моим сообщениям, не применяется, когда сообщение просматривается на главной странице.
Вот плагин 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);
не помогло `
1 ответ
Причина, по которой скрипт не загружается на главной странице, заключается в том, что плагин загружает скрипт по условию только для отдельных постов и страниц.
внутри function smart_syntax_prettify_script()
вы можете удалить is_singular()
из этого заявления:
if (is_singular() && !empty($matches[0]) && isset($matches[0])) {
Так что это гласит:
if (!empty($matches[0]) && isset($matches[0])) {
Важная заметка
Вы будете редактировать сам плагин, поэтому рекомендуется его каким-то образом раскошелиться. В противном случае, когда автор плагина выпустит обновление, он отменит ваше изменение и вернет его обратно.
Вы также можете связаться с автором плагина и попросить его создать опцию, позволяющую вам указать, на каких типах страниц должен загружаться плагин.