Select Page
WordPress 關閉自動更新的方法

WordPress 關閉自動更新的方法

自動更新是個好功能,但因為 WordPress 本身的特性,外掛幾乎都是第三方寫的,總是難免會有很多的衝突,要求一個穩定的系統,只能把自動更新關閉,之後手動更新,確認沒有衝突後再讓他全站更新出去,才能確保穩定性,以下介紹幾種關閉自動更新的方法。

1.使用佈景主題內的 functions.php 寫入關閉自動更新的程式碼

將以下的程式碼放到佈景主題下的functions.php中即可

// 關閉自動更新以及通知
function remove_core_updates(){
global $wp_version;return(object) array('last_checked'=> time(),'version_checked'=> $wp_version,);
}
add_filter('pre_site_transient_update_core','remove_core_updates'); //hide updates for WordPress itself
add_filter('pre_site_transient_update_plugins','remove_core_updates'); //hide updates for all plugins000
add_filter('pre_site_transient_update_themes','remove_core_updates'); //hide updates for all themes

2.設定 wp-config.php 檔案

在 WP 的根目錄中,修改 wp-config.php ,將下面這一行加入即可

define( 'WP_AUTO_UPDATE_CORE', false );

3.利用外掛來關閉自動更新

3.1 disable-admin-notices

https://clearfy.pro/disable-admin-notices/

windows server iis 下的 wordpress web.config 設定

在 windows server 的 iis 下,預設情況下安裝 php 架構的 wordpress 總是會遇到很多的困難,像是如果遭遇到想要訪問 wordpress 目錄下的目錄資料,例如:https://rain.tips/uploads/,會跟你說找不到資料,原因是wordpress所有的入口要先透由 index.php 去做路由,解決方案則是用 web.config 去指定路由要透過 index.php 即可

請在web.config檔案中添加 rewrite rules

    <rewrite>
      <rules>
			<rule name="WordPress: https://yoururl.com" patternSyntax="Wildcard">
				<match url="*"/>
					<conditions>
						<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>

最後會長成這樣子

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
			<rule name="WordPress: https://yoururl.com" patternSyntax="Wildcard">
				<match url="*"/>
					<conditions>
						<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>

同場加映

WordPress logout 登出的轉址設定

WordPress 想要自訂登出後導引到某一個頁面,可以採用下面的程式碼,並且放於 functions.php 中即可

// 登出轉址
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
  wp_safe_redirect( home_url() );
  exit;
}

其中 home_url() 是首頁,這段程式碼的意思是,當登出使用者後,導引到首頁

WordPress-註冊後跳轉到特定網頁

對於使用 WordPress 來做會員管理的人,需要針對註冊、登入、登出等會員行未做個細節的控管,第一步應該要先修改會員註冊後,要給他一個反應頁面,通常是「Thank You」的頁面,謝謝他的註冊,告知他註冊已經成功並且下一個步驟應該往那邊去,例如去領註冊獎勵金。

/**
  * 註冊後,跳轉到指定頁面
  */
function auto_login_new_user( $user_id ) {
	wp_set_current_user($user_id);
	wp_set_auth_cookie($user_id);
	// 跳轉到 thank you 網頁
	//wp_redirect( home_url() ); 
    wp_redirect('/thank-you/');
	exit;
}
add_action( 'user_register', 'auto_login_new_user');

https://www.wpdaxue.com/user-first-login-redirect.html

WordPress-取得User Id的方法

我們可以用 php 程式,來取得使用者的資訊,最重要的當然是 User ID 的取得,能取得 User ID 才能進行使用者的資訊取得,取得uid的方法百百種,這邊列出四種常用的場景,取得當下登入的使用者ID,另用會員的關鍵資料取得會員ID,利用文章取得作者ID,在商店中取得客戶ID,

1.get_current_user_id()

$current_user_id = get_current_user_id();
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;

2.使用會員資訊取得ID,get_user_by()

利用Email取的ID

$the_user = get_user_by('email', '[email protected]');
$the_user_id = $the_user->ID;

利用登入帳號

$the_user = get_user_by('login', 'rain');
$the_user_id = $the_user->ID;

3.利用文章資訊取得作者 ID

$my_post = get_post( $id ); // $id - Post ID
echo $my_post->post_author; // print post author ID

4.在woocommerce中取得訂單的客戶ID

$order = wc_get_order( 123 ); // your order ID
$customer_id = $order->get_customer_id(); // or $order->get_user_id() – the same

WordPress 存取 user meta 的方法

在 WordPress 中,尤其是使用到 woocommerce 商店功能時候,最常需要客製化的通常是使用者的各式各樣資訊,例如有使用者的手機號碼、推薦人資訊等,通常這些資訊沒有良好的plugin可以支援,欄位高度客製化,這時可以寫點 php 程式,來支持讓自己的網站更美好,以下分CRUD新增、修改、刪除、讀取來做說明。

新增 user meta 的方法

add_user_meta(int $user_id, string $meta_key,  mixed $meta_value,  bool $unique = false);

刪除 user meta 的方法

delete_user_meta( $user_id, $meta_key, $meta_value );

修改 user meta 的方法

update_user_meta ( $user_id, $meta_key, $meta_value, $prev_value );

讀取 user meta 的方法

get_user_meta( $user_id, $meta_key, $single );

參考資料