函数文档

install_plugins_favorites_form()

💡 云策文档标注

概述

install_plugins_favorites_form() 函数用于在插件收藏页面显示一个用户名表单,允许用户输入或更新其 WordPress.org 用户名以管理收藏的插件。

关键要点

  • 函数显示一个表单,用于输入 WordPress.org 用户名,以访问插件收藏功能。
  • 使用 get_user_option() 获取当前用户的 wporg_favorites 选项,以预填充表单。
  • 通过 wp_create_nonce() 创建安全 nonce,防止跨站请求伪造(CSRF)。
  • 表单包含隐藏字段,如 action 和 _wpnonce,用于安全处理提交。
  • 相关函数包括 esc_attr_e()、_e()、esc_attr() 等,用于国际化和安全转义。

代码示例

Source function install_plugins_favorites_form() {
	$user   = get_user_option( 'wporg_favorites' );
	$action = 'save_wporg_username_' . get_current_user_id();
	?>
	<form method="post">
		<input type="hidden" name="action" value="<?php echo esc_attr( $action ); ?>" />
		<?php wp_nonce_field( $action ); ?>
		<label for="wporg_username"><?php _e( 'Your WordPress.org username:' ); ?></label>
		<input type="text" id="wporg_username" name="wporg_username" value="<?php echo esc_attr( $user ); ?>" />
		<?php submit_button( __( 'Get Favorites' ), 'primary', 'submit', false ); ?>
	</form>
	<?php
}

📄 原文内容

Shows a username form for the favorites page.

Source

function install_plugins_favorites_form() {
$user = get_user_option( 'wporg_favorites' );
$action = 'save_wporg_username_' . get_current_user_id();
?>
<p></p>
<form method="get">
<input type="hidden" name="tab" value="favorites" />
<p>
<label for="user"></label>
<input type="search" id="user" name="user" value="<?php echo esc_attr( $user ); ?>" />
<input type="submit" class="button" value="<?php esc_attr_e( 'Get Favorites' ); ?>" />
<input type="hidden" id="wporg-username-nonce" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( $action ) ); ?>" />
</p>
</form>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/plugin-install.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/plugin-install.php#L366">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/plugin-install.php#L366-L381">View on GitHub</a></p></section>

<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/esc_attr_e/">esc_attr_e()</a><code>wp-includes/l10n.php

Displays translated text that has been escaped for safe use in an attribute.

get_user_option()wp-includes/user.php

Retrieves user option that can be either per Site or per Network.

_e()wp-includes/l10n.php

Displays translated text.

esc_attr()wp-includes/formatting.php

Escaping for HTML attributes.

wp_create_nonce()wp-includes/pluggable.php

Creates a cryptographic token tied to a specific action, user, user session, and window of time.

get_current_user_id()wp-includes/user.php

Gets the current user’s ID.

Show 4 moreShow less

Changelog

Version Description
3.5.0 Introduced.