讓 WordPress 使用者只看見自己上傳的媒體內容

快把以下這段加到你的 functions.php 裡,立馬見效!

1
2
3
4
5
6
7
8
9
10
11
function restrict_media_library( $wp_query_obj ) {
    global $current_user, $pagenow;
    if( !is_a( $current_user, 'WP_User') )
    return;
    if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
    return;
    if( !current_user_can('manage_media_library') )
    $wp_query_obj->set('author', $current_user->ID );
    return;
}
add_action('pre_get_posts','restrict_media_library');

WordPress 轉址與中文網址在 IIS 上會遇到的問題

以下包含暫時解法,因為是直接動WordPress Kernel

1. WordPress 網址無法轉址(Permalinks)

要修改 web.config 檔

 <?xml version="1.0" encoding="UTF-8"?>

<configuration>

 <system.webServer>

 <rewrite>

 <rules>

 <rule name="wordpress" stopProcessing="true">

 <match url=".*" />

 <conditions logicalGrouping="MatchAll">

 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

 </conditions>

 <action type="Rewrite" url="index.php" />

 </rule>

 </rules>

 </rewrite>

 </system.webServer>

</configuration>

2. WordPress 中文網址無法正常轉址

修改 wp-includes/ 底下的 class-wp.php

$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';

改成

$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? mb_convert_encoding( $_SERVER['PATH_INFO'], 'UTF-8', 'Big5') : '';

然後

list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] );

改成

list( $req_uri ) = explode( '?', mb_convert_encoding( $_SERVER['REQUEST_URI'], 'UTF-8', 'Big5' )  );

3. WordPress 中文檔案上傳錯誤
修改 wp-admin/includes/ 底下的 file.php

$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );

改成

$fileTypeNameArr = explode( '.', $filename );
$countNum = count( $fileTypeNameArr ) - 1;
$fileExt = $fileTypeNameArr[$countNum];
$filename = time() . '-' . rand( 0,999999999 ) . '.' . $fileExt;

—-參考文獻—-
[1] Using Permalinks – https://codex.wordpress.org/Using_Permalinks
[2] [WordPress]中文網址,發生404錯誤,教你如何解決? – http://dev.brucelulu.com/topics/180
[3] How to Setup WordPress Permalinks – https://gist.github.com/jonahvsweb/5479580
[4] IIS permalinks compatibility – web.config not working? – http://stackoverflow.com/questions/27988710/iis-permalinks-compatibility-web-config-not-working
[5] 解決 WordPress 在 IIS 中無法使用固定網址的問題 – http://www.minitw.com/archives/server/solved-can-not-be-used-in-iis-wordpress-permalink-issue.htm?variant=zh-tw
[6] WordPress小技巧(1):解決中文檔名圖片無法上傳問題 – http://www.cc.net.tw/2013/07/434/

快速針對 WordPress 的 wpdb 進行debug

直接貼入程式碼使用即可

<?php
// Print last SQL query string
echo 'Last Query : ' . $wpdb->last_query . '<br>';
// Print last SQL query result
echo 'Last Result : ';
var_dump( $wpdb->last_result );
// Print last SQL query error
echo 'Last Error : ' . $wpdb->last_error . '<br>';
?>

解決WordPress改網址後圖片出不來的問題

最近在做網站時,都是在host以測試網址先架好wordpress,資料也都上架後才買網址然後更換,就會有很多文章的圖片無法正常顯示,因為主要網址已經換掉了,即使在wordpress後台設定改了網址,也只會影響到wordpress預設欄位的網址,很多theme的post meta不會自動更新網址,只好手動去phpMyAdmin敲一些SQL指令了:

在SQL指令那邊複製貼上以下文字(記得把網址換成自己的來源與目標網址)

1
2
3
4
UPDATE wp_options SET option_value = replace(option_value, 'http://舊網址', 'http://新網址') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://舊網址','http://新網址');
UPDATE wp_posts SET post_content = replace(post_content, 'http://舊網址', 'http://新網址');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://舊網址','http://新網址');

Ubuntu 14.04 安裝Apache, MySQL, PHP (LAMP) + WordPress 權限設定

Step 1 : Apache

安裝前請先sudo apt-get update
其他就不多說了,直接裝吧

sudo apt-get update
sudo apt-get install apache2

Step 2 : MySQL

安裝指令

sudo apt-get install mysql-server php5-mysql

設定安全性

sudo mysql_install_db
sudo mysql_secure_installation

Step 3 : PHP

安裝指令

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

修改一下路徑設定檔

sudo nano /etc/apache2/mods-enabled/dir.conf

裡面原本長這樣

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

現在要把index.php往前移

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

重啟Apache

sudo service apache2 restart

權限要先設定

設定apache2成爲自己設定之資料夾的group owner,同時也是owner

sudo chown -R www-data:www-data /home/my_user_name/my_path

把自己加進www-data的group裡面

sudo usermod -a -G www-data my_user_name

把www-data列爲最高group priority

sudo usermod -g www-data my_user_name

(以下是附贈的,斟酌使用)
把user踢出group,注意要先把group priority設成別的

sudo deluser

顯示目前group

groups my_user_name

更改資料夾權限

sudo chmod -R 775 /home/my_user_name/my_path

Step 4 : phpMyAdmin

自行下載並安裝phpMyAdmin比較好控制,現在載好的版本感覺是不用調整參數,預設cookie

參照以下網址(跟本沒動過阿XD)
paste.plurk.com/show/2081776

要自己重新設定的話,網址如下
/phpmyadmin/setup/index.php

Step 5 : 目錄更改

接下來要做的事情是,把根目錄改成自己的指定路徑:

sudo nano /etc/apache2/sites-available/000-default.conf

DocumentRoot /var/www/html

改成

DocumentRoot /home/my_user_name/my_path

注意不可以使用 ~/ 作爲home directory的簡寫

sudo service apache2 restart

修改 apache2.conf

sudo nano /etc/apache2/apache2.conf

新增

<Directory /path/to/another/directory>
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
    Allow from all
</Directory>

重啓apache

sudo service apache2 restart

—參考文獻
[1] How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 14.04 – https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04
[2] Where to place my local website starting with the 2.4.7 version of apache2? – http://askubuntu.com/questions/448944/where-to-place-my-local-website-starting-with-the-2-4-7-version-of-apache2
[3] Apache can’t access folders in my home directory – http://askubuntu.com/questions/325498/apache-cant-access-folders-in-my-home-directory
[4] How to give apache2 permission to write and read files? – http://askubuntu.com/questions/246437/how-to-give-apache2-permission-to-write-and-read-files
[5] How To Install and Secure phpMyAdmin on Ubuntu 12.04 – https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-12-04
[6] Howto: Linux Add User To Group – http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/
[7] Permissions issue: how can Apache access files in my Home directory? – http://askubuntu.com/questions/26848/permissions-issue-how-can-apache-access-files-in-my-home-directory
[8] Enable Userdir – http://www.server-world.info/en/note?os=Ubuntu_14.04&p=httpd&f=5
[9] phpMyAdmin sample configuration – http://paste.plurk.com/show/2081776/