WordPressã§Javascriptã§ã¯ãªãPHPã使ã£ã¦ã·ã³ã¿ãã¯ã¹ãã¤ã©ã¤ããè¡ãæé
Photo by Chris Ried on Unsplash
ã¯ããã«
ã·ã³ã¿ãã¯ã¹ãã¤ã©ã¤ããè¡ãã©ã¤ãã©ãªã¨ãã¦Javascriptã®highlight.jsãæåã§ãããJavascriptã§ã¯ãªãPHPçã®highlight.phpã¨ãããã®ãããã¾ããJavascriptã§ã¯ãªãPHPã ã¨ä½ãå¬ãããã¨è¨ãã¨ãAMPã§ã¯Javascriptãåºæ¬çã«åä½ã§ããªããããhighlight.jsã使ç¨ãããã¨ãã§ãã¾ãããããã§PHPçã®highlight.phpã使ç¨ããã°ããµã¼ãã¼ãµã¤ãã§ã®ãã¤ã©ã¤ã¿ã¼ã¨ãã¦åä½ãã¾ããã¤ã¾ãJavascriptã使ããªããããªãã¡AMP対å¿ã®ãã¼ã¸ã§ãåé¡ãªãåä½ãã¾ããAMP対å¿ã®Webãµã¤ãã§ãªãå ´åã§ããèªã¿è¾¼ãJavascriptãæ¸ãããã®ã§è¡¨ç¤ºé度ã®é«éåãæå¾ ã§ãã¾ãã ãã®è¨äºã§ã¯ãWordPressã§highlight.phpã使ç¨ããæé ãã¾ã¨ãã¾ãã
åæã¨ç°å¢
以ä¸ã®éãã§ãã
- PHPã¯ã¤ã³ã¹ãã¼ã«æ¸ã¨ããããã¼ã¸ã§ã³æ å ±ã¯å¾è¿°
- WordPressã§ä½¿ç¨ãã¦ãããã¼ãã®ãã£ã¬ã¯ããªã«Composerãã¤ã³ã¹ãã¼ã«æ¸ã¨ãã
- 確èªããOS : Ubuntu18.04ãCloud Linux
PHPã®ãã¼ã¸ã§ã³æ å ±ã¯ä»¥ä¸ã§ãã
$ php -v
PHP 7.3.3 (cli) (built: Mar 7 2019 10:02:44) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.3, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.3, Copyright (c) 1999-2018, by Zend Technologies
ãªããPHPã®ããã±ã¼ã¸ç®¡çã·ã¹ãã ã§ããComposerã以éã®æé ã§ä½¿ç¨ãã¾ããããã¾ã Composerãã¤ã³ã¹ãã¼ã«ãã¦ããªãå ´åã¯ãã¯ããã«Composerãã¤ã³ã¹ãã¼ã«ããå¿ è¦ãããã¾ãã以ä¸ã«Composerã®ã¤ã³ã¹ãã¼ã«æé ãã¾ã¨ãã¦ãã¾ãã®ã§ãå¿ è¦ãªæ¹ã¯è¦ã¦ã¿ã¦ãã ãããã¬ã³ã¿ã«ãµã¼ãã®ãããªå ±æç°å¢ã§ãã¤ã³ã¹ãã¼ã«å¯è½ã§ãã
PHPã®ããã±ã¼ã¸ç®¡çã·ã¹ãã ã§ããComposerãUbuntuãCloud Linuxã®ä¸¡æ¹ã«å ¥ããæ©ä¼ããã£ãã®ã§ãã®æé ãã¡ã¢ãã¾ããUbuntuã¨Cloud Linuxã§æé ã¯åãã§ããã¾ããã°ãã¼ãã«ããã¼ã«ã«ããããã«ã¤ã³ã¹ãã¼ã«æé ãã¾ã¨ãã¾ãã
Composerã使ã£ã¦highlight.phpãã¤ã³ã¹ãã¼ã«ãã
ããã§ã¯ãWordPressã«ã¦å®éã«ä½¿ç¨ãã¦ãããã¼ããã¡ã¤ã«ã®ãã£ã¬ã¯ããªã«Composerã使ã£ã¦highlight.phpãã¤ã³ã¹ãã¼ã«ãã¾ããComposerã¯ã°ãã¼ãã«ç°å¢ããã¼ã«ã«ç°å¢ã®ã©ã¡ãã«ã¤ã³ã¹ãã¼ã«ããã¦ãã¦ãåé¡ããã¾ãããããã§ã¯ãWordPressã®ãã¼ããã¡ã¤ã«ã®ãã£ã¬ã¯ããªãmytheme
ã¨ããmytheme/bin
ã«Composerãã¤ã³ã¹ãã¼ã«ããã¦ããåæã§é²ãã¾ãã
Composerãã¤ã³ã¹ãã¼ã«ããã¦ããã°highlight.phpã®ã¤ã³ã¹ãã¼ã«ã¯ç°¡åã§ã以ä¸ã®ã³ãã³ããå®è¡ããã ãã§ãã
$ bin/composer require scrivo/highlight.php
Using version v9.15.6.1 for scrivo/highlight.php
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing scrivo/highlight.php (v9.15.6.1): Downloading (100%)
Writing lock file
Generating autoload files
以ä¸ã§highlight.phpã®ã¤ã³ã¹ãã¼ã«ã¯å®äºã§ããèªèº«ã®ãã¼ããã¡ã¤ã«ã®ãã£ã¬ã¯ããªã«vendor
ã¨ãããã£ã¬ã¯ããªãä½æãããããã®ä¸ã«highlight.phpãã¤ã³ã¹ãã¼ã«ããã¦ãã¾ããç¶ãã¦å®éã«WordPressã§highlight.phpã使ãããã®æé ã説æãã¾ãã
function.phpã«è¿½è¨ãã
highlight.phpãWordPressã®ãã¼ããã¡ã¤ã«å
ã§ä½¿ç¨ããããã«ãfunction.php
ã«ããã¤ã追è¨ãã¾ãã
ã¾ããComposerã§ã¤ã³ã¹ãã¼ã«ããhighlight.phpãèªã¿è¾¼ãããã«ã以ä¸ãfunction.php
ã«è¿½è¨ãã¾ãã
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Highlight\Highlighter;
ï¼...以ä¸çç¥...ï¼
ã¾ããWordPressã®è¨äºæ¬æã®ä¸ãããã³ã¼ãé¨åã«ç¸å½ããé¨åï¼ä¾ãã°ã
~
ãªã©ï¼ãæ¢ãã¦ã·ã³ã¿ãã¯ã¹ãã¤ã©ã¤ããé©ç¨ããããã«ä»¥ä¸ã®é¢æ°ãfunction.php
ã«è¿½å ãã¾ãã追å ç®æã¯function.php
ã®ä¸çªæå¾ã§å¤§ä¸å¤«ã§ãã
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Highlight\Highlighter;
ï¼...éä¸çç¥...ï¼
function applySyntaxHighlighting($content) {
// 以ä¸ã®æ£è¦è¡¨ç¾ã§ãè¨äºæ¬æã®ä¸ãã<pre><code> ~ </code></pre>çãæ¢ãåºãã
$pattern = '~<pre><code[^>]*>\K.*(?=</code></pre>)~Uis';
return preg_replace_callback($pattern, function ($match) {
$highlighter = new Highlighter();
// 以ä¸ã§ã·ã³ã¿ãã¯ã¹ãã¤ã©ã¤ããè¡ãããè¨èªã追å ãã¦ããã¾ãã
// 以ä¸ã®ä¸ããhighlight.phpãèªåã§ãã¤ã©ã¤ãã«ã¼ã«ãé¸æãã¾ãã
$highlighter->setAutodetectLanguages([
'html',
'php',
'css',
'javascript',
'shell',
'ruby',
'java'
]);
$input = htmlspecialchars_decode($match[0]);
return $highlighter->highlightAuto($input)->value;
}, $content);
}
// WordPressã®the_contenté¢æ°ã®ãã£ã«ã¿ã¼ã¨ãã¦ä½¿ç¨ãã
add_filter('the_content', 'applySyntaxHighlighting', 10);
ä¸è¨ã®applySyntaxHighlighting
ã¨ããé¢æ°ã¯èªåã§å®ç¾©ããé¢æ°ã§ãããWordPressè¨äºæ¬æã®ä¸ã«å«ã¾ããã³ã¼ãé¨åãæ¤ç´¢ãããã¤ã©ã¤ãå¦çãè¡ãªãã¾ãã
éè¦ãªã®ã¯ä»¥ä¸ï¼ç¹ã§ããããããã«ã¤ãã¦å¾è¿°ãã¾ãã
$pattern
ã«è¨äºæ¬æã®ã³ã¼ãé¨åã®æ¤ç´¢ãè¡ãããã®æ£è¦è¡¨ç¾ãæå®ãã¾ããsetAutodetectLanguages
ã«æ¸¡ãå¼æ°ã«ãã¤ã©ã¤ããããè¨èªãæå®ãã¾ããadd_filter
ã§WordPressã®the_content
é¢æ°ã®ãã£ã«ã¿ã¼ã¨ãã¦ç»é²ãã¾ãã
æ£è¦è¡¨ç¾ã使ã£ã¦ã³ã¼ãé¨åãæ¤ç´¢ãã
以ä¸ã®æ£è¦è¡¨ç¾ã«ãã£ã¦ã
~
ãæ¤ç´¢ãããã¨ãã§ãã¾ãã
// <pre><code> ~ </code></pre> ãæ¤ç´¢ãã
$pattern = '~<pre><code[^>]*>\K.*(?=</code></pre>)~Uis';
ã¾ãããã
~
ã§ã¯ãªããä¾ãã°ä½ãããCSSã®ã¯ã©ã¹ã使ç¨ãã¦ãããããªå ´åã¯ãæ¤ç´¢ããããããããæ£è¦è¡¨ç¾ãããããã®ç¶æ³ã«åããã¦èª¿æ´ããå¿
è¦ãããã¾ããä¾ãã°ãã®ãµã¤ãã§ã¯ã~ ~
ã¨ããããã«ã
ã¨
ã®éã«
ãå ¥ã£ã¦ããå ´åã¨å ¥ã£ã¦ããªãå ´åã®ä¸¡æ¹ãããã¾ãããã£ã¦ããããæ¤ç´¢ããã«ã¯æ£è¦è¡¨ç¾ã以ä¸ã®ããã«ãã¾ãã
/wp-content/themes/mytheme/function.php// <pre><span class="code-title">~</span><code> ~ </code></pre> ã®ãããªãã¿ã¼ã³ãæ¤ç´¢ãããå ´åã¯ä»¥ä¸ $pattern = '~<pre>(<span class="code-title">.*</span>)?<code[^>]*>\K.*(?=</code></pre>)~Uis';
ããã®æ£è¦è¡¨ç¾ã§æ£ããèªèº«ã®ã³ã¼ãç¸å½é¨åãæ¤ç´¢ã§ããªãã¨ãã¤ã©ã¤ãã§ãã¾ããã
setAutodetectLanguagesã«ãã¤ã©ã¤ããããè¨èªãæå®ãã
以ä¸ã®ããã«ããã¤ã©ã¤ããããè¨èªãäºåã«æå®ãã¦ããå¿ è¦ãããã¾ãã
/wp-content/themes/mytheme/function.php$highlighter->setAutodetectLanguages([ 'html', 'php', 'css', 'javascript', 'shell', 'ruby', 'java' ]);
highlight.phpã§ã¯ãhighlight.jsã¨åæ§ã«ç¾å¨ï¼ï¼ï¼è¨èªã«å¯¾å¿ãã¦ãã¾ãã対å¿è¨èªã¯ä»¥ä¸ãã確èªã§ãã¾ãã®ã§ãèªèº«ã®è¨äºã®ä¸ã§ä½¿ãã§ãããè¨èªãæå®ãã¦ããã¾ãã
add_filterã使ã£ã¦ãã£ã«ã¿ã¼ã¨ãã¦ç»é²ãã
WordPressã§ã¯ã
the_content
ã¨ããé¢æ°ã«ãã£ã¦è¨äºæ¬æãåºåãããã¨ãã§ãã¾ããããã¦ãã®åºååå¾ã§è¨äºæ¬æã«å¯¾ãã¦ä½ãããã®ä¿®æ£ãå ããæ¹æ³ã¨ãã¦add_filter
ãç¨æããã¦ãã¾ãããªããthe_content
é¢æ°ã¯é常ã ã¨è¨äºã®åºåã«ä½¿ããã¦ãããå½ç¶ãªããthe_content
é¢æ°ã«å¯¾ãããã£ã«ã¿ã¼ã¯å ¨ã¦ã®è¨äºã«å½±é¿ãã¾ãããããã£ã¦ãäºåã«æã£ã¦ããåä½ããããããã¹ãç°å¢ãªã©ã§ç¢ºèªãããã¨ããããããã¾ãã ããã§ã¯add_filter
ã®è©³ç´°ã¯çç¥ãã¾ããã以ä¸ã®è¨äºã大å¤åèã«ãªãã¾ãããqiita.comè¨äºæ¬æãå å·¥ã»è§£æãããå ´å㯠`the_content` ã«å¯¾ãã¦ãã£ã«ã¿ã¼ãããã¾ãã
以ä¸ã§
function.php
ã¸ã®è¿½è¨ã¯å®äºã§ãã ãªããä¸è¨ã®é¢æ°ã«ã¤ãã¦ã¯ä»¥ä¸ã®è¨äºãåèã«ããã¦é ãã¾ããããããã¨ããããã¾ãããstefanzweifel.ioSyntax Highlighting doesn't always have to be run on the client side.
ãã¤ã©ã¤ãç¨ã®CSSã追å ãã
æå¾ã«ããã¤ã©ã¤ãç¨ã«ä½¿ç¨ããCSSã¹ã¿ã¤ã«ãèªèº«ã®WordPressãã¼ãã«è¿½å ãã¾ããhighlight.phpã§ã¯highlight.jsã¨åãã¹ã¿ã¤ã«ã使ç¨ã§ãã以ä¸ã®ãµã¤ããã好ããªãã®ãé¸æãã¾ãã以ä¸ãã¼ã¸ãéãã¦å·¦å´ã®ã¡ãã¥ã¼ã®ãStylesããã好ããªãã®ãã¯ãªãã¯ãã¦ã©ã®ãããªè¦ãç®ã«ãªããã確èªã§ãã¾ãã
ãã®ãµã¤ãã§ã¯
Tomorrow Night Eighties
ã使ç¨ãã¦ãã¾ãã使ç¨ãããã¹ã¿ã¤ã«ã決ã¾ã£ãããä»åº¦ã¯ä»¥ä¸ã®ãã¼ã¸ã«å ¨ã¦ã®ã¹ã¿ã¤ã«ã®CSSãã¡ã¤ã«ãç½®ãã¦ããã®ã§æ±ºããã¹ã¿ã¤ã«ãã¡ã¤ã«ãéãã¾ããä¾ãã°
Tomorrow Night Eighties
ãªãã°tomorrow-night-eighties.css
ãéãã¾ãã以ä¸ã®ãããªä¸èº«ã«ãªã£ã¦ããã®ã§ãããå ¨ã¦ã³ãã¼ãã¾ããtomorrow-night-eighties.css/* Tomorrow Night Eighties Theme */ /* Original theme - https://github.com/chriskempson/tomorrow-theme */ /* https://jmblog.github.com/color-themes-for-google-code-highlightjs */ /* Tomorrow Comment */ .hljs-comment, .hljs-quote { color: #999999; } (...éä¸çç¥...) .hljs-strong { font-weight: bold; }
ãã¨ã¯ä¸è¨CSSãèªèº«ã®ãã¼ããã¡ã¤ã«ã®CSSã«è¿½è¨ããã°å®äºã§ãããã¡ãããhighlightç¨ã®CSSãã¡ã¤ã«ããã¦ã³ãã¼ããã¦ã¤ã³ãã¼ããã¦ãOKã§ãã ã¡ãªã¿ã«ããã®ã¹ã¿ã¤ã«ãåæ ãããã«ã¯ã
ã®ããã«
~
hljs
ã¯ã©ã¹ãæå®ããã
ã¿ã°ãå¿ è¦ã§ãã ããããã¾ã§ã«å ¬éæ¸ã®èªåã®è¨äºå ã®ã³ã¼ãé¨åã§
ã¿ã°ã§
hljs
ãæå®ããã¦ããªãå ´åã¯ãæå®ããªãéããã¤ã©ã¤ãã¯æå¹ã«ãªãã¾ãããè¨äºãå°ãªãå ´åã¯æä½æ¥ã§ãè¯ãã§ãããè¨äºãå¤ãå ´åã¯ãWordPressã®MySQLãã¼ã¿ãã¼ã¹ãã¨ã¯ã¹ãã¼ããã¦ä¸æ¬ç½®æãã¦ãã¾ãã®ãä¸çªã¯ããã§ããã¾ã¨ã
AMP対å¿ã«ãã£ãã£ã¦ã·ã³ã¿ãã¯ã¹ãã¤ã©ã¤ããJavascriptã使ããã«å®ç¾ããæ¹æ³ãæå¤ã«ãããä¸ã«ãæ å ±ãå°ãªãã£ãã®ã§ã¡ã¢ã¨ãã¦æ®ãã¦ããã¾ãã
é¢é£è¨äº
- å ¬éæ¥ï¼2020/01/03 æ´æ°æ¥ï¼2020/01/03
WordPressã®ããããã¼ã¸ã§ç¹å®ã«ãã´ãªãé¤å¤ãéå®ãªã©ãã£ã«ã¿ãã¦è¡¨ç¤ºããã«ã¹ã¿ãã¤ãº
ãã®è¨äºã§ã¯ãæ稿ãã¼ã¸ã®ã«ãã´ãªããã£ã«ã¿ãã¦ãç¹å®ã®ã«ãã´ãªã®æ稿ãã¼ã¸ãè¨äºä¸è¦§ããé¤å¤ããããéã«ç¹å®ã®ã«ãã´ãªã®æ稿ãã¼ã¸ã®ã¿ãè¨äºä¸è¦§ã«å«ããããã®ã«ã¹ã¿ãã¤ãºæ¹æ³ãã¾ã¨ãã¾ããé常ã«ç°¡åã§ãã
- å ¬éæ¥ï¼2019/05/23 æ´æ°æ¥ï¼2019/05/23
WordPressãµã¤ããå ¬å¼ãã©ã°ã¤ã³ã使ã£ã¦AMP対å¿ãããçµæã¨æé
Google Analyticsããã¢ãã¤ã«è¡¨ç¤ºã®åé¡ãæ·±å»ã®ãã対å¿ããããã¡ã¼ã«ãå±ãã¦ãã¾ãããããã¦ãã®ä¸ã§WordPresså ¬å¼ã®AMPãã©ã°ã¤ã³çã使ç¨ããããæ¸ããã¦ããã®ã§å®éã«å°å ¥ãã¦ã¿ã¾ããããã®è¨äºã§ã¯ãã®æé ãã¡ã¢ãã¾ãã
- å ¬éæ¥ï¼2019/05/15 æ´æ°æ¥ï¼2019/05/15
ConoHa WINGã§WordPressãµã¤ããæ°ããæ§ç¯ããæé
WordPressãµã¤ããæ§ç¯ããéã«ãç¾å¨ã¯è²ã ãªã¬ã³ã¿ã«ãµã¼ããããé¸æè¢ãè±å¯ã§ããä»åã¯GMOã¤ã³ã¿ã¼ãããæ ªå¼ä¼ç¤¾æ§ãæä¾ããConoHa WINGã¨ããã¬ã³ã¿ã«ãµã¼ãä¸ã«WordPressãå®éã«æ§ç¯ãã¦ã¿ãã®ã§ãã®æé ãã¾ã¨ãã¾ãã
- å ¬éæ¥ï¼2019/05/15 æ´æ°æ¥ï¼2019/05/15
ConoHa WINGã¸ã®ç§»è¡ã«ãããµã¤ã表示é度ãåçã«æ¹åãããã®ã§ãã®çµæã¨ç§»è¡æé ã«ã¤ãã¦ã®ã¾ã¨ã
ãã®åº¦ãã®ãµã¤ããConoHa WINGã«ç§»è¡ããã¨ãããäºæ³ã大ããä¸åãæ¹åã¨ãªãã¾ããããã®è¨äºã§ã¯ãå®éã®æ¹åçµæã®è©³ç´°ã¨ãä»ã®ã¬ã³ã¿ã«ãµã¼ãä¼ç¤¾æ§ããConoHa WINGã¸ã®ç§»è¡ã®ããã«è¡ã£ãæé ãã¾ã¨ãã¾ãã
éçºã¢ããª
æ¯æ¥ã®å°ããªåºæ¥äºããªãã§ãè¨é²ãã¦ããã°ã¨ãã¦æ®ãããã®ã©ã¤ããã°ã¢ããªã§ãã