media_upload_form ( $errors = null )
media_upload_form函式用於顯示WordPress媒體上傳器中的媒體上傳表單: 當點選”新增媒體”按鈕並選擇”媒體庫”標籤時,它被呼叫。開發人員可以使用這個函式來修改媒體上傳表格,或者新增他們自己的自定義媒體上傳表格。
輸出傳統的媒體上傳表格。
function media_upload_form( $errors = null ) {
global $type, $tab, $is_IE, $is_opera;
if ( ! _device_can_upload() ) {
echo '<p>' . sprintf(
/* translators: %s: https://apps.wordpress.org/ */
__( 'The web browser on your device cannot be used to upload files. You may be able to use the <a href="%s">native app for your device</a> instead.' ),
'https://apps.wordpress.org/'
) . '</p>';
return;
}
$upload_action_url = admin_url( 'async-upload.php' );
$post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0;
$_type = isset( $type ) ? $type : '';
$_tab = isset( $tab ) ? $tab : '';
$max_upload_size = wp_max_upload_size();
if ( ! $max_upload_size ) {
$max_upload_size = 0;
}
?>
<div id="media-upload-notice">
<?php
if ( isset( $errors['upload_notice'] ) ) {
echo $errors['upload_notice'];
}
?>
</div>
<div id="media-upload-error">
<?php
if ( isset( $errors['upload_error'] ) && is_wp_error( $errors['upload_error'] ) ) {
echo $errors['upload_error']->get_error_message();
}
?>
</div>
<?php
if ( is_multisite() && ! is_upload_space_available() ) {
/**
* Fires when an upload will exceed the defined upload space quota for a network site.
*
* @since 3.5.0
*/
do_action( 'upload_ui_over_quota' );
return;
}
/**
* Fires just before the legacy (pre-3.5.0) upload interface is loaded.
*
* @since 2.6.0
*/
do_action( 'pre-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
$post_params = array(
'post_id' => $post_id,
'_wpnonce' => wp_create_nonce( 'media-form' ),
'type' => $_type,
'tab' => $_tab,
'short' => '1',
);
/**
* Filters the media upload post parameters.
*
* @since 3.1.0 As 'swfupload_post_params'
* @since 3.3.0
*
* @param array $post_params An array of media upload parameters used by Plupload.
*/
$post_params = apply_filters( 'upload_post_params', $post_params );
/*
* Since 4.9 the `runtimes` setting is hardcoded in our version of Plupload to `html5,html4`,
* and the `flash_swf_url` and `silverlight_xap_url` are not used.
*/
$plupload_init = array(
'browse_button' => 'plupload-browse-button',
'container' => 'plupload-upload-ui',
'drop_element' => 'drag-drop-area',
'file_data_name' => 'async-upload',
'url' => $upload_action_url,
'filters' => array( 'max_file_size' => $max_upload_size . 'b' ),
'multipart_params' => $post_params,
);
/*
* Currently only iOS Safari supports multiple files uploading,
* but iOS 7.x has a bug that prevents uploading of videos when enabled.
* See #29602.
*/
if (
wp_is_mobile() &&
strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false
) {
$plupload_init['multi_selection'] = false;
}
// Check if WebP images can be edited.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$plupload_init['webp_upload_error'] = true;
}
/**
* Filters the default Plupload settings.
*
* @since 3.3.0
*
* @param array $plupload_init An array of default settings used by Plupload.
*/
$plupload_init = apply_filters( 'plupload_init', $plupload_init );
?>
<script type="text/javascript">
<?php
// Verify size is an int. If not return default value.
$large_size_h = absint( get_option( 'large_size_h' ) );
if ( ! $large_size_h ) {
$large_size_h = 1024;
}
$large_size_w = absint( get_option( 'large_size_w' ) );
if ( ! $large_size_w ) {
$large_size_w = 1024;
}
?>
var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
wpUploaderInit = <?php echo wp_json_encode( $plupload_init ); ?>;
</script>
<div id="plupload-upload-ui" class="hide-if-no-js">
<?php
/**
* Fires before the upload interface loads.
*
* @since 2.6.0 As 'pre-flash-upload-ui'
* @since 3.3.0
*/
do_action( 'pre-plupload-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
?>
<div id="drag-drop-area">
<div class="drag-drop-inside">
<p class="drag-drop-info"><?php _e( 'Drop files to upload' ); ?></p>
<p><?php _ex( 'or', 'Uploader: Drop files here - or - Select Files' ); ?></p>
<p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e( 'Select Files' ); ?>" class="button" /></p>
</div>
</div>
<?php
/**
* Fires after the upload interface loads.
*
* @since 2.6.0 As 'post-flash-upload-ui'
* @since 3.3.0
*/
do_action( 'post-plupload-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
?>
</div>
<div id="html-upload-ui" class="hide-if-js">
<?php
/**
* Fires before the upload button in the media upload interface.
*
* @since 2.6.0
*/
do_action( 'pre-html-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
?>
<p id="async-upload-wrap">
<label class="screen-reader-text" for="async-upload"><?php _e( 'Upload' ); ?></label>
<input type="file" name="async-upload" id="async-upload" />
<?php submit_button( __( 'Upload' ), 'primary', 'html-upload', false ); ?>
<a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e( 'Cancel' ); ?></a>
</p>
<div class="clear"></div>
<?php
/**
* Fires after the upload button in the media upload interface.
*
* @since 2.6.0
*/
do_action( 'post-html-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
?>
</div>
<p class="max-upload-size">
<?php
/* translators: %s: Maximum allowed file size. */
printf( __( 'Maximum upload file size: %s.' ), esc_html( size_format( $max_upload_size ) ) );
?>
</p>
<?php
/**
* Fires on the post upload UI screen.
*
* Legacy (pre-3.5.0) media workflow hook.
*
* @since 2.6.0
*/
do_action( 'post-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}
要使用` 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;