2017-12-10 3 views
1

私のWPブログでSmart Syntaxプラグインを使用しています。それはGoogleのprettifyを利用して構文を強調表示します。PHP:WordPressブログのホームページにプラグインを作成する

私のカスタム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);を実行しても役に立ちませんでした。 `

答えて

1

プラグインが条件付きでスクリプトを読み込んでいるのは、単一の投稿とページにのみがあるためです。 function smart_syntax_prettify_script()インサイド

あなたはこの文からis_singular()を削除することができます:あなたはそう、プラグイン自体を編集します

if (!empty($matches[0]) && isset($matches[0])) { 

重要な注意

if (is_singular() && !empty($matches[0]) && isset($matches[0])) { 

それは読むように何らかの方法でフォークすることをお勧めします。それ以外の場合は、プラグインの作者が更新をリリースすると、変更を上書きして元に戻します。

また、プラグインの作成者に連絡して、プラグインがロードするページタイプを設定できるオプションを作成するように依頼することもできます。

関連する問題