by Rain Chu | 1 月 19, 2023 | IIS, PHP, web, Windows, wordpress
在 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>
同場加映
by Rain Chu | 12 月 26, 2022 | Python, Raspberry Pi, 程式
如果想要用 Raspberry 樹梅派打造自己擁有的播放器,首選就是用VLC,可以全功能的做各種高難度的自訂控制,真的有找不到的功能,就自己再添加一下就可以了。
首先要先安裝 VLC
接下來安裝 Python VLC
python -m pip install python-vlc
封裝一個 VLC.py
import os, time
# 設置VLC庫路徑,需在import vlc之前
os.environ['PYTHON_VLC_MODULE_PATH'] = "./vlc-3.0.6"
import vlc
class Player:
'''
args:設置 options
'''
def __init__(self, *args):
if args:
instance = vlc.Instance(*args)
self.media = instance.media_player_new()
else:
self.media = vlc.MediaPlayer()
# 設置待播放的url地址或本地文件路徑,每次調用都會重新加載資源
def set_uri(self, uri):
self.media.set_mrl(uri)
# 播放 成功返回0,失敗返回-1
def play(self, path=None):
if path:
self.set_uri(path)
return self.media.play()
else:
return self.media.play()
# 暫停
def pause(self):
self.media.pause()
# 恢復
def resume(self):
self.media.set_pause(0)
# 停止
def stop(self):
self.media.stop()
# 釋放資源
def release(self):
return self.media.release()
# 是否正在播放
def is_playing(self):
return self.media.is_playing()
# 已播放時間,返回毫秒值
def get_time(self):
return self.media.get_time()
# 拖動指定的毫秒值處播放。成功返回0,失敗返回-1 (需要注意,只有當前多媒體格式或流媒體協議支持才會生效)
def set_time(self, ms):
return self.media.get_time()
# 音視頻總長度,返回毫秒值
def get_length(self):
return self.media.get_length()
# 獲取當前音量(0~100)
def get_volume(self):
return self.media.audio_get_volume()
# 設置音量(0~100)
def set_volume(self, volume):
return self.media.audio_set_volume(volume)
# 返回當前狀態:正在播放;暫停中;其他
def get_state(self):
state = self.media.get_state()
if state == vlc.State.Playing:
return 1
elif state == vlc.State.Paused:
return 0
else:
return -1
# 當前播放進度情況。返回0.0~1.0之間的浮點數
def get_position(self):
return self.media.get_position()
# 拖動當前進度,傳入0.0~1.0之間的浮點數(需要注意,只有當前多媒體格式或流媒體協議支持才會生效)
def set_position(self, float_val):
return self.media.set_position(float_val)
# 獲取當前文件播放速率
def get_rate(self):
return self.media.get_rate()
# 設置播放速率(如:1.2,表示加速1.2倍播放)
def set_rate(self, rate):
return self.media.set_rate(rate)
# 設置寬高比率(如"16:9","4:3")
def set_ratio(self, ratio):
self.media.video_set_scale(0) # 必須設置為0,否則無法修改屏幕寬高
self.media.video_set_aspect_ratio(ratio)
# 註冊監聽器
def add_callback(self, event_type, callback):
self.media.event_manager().event_attach(event_type, callback)
# 移除監聽器
def remove_callback(self, event_type, callback):
self.media.event_manager().event_detach(event_type, callback)
使用VLC.py
def my_call_back(event):
print("call:", player.get_time())
if "__main__" == __name__:
player = Player()
player.add_callback(vlc.EventType.MediaPlayerTimeChanged, my_call_back)
player.play("test.mp3")
# 防止当前进程退出
while True:
pass
def my_call_back(event):
print("call:", player.get_time())
官方文檔
https://www.olivieraubert.net/vlc/python-ctypes/doc/
參考資料
https://zhuanlan.zhihu.com/p/69906080
by Rain Chu | 10 月 8, 2022 | PHP, wordpress
WordPress 想要自訂登出後導引到某一個頁面,可以採用下面的程式碼,並且放於 functions.php 中即可
// 登出轉址
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
wp_safe_redirect( home_url() );
exit;
}
其中 home_url() 是首頁,這段程式碼的意思是,當登出使用者後,導引到首頁
by Rain Chu | 7 月 10, 2022 | PHP, 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
by Rain Chu | 7 月 10, 2022 | PHP, wordpress
我們可以用 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('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
by Rain Chu | 7 月 9, 2022 | web
除了 wordpress ,我們也可以從最基本的網頁編輯起,今天可以來推薦 2022 年最多新創採用的線上網頁編輯器。