本文使用「署名 4.0 國際 (CC BY 4.0)」許可協議,歡迎轉載、或從新修改使用,但須要註明來源。 署名 4.0 國際 (CC BY 4.0)php
本文做者: 蘇洋html
建立時間: 2019年06月28日 統計字數: 3644字 閱讀時間: 8分鐘閱讀 本文連接: soulteary.com/2019/06/28/…nginx
今年年初的時候,我曾經寫過接近十篇博客,介紹如何「從零到一搭建Wiki」,聊過了 MediaWiki、Doku、Confluence、Tiddly、MoinMoin 等系統,可是這裏涉及的許多系統的寫做體驗都不是很好。web
在以後,我也寫過幾篇 「如何使用容器技術搭建 WordPress」 的文章,據官方數據稱 WordPress 目前已經佔據了互聯網 34% 的應用,寫做體驗和插件生態其實仍是很棒的,那麼咱們爲什麼不使用 WordPress 來搭建 Wiki 呢?docker
在使用了4個月以後,體驗下來問題不大,我決定把方法分享出來,但願能幫到更多的人。若是你熟悉 WordPress 的搭建,本篇將文章兩三分鐘內就可以搞定啦。編程
在搭建以前,咱們明確一下我的 Wiki 的主要功能(以我我的爲例):瀏覽器
別看這些功能都很「基礎」,文章開頭提到的軟件,這些功能或多或少都不支持、或者作的差強人意呢。bash
搭建過程以前的文章中有提,有興趣的同窗能夠進行翻閱。cookie
固然,你也可使用傳統的方案進行搭建。session
使用最新的 5.0 版本的軟件,默認安裝完畢後,你將看到下面的界面。
默認的界面適合展現博客列表內容,對於 Wiki 用途而言不是特別友好,解決方案十分簡單,咱們進入管理後臺,先將 ** 二〇一九** 主題切換爲 ** Twenty Sixteen** 。
而後點擊主題上方的「自定義」按鈕,開始對主題進行自定義調整。點擊左側菜單的小工具,而後按照你的需求對主題進行調整,好比我在這裏,保留了「搜索框」、「分類目錄」、「近期文章」、「功能」四個模塊,並按照這個順序對模塊進行了調整。
接着打開 設置菜單中的閱讀頁面,將主頁顯示調整爲靜態頁面,並選擇靜態頁面爲示例頁面。
當前展現的頁面看起來仍是不像一個Wiki,那麼咱們繼續進行調整。
仍是打開管理後臺的主題菜單頁面,選擇最後一項編輯(Theme Editor),開始對主題源文件進行修改。(若是你由於一些緣由不能在瀏覽器直接修改這個文件,也能夠經過編輯 wp-content/themes/twentysixteen/page.php
文件來達到一樣效果)
在右側選擇 page.php
,原始的代碼以下:
<?php
/**
* The template for displaying pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other "pages" on your WordPress site will use a different template.
*
* @package WordPress
* @subpackage Twenty_Sixteen
* @since Twenty Sixteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) :
the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
// End of the loop.
endwhile;
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
複製代碼
修改很簡單,咱們把最下面的 get_sidebar();
放到 <main>
以前,並補充一段樣式便可。
<?php
/**
* The template for displaying pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other "pages" on your WordPress site will use a different template.
*
* @package WordPress
* @subpackage Twenty_Sixteen
* @since Twenty Sixteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<style>
.sidebar{
column-count: 5;
column-width: 240px;
column-gap: 20px;
}
</style>
<?php get_sidebar(); ?>
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) :
the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
// End of the loop.
endwhile;
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_footer(); ?>
複製代碼
當你在 Wiki 中適當填充一些內容以後,你會獲得這樣的頁面。
在插件中心搜索並安裝 WP Githuber MD
,完成以後,記得啓用插件。
再次打開編輯器,你會發現本來的所見即所得編輯器就變成了咱們熟悉的左右分欄的 Markdown 編輯器。
在瀏覽器性能愈來愈高的今天,咱們幾乎徹底不須要再使用服務端進行代碼高亮啦。而且我的 Wiki 幾乎沒有搜索引擎 SEO 的需求。
在插件中心搜索並安裝 WP Code Highlight.js
,啓用插件後,文章中的代碼便會自動進行高亮展現啦。
若是你是我的使用,搭建在內網,徹底不須要考慮權限問題,那麼能夠和我同樣,設置 WordPress 自動登陸。
在 wp-config.php
的 require_once ABSPATH . 'wp-settings.php';
前,添加下面一段代碼。
if ($_SERVER['SCRIPT_NAME'] == '/wp-login.php') {
require_once ABSPATH . 'wp-settings.php';
require_once ABSPATH . '/wp-load.php';
if (
isset($_SERVER['QUERY_STRING']) &&
(strpos($_SERVER['QUERY_STRING'], '=logout') !== false)
) {
wp_destroy_current_session();
wp_clear_auth_cookie();
do_action('wp_logout');
} else {
$user_login = getenv('WP_USER');
$user = get_userdatabylogin($user_login);
$user_id = $user->ID;
wp_set_current_user($user_id, $user_login);
wp_set_auth_cookie($user_id);
do_action('wp_login', $user_login);
}
wp_redirect(home_url(), 302);
die;
}
複製代碼
實現原理很簡單,趕在程序大部隊執行前,劫持應用登錄路由,自動替用戶設置登陸狀態。
WordPress 是一款開源免費的軟件,由 PHP 編寫。前文提過,據官方數據,目前已經佔據了 34% 的互聯網軟件份額。
Use the software that powers over 34% of the web. — wordpress.org/download
我我的從 2009 年開始使用它到如今:
在新浪雲工做的時候,我負責過 WP4SAE 的開發維護,即便不看平臺下載數據,單從我每次換公司,都能發現有很多的同事用過,就能夠看出用戶量應該還不錯(偷笑); 在淘寶工做的時候有寫過幾個下載量還不錯的 WP 插件,其中一個被 360 CDN 資源站官方推薦(用於替換 Google Fonts,加速博客打開); 也曾基於它(淘寶UED博客)作過一套自動化的 D2 會議電子票程序,歷史 GitHub 倉庫到如今還有近千 pull request 和大幾百的 fork; 還曾在內部使用它做爲 confluence 的替代者,存放多個修改版本的技術文章…
從某種意義來講,我也算是見證了這套軟件的進化過程。固然,個人我的成長過程當中也屢次受惠於這套軟件。我認爲這是一款偉大的軟件,某種意義來講,也是一個很成功的開源項目。
但從網上的帖子來看,一旦提起這款軟件,老是出現過分的批判,甚至許多人根本不知道時過境遷,一些事情早已被改變。甚至出現了批判 WordPress 是政治正確的事情… 技術沒有銀彈,軟件也是,在適合的場景用適合的技術,遇到問題分析並解決問題,纔是技術人應該作的事情,而不是一味批判和吐槽。
感謝 WordPress ,替我節約了大量的時間去折騰更有意思的事情。
— EOF
我如今有一個小小的折騰羣,裏面彙集了一些喜歡折騰的小夥伴。
在不發廣告的狀況下,咱們在裏面會一塊兒聊聊軟件、HomeLab、編程上的一些問題,也會在羣裏不按期的分享一些技術沙龍的資料。
喜歡折騰的小夥伴歡迎掃碼添加好友。(請註明來源和目的,不然不會經過審覈)