link_submit_meta_box()
概述
link_submit_meta_box() 函数用于在 WordPress 后台显示链接创建或编辑的表单字段,包括链接名称、URL、可见性等输入项,并处理删除操作。它主要面向开发者自定义链接管理界面。
关键要点
- 函数接受一个 $link 参数,表示当前链接对象,用于填充表单字段。
- 输出表单包含链接名称、URL、可见性(Y/N)等字段,并支持删除链接的确认操作。
- 使用 WordPress 核心函数如 checked()、esc_attr_e()、wp_nonce_url() 来确保安全性和正确显示。
- 触发 submitlink_box 和 post_submitbox_start 钩子,允许开发者扩展功能。
- 函数自 WordPress 2.7.0 版本引入,位于 wp-admin/includes/meta-boxes.php 文件中。
代码示例
function link_submit_meta_box( $link ) {
// 检查链接 ID 是否存在以决定显示编辑或创建表单
if ( ! empty( $link->link_id ) ) {
// 显示编辑表单,包括链接名称、URL 和可见性字段
// 使用 checked() 函数设置可见性复选框状态
// 提供删除链接的按钮,使用 wp_nonce_url() 添加安全 nonce
} else {
// 显示创建新链接的表单
}
// 触发 submitlink_box 钩子
do_action( 'submitlink_box' );
}注意事项
- 确保 $link 参数正确传递链接对象,否则表单字段可能无法正确填充。
- 删除操作使用 JavaScript 确认对话框,需确保非空链接 ID 和正确 nonce 以防止 CSRF 攻击。
- 函数输出 HTML 表单,适合在 meta box 中使用,但需注意样式和布局兼容性。
Displays link create form fields.
Parameters
$linkobjectrequired-
Current link object.
Source
function link_submit_meta_box( $link ) {
?>
<div class="submitbox" id="submitlink"><div id="minor-publishing">
<div style="display:none;">
</div>
<div id="minor-publishing-actions">
<div id="preview-action">
link_id ) ) { ?>
<a class="preview button" href="<?php echo $link->link_url; ?>" target="_blank"></a></div>
<div class="clear"></div>
</div><div id="misc-publishing-actions">
<div class="misc-pub-section misc-pub-private">
<label for="link_private" class="selectit"><input id="link_private" name="link_visible" type="checkbox" value="N" <?php checked( $link->link_visible, 'N' ); ?> /> </label>
</div>
</div></div>
<div id="major-publishing-actions">
<div id="delete-action">
%s</a>',
wp_nonce_url( "link.php?action=delete&link_id=$link->link_id", 'delete-bookmark_' . $link->link_id ),
/* translators: %s: Link name. */
esc_js( sprintf( __( "You are about to delete this link '%s'n 'Cancel' to stop, 'OK' to delete." ), $link->link_name ) ),
__( 'Delete' )
);
}
?>
</div><div id="publishing-action">
link_id ) ) { ?>
<input name="save" type="submit" class="button button-primary button-large" id="publish" value="<?php esc_attr_e( 'Update Link' ); ?>" /><input name="save" type="submit" class="button button-primary button-large" id="publish" value="<?php esc_attr_e( 'Add Link' ); ?>" />
</div>
<div class="clear"></div>
</div><div class="clear"></div>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/meta-boxes.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/meta-boxes.php#L1100">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L1100-L1167">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/post_submitbox_start/"><span class="hook-func">do_action</span>( ‘post_submitbox_start’, <nobr><span class="arg-type">WP_Post|null</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Fires at the beginning of the publishing actions section of the Publish meta box.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/submitlink_box/"><span class="hook-func">do_action</span>( ‘submitlink_box’ )</a></dt><dd><p>Fires at the end of the Publish box in the Link editing screen.</p>
</dd></dl></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/submit_button/">submit_button()</a><code>wp-admin/includes/template.phpEchoes a submit button, with provided text and appropriate class(es).
esc_attr_e() wp-includes/l10n.phpDisplays translated text that has been escaped for safe use in an attribute.
esc_js() wp-includes/formatting.phpEscapes single quotes,
",,&, and fixes line endings.checked() wp-includes/general-template.phpOutputs the HTML checked attribute.
current_user_can() wp-includes/capabilities.phpReturns whether the current user has the specified capability.
__() wp-includes/l10n.phpRetrieves the translation of $text.
_e() wp-includes/l10n.phpDisplays translated text.
wp_nonce_url() wp-includes/functions.phpRetrieves URL with nonce added to URL query.
do_action() wp-includes/plugin.phpCalls the callback functions that have been added to an action hook.
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |