
get_bloginfo ( $show = '', $filter = 'raw' )
get_bloginfo: 這個函式檢索關於當前站點或多站點網路中當前站點的父站點的資訊: 該函式接收一個引數,指定要檢索的資訊型別,如網站標題、描述、URL等。
檢索關於當前網站的資訊。
`$show’的可能值包括:
– ‘name’ – 網站標題(在設定> General中設定)。
– ‘description’- 網站標語(在設定> General中設定)。
– ‘wpurl’ – WordPress的地址(URL)(在設定> 常規中設定)。
– ‘url’ – 網站地址(URL)(在設定> General中設定)。
– ‘admin_email’ – 管理員的電子郵件(在設定> General中設定)。
– ‘charset’ -“頁面和feeds的編碼”(在Settings > Reading中設定)。
– ‘版本’ – 當前WordPress的版本
– html_type’ – 內容型別(預設:”text/html”)。主題和外掛
可以使用{@see ‘pre_option_html_type’}過濾器覆蓋預設值。
– ‘text_direction’ – 由網站語言決定的文字方向。
應該被用來代替
– ‘language’ – 當前網站的語言程式碼
– ‘styleheet_url’ – 活動主題的樣式表的URL。一個活躍的子主題
將優先於這個值
– stylesheet_directory’ – 活動主題的目錄路徑。 一個活動的子主題
將會優先於這個值
– ‘template_url’ / ‘template_directory’ – 活動主題目錄的URL。一個活躍的
子主題不會優先考慮這個值。
– ‘pingback_url’ – 回傳的XML-RPC檔案的URL(xmlrpc.php)。
– ‘atom_url’ – Atom feed URL (/feed/atom)
– ‘rdf_url’ – RDF/RSS 1.0饋送URL(/feed/rdf)
– ‘rss_url’ – RSS 0.92 feed URL (/feed/rss)
– ‘rss2_url’ – RSS 2.0的URL(/feed)
– ‘comments_atom_url’ – 評論的Atom feed URL (/comments/feed)
– ‘comments_rss2_url’ – 評論RSS2.0的URL(/comments/feed)
一些`$show’值已被棄用,在未來的版本中會被刪除。這些選項將觸發_deprecated_argument()函式。
棄用的引數包括:
– ‘siteurl’ – 使用’url’代替
– ‘home’ – 使用’url’代替
function get_bloginfo( $show = '', $filter = 'raw' ) { switch ( $show ) { case 'home': // Deprecated. case 'siteurl': // Deprecated. _deprecated_argument( __FUNCTION__, '2.2.0', sprintf( /* translators: 1: 'siteurl'/'home' argument, 2: bloginfo() function name, 3: 'url' argument. */ __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s option instead.' ), '<code>' . $show . '</code>', '<code>bloginfo()</code>', '<code>url</code>' ) ); // Intentional fall-through to be handled by the 'url' case. case 'url': $output = home_url(); break; case 'wpurl': $output = site_url(); break; case 'description': $output = get_option( 'blogdescription' ); break; case 'rdf_url': $output = get_feed_link( 'rdf' ); break; case 'rss_url': $output = get_feed_link( 'rss' ); break; case 'rss2_url': $output = get_feed_link( 'rss2' ); break; case 'atom_url': $output = get_feed_link( 'atom' ); break; case 'comments_atom_url': $output = get_feed_link( 'comments_atom' ); break; case 'comments_rss2_url': $output = get_feed_link( 'comments_rss2' ); break; case 'pingback_url': $output = site_url( 'xmlrpc.php' ); break; case 'stylesheet_url': $output = get_stylesheet_uri(); break; case 'stylesheet_directory': $output = get_stylesheet_directory_uri(); break; case 'template_directory': case 'template_url': $output = get_template_directory_uri(); break; case 'admin_email': $output = get_option( 'admin_email' ); break; case 'charset': $output = get_option( 'blog_charset' ); if ( '' === $output ) { $output = 'UTF-8'; } break; case 'html_type': $output = get_option( 'html_type' ); break; case 'version': global $wp_version; $output = $wp_version; break; case 'language': /* * translators: Translate this to the correct language tag for your locale, * see https://www.w3.org/International/articles/language-tags/ for reference. * Do not translate into your own language. */ $output = __( 'html_lang_attribute' ); if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) { $output = determine_locale(); $output = str_replace( '_', '-', $output ); } break; case 'text_direction': _deprecated_argument( __FUNCTION__, '2.2.0', sprintf( /* translators: 1: 'text_direction' argument, 2: bloginfo() function name, 3: is_rtl() function name. */ __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s function instead.' ), '<code>' . $show . '</code>', '<code>bloginfo()</code>', '<code>is_rtl()</code>' ) ); if ( function_exists( 'is_rtl' ) ) { $output = is_rtl() ? 'rtl' : 'ltr'; } else { $output = 'ltr'; } break; case 'name': default: $output = get_option( 'blogname' ); break; } $url = true; if ( strpos( $show, 'url' ) === false && strpos( $show, 'directory' ) === false && strpos( $show, 'home' ) === false ) { $url = false; } if ( 'display' === $filter ) { if ( $url ) { /** * Filters the URL returned by get_bloginfo(). * * @since 2.0.5 * * @param string $output The URL returned by bloginfo(). * @param string $show Type of information requested. */ $output = apply_filters( 'bloginfo_url', $output, $show ); } else { /** * Filters the site information returned by get_bloginfo(). * * @since 0.71 * * @param mixed $output The requested non-URL site information. * @param string $show Type of information requested. */ $output = apply_filters( 'bloginfo', $output, $show ); } } return $output; }
要使用` get_users
`函式獲取所有使用者列表,可以按照以下步驟進行:
1. 使用` get_users
`函式呼叫獲取使用者列表:
$users = get_users();
2. 您可以按需使用引數來過濾結果。例如,您可以通過角色、使用者ID、使用者登入名等過濾使用者列表。以下是一個根據使用者角色為過濾條件的示例:
$users = get_users( array( 'role' => 'subscriber' // 將角色名稱替換為您要過濾的角色 ) );
在上述示例中,將` role
`引數設定為所需的角色名稱來過濾使用者列表。
3. 您可以使用迴圈遍歷獲取的使用者列表,並訪問每個使用者的屬性。例如,以下示例將顯示每個使用者的使用者名稱和電子郵件地址:
foreach( $users as $user ) { echo '使用者名稱:' . $user->user_login . ', 電子郵件:' . $user->user_email . ; }
在上述示例中,通過` $user->user_login
`和` $user->user_email
`訪問每個使用者的使用者名稱和電子郵件地址。
請注意,` get_users
`函式預設返回所有使用者,並可以根據需要使用更多引數進行過濾。您可以參閱WordPress官方文件中的` get_users
`函式文件,瞭解更多可用引數和用法示例。
總結起來,使用` get_users
`函式獲取所有使用者列表的步驟是:
get_users
`函式獲取使用者列表。在WordPress中,可以使用WP_PLUGIN_DIR和WP_PLUGIN_URL常量來定義外掛的目錄路徑和URL。
1. `WP_PLUGIN_DIR`:這是一個常量,用於定義外掛的目錄路徑(檔案系統路徑)。您可以使用以下程式碼在外掛檔案中訪問該常量:
$plugin_dir = WP_PLUGIN_DIR . '/your-plugin-folder/';
在上述程式碼中,將"your-plugin-folder"替換為您外掛的實際資料夾名稱。使用該常量,您可以獲取外掛檔案的完整路徑。
2. `WP_PLUGIN_URL`:這是一個常量,用於定義外掛的URL(用於在網頁上訪問外掛檔案)。以下是一個使用該常量的示例:
$plugin_url = WP_PLUGIN_URL . '/your-plugin-folder/';
同樣,請將"your-plugin-folder"替換為您外掛的實際資料夾名稱。使用該常量,您可以獲取外掛在網頁上的完整URL。
請注意,`WP_PLUGIN_DIR`和`WP_PLUGIN_URL`常量在WordPress版本2.6之後引入。從WordPress 5.5版本開始,這兩個常量被標記為過時(deprecated),因為WordPress更傾向於使用新的外掛檔案結構。如果您正在開發新外掛,建議使用新的外掛檔案結構和相關函式。
在新的外掛檔案結構中,可以使用以下函式來獲取外掛的目錄路徑和URL:
- `plugin_dir_path()`:獲取外掛目錄路徑。
- `plugin_dir_url()`:獲取外掛URL。
這些函式會自動將外掛的版本、多站點和SSL等考慮因素納入計算。
總結起來,使用`WP_PLUGIN_DIR`和`WP_PLUGIN_URL`常量定義外掛的目錄和URL的方法是:
$plugin_dir = WP_PLUGIN_DIR . '/your-plugin-folder/'; $plugin_url = WP_PLUGIN_URL . '/your-plugin-folder/';
但請注意,這兩個常量已被標記為過時,建議使用新的外掛檔案結構和相關函式來獲取外掛的路徑和URL。
使用PHP在WordPress中新增自定義功能可以通過以下方式實現:
下面是一個實操示例。
要在WordPress中新增自定義功能,可以按照以下步驟使用PHP編寫並新增自定義功能:
// 新增自定義功能示例 // 1. 建立自定義短程式碼 function custom_shortcode() { return '這是我的自定義短程式碼內容'; } add_shortcode('custom', 'custom_shortcode'); // 2. 自定義小工具 function custom_widget() { echo '這是我的自定義小工具內容'; } register_widget('custom_widget'); // 3. 自定義選單 function custom_menu() { register_nav_menu('custom-menu', '自定義選單'); } add_action('after_setup_theme', 'custom_menu'); // 4. 自定義頁面模板 function custom_page_template() { /* Template Name: 自定義模板 */ // 自定義模板的內容和樣式 }
請注意,修改主題檔案可以在主題更新時丟失,因此建議在進行任何更改之前備份functions.php檔案。此外,為避免不必要的錯誤和衝突,建議在新增自定義功能前先了解WordPress開發文件和最佳實踐,以確保正確、安全地實現所需的自定義功能。
使用 do_action
函式可以觸發一個鉤子函式。do_action
函式的引數與要觸發的鉤子函式的引數相同。
例如,觸發save_post鉤子函式的程式碼如下:
do_action( 'save_post', $post_ID, $post );
這裡,$post_ID
和 $post
是傳遞給鉤子函式的引數。
使用 wp_get_current_user
獲取當前登入使用者的資訊:
$current_user = wp_get_current_user(); // 獲取當前使用者的ID $user_id = $current_user->ID; // 獲取當前使用者的使用者名稱 $user_login = $current_user->user_login; // 獲取當前使用者的郵箱 $user_email = $current_user->user_email; // 獲取當前使用者的顯示名稱 $display_name = $current_user->display_name;