
內容簡介
此外掛可在每個小工具中新增一個名為 "Widget logic" 的額外控制欄,讓您控制這個小工具會出現在哪些頁面上。文字欄位允許您使用 WP 的Conditional Tags或任何通用的 PHP 程式碼。
請注意,您所輸入的 Widget logic 會直接被 EVAL,任何有權限編輯小工具外觀的人都可以添加任何程式碼,包括惡意和可能具有破壞性的功能。此外,有一個可選的篩選器 'widget_logic_eval_override' 可供您使用,以繞過 EVAL,使用您自己的程式碼(請參閱其他注意事項)。
配置和選項在通常的小工具管理介面中。
配置
除了透過這個外掛程式對小工具進行條件邏輯外,還有三個選項被添加到小工具管理頁面的底部(請見屏幕截圖)。
使用 'wp_reset_query' 修復 — WP 的許多功能,以及許多主題和外掛程式,都可能干擾條件標記,使 is_home 不在首頁上是真的。通常可以透過在呼叫小工具之前快速使用 wp_reset_query() 陳述式來解決這個問題,這個選項可以替您加入這個程式碼,而不必編輯程式碼。
Load logic — 這個選項允許您設置頁面加載時點檢查小工具邏輯的時間。在 v.50 之前,它是在 'wp_head' 觸發時(也就是 HTML 的 HEAD 區塊建立期間)發生的。許多主題並沒有呼叫 wp_head,這是一個問題。從 v.50 開始,預設情況下,這個過程盡可能早,即當這個外掛程式加載時就開始。您現在可以指定這些「後期加載」點(按時間順序):
在主題載入後 (after_setup_theme 觸發)
在所有 PHP 載入後 (wp_loaded 觸發)
在查詢變數設置後 (parse_query 觸發) - 這是預設值
在頁面header區塊期間 (wp_head 觸發)
如果您的邏輯取決於定義在主題 functions.php 文件中的函數,則可能需要延遲載入。相反,如果您想確保小工具計數正確地計算,例如,在側邊欄沒有小工具時顯示替代版面或內容,則可能希望盡早載入它。
Do not cache widget logic results — 從 v.58 開始,widget logic 程式碼只需執行一次就可以,但這可能會導致某些主題出現意外結果,因此這個選項可用來關閉這個行為。(每次調用 sidebars_widgets 篩選器時都會評估程式碼的真/假)。
撰寫邏輯程式碼
"Widget logic" 欄位中的文字可以是完整的 PHP 程式碼,並應在您需要小工具出現時返回 "true"。如果沒有 "return" 在文字中,將在開頭隱式添加 "return",在結尾添加 ";"。(這只是使像 is_home() 這樣的單個語句更方便。)
基本原理
充分利用WP 的條件標記。您可以使用下列方式變化和結合代碼:
!(NOT)來反轉邏輯,例如 !is_home() 當不是首頁時是 TRUE。
||(OR)來組合條件。當 X 為真或 Y 為真時,X OR Y 為 TRUE。
&&(AND)使條件更加具體。X AND Y 在 X 和 Y 都為 TRUE 時為 TRUE。
WP論壇和網站上有許多出色的代碼示例。但 WP Codex 也是一個有用的資源,值得參考。
外掛標籤
開發者團隊
📦 歷史版本下載
原文外掛簡介
This plugin gives every widget an extra control field called “Widget logic” that lets you control the pages that the widget will appear on. The text field lets you use WP’s Conditional Tags, or any general PHP code.
The configuring and options are in the usual widget admin interface.
BIG UPDATE:
Now you can control widget in Gutenberg Widgets editor as well as in Classic Editor. It is just as easy as before but also in gutenberg view.
Pre-installed widgets let you add special widget with one click of the mouse. First pre-installed widget is Live Match that let you add widget of one random live football game with real time score updates (teams logos, livescore, minute of the match, tournament name). And more interesting widgets to come!
NOTE ON DEFAULT FUNCTIONS: Widget Logic includes a whitelist of common WordPress conditional tags and safe functions. If you need additional WordPress functions that are not currently whitelisted, please create a topic in our support forum to request them. We regularly add commonly requested functions in new releases.
Configuration
Aside from logic against your widgets, there are three options added to the foot of the widget admin page (see screenshots).
Use ‘wp_reset_query’ fix — Many features of WP, as well as the many themes and plugins out there, can mess with the conditional tags, such that is_home is NOT true on the home page. This can often be fixed with a quick wp_reset_query() statement just before the widgets are called, and this option puts that in for you rather than having to resort to code editing
Load logic — This option allows you to set the point in the page load at which your widget logic if first checked. Pre v.50 it was when the ‘wp_head’ trigger happened, ie during the creation of the HTML’s HEAD block. Many themes didn’t call wp_head, which was a problem. From v.50 it happens, by default, as early as possible, which is as soon as the plugin loads. You can now specify these ‘late load’ points (in chronological order):
after the theme loads (after_setup_theme trigger)
when all PHP loaded (wp_loaded trigger)
after query variables set (parse_query) – this is the default
during page header (wp_head trigger)
You may need to delay the load if your logic depends on functions defined, eg in the theme functions.php file. Conversely you may want the load early so that the widget count is calculated correctly, eg to show an alternative layour or content when a sidebar has no widgets.
Don’t cache widget logic results — From v .58 the widget logic code should only execute once, but that might cause unexpected results with some themes, so this option is here to turn that behaviour off. (The truth/false of the code will be evaluated every time the sidebars_widgets filter is called.
Custom PHP Functions — From v.6.0.6 you can use the widget_logic_allowed_functions filter to add custom PHP functions that will be allowed in Widget Logic fields. By default, only WordPress conditional tags and a whitelist of safe functions are available. This filter allows you to extend the functionality and use your own custom functions.
To add a custom function, add the following code to your theme’s functions.php file:
`add_filter('widget_logic_allowed_functions', 'my_allowed_functions');
function my_allowed_functions($functions) {
$functions[] = ‘my_custom_function_name‘;
return $functions;
}`
You can add multiple functions by using one wrapper function:
`add_filter('widget_logic_allowed_functions', 'my_allowed_functions');
function my_allowed_functions($functions) {
$functions[] = ‘is_special_page’;
$functions[] = ‘is_user_verified’;
$functions[] = ‘get_sidebar_title’;
return $functions;
}`
IMPORTANT NOTE ON VARIABLES: Widget Logic is designed to work with simple data types (strings, numbers, booleans). If you need to use complex variables, global state, or conditional logic that depends on many factors, create a custom function in your theme’s functions.php file and call it from Widget Logic:
Good approach (in functions.php):
`function is_special_page() {
global $post;
$special_ids = array(5, 10, 15);
$conditions = some_complex_function();
return is_page() && in_array($post->ID, $special_ids) && $conditions;
}`
Then in Widget Logic field, simply use: is_special_page()
Less ideal approach (in Widget Logic field):
Avoid putting complex logic directly in the Widget Logic field. Keep it simple and let your custom function handle the complexity. This keeps your widget settings clean and maintainable.
Interaction with External Services
Widget Logic uses the external service to obtain up-to-date information about the results of football matches. widgetlogic.org is a source of sports information, that provides a wide range of information about football, including various leagues, tournaments, and championships from around the world.
The functioning of the widgetlogic.org service is based on delivering real-time data about selected matches without the need to refresh the page. This means that data is automatically updated without requiring page reload. This approach ensures users quick and uninterrupted access to the latest sports data without the effort of manually updating information, allowing them to stay informed about ongoing events in real-time.
Writing Logic Code
The text in the ‘Widget logic’ field can be full PHP code and should return ‘true’ when you need the widget to appear. If there is no ‘return’ in the text, an implicit ‘return’ is added to the start and a ‘;’ is added on the end. (This is just to make single statements like is_home() more convenient.)
The Basics
Make good use of WP’s own conditional tags. You can vary and combine code using:
! (NOT) to reverse the logic, eg !is_home() is TRUE when this is NOT the home page.
|| (OR) to combine conditions. X OR Y is TRUE when either X is true or Y is true.
&& (AND) to make conditions more specific. X AND Y is TRUE when both X is true and Y is true.
There are lots of great code examples on the WP forums, and on WP sites across the net. But the WP Codex is also full of good examples to adapt, such as Test if post is in a descendent category.
Examples
is_home() — just the main blog page
!is_page('about') — everywhere EXCEPT this specific WP ‘page’
!is_user_logged_in() — shown when a user is not logged in
is_category(array(5,9,10,11)) — category page of one of the given category IDs
is_single() && in_category('baked-goods') — single post that’s in the category with this slug
current_user_can('level_10') — admin only widget
strpos($_SERVER['HTTP_REFERER'], "google.com")!=false — widget to show when clicked through from a google search
is_category() && custom_function_to_check_the_category() — category page that’s a descendent of category 5
custom_function_from_functions_php_to_check_the_page() — WP page that is a child of page 77
custom_function_from_functions_php_to_check_the_page_child_of(13) — home page OR the page that’s a child of page 13
