link_advanced_meta_box()
云策文档标注
概述
link_advanced_meta_box() 函数用于在 WordPress 后台显示链接的高级选项表单字段,包括图像、RSS 和备注等输入框。
关键要点
- 函数接受一个必需的参数 $link,表示当前链接对象,用于预填充表单字段值。
- 表单字段包括 link_image(链接图像)、link_rss(链接 RSS)、link_notes(链接备注)和 link_rating(链接评级),使用 esc_attr() 和 textarea_escaped 进行安全转义。
- 该函数自 WordPress 2.6.0 版本引入,属于核心功能,用于管理链接元数据。
代码示例
function link_advanced_meta_box( $link ) {
?>
<input type="text" name="link_image" value="<?php echo esc_attr( $link->link_image ? $link->link_image : '' ); ?>" />
<input type="text" name="link_rss" value="<?php echo esc_attr( $link->link_rss ? $link->link_rss : '' ); ?>" />
<textarea name="link_notes"><?php echo $link->link_notes ? $link->link_notes : ''; ?></textarea>
<select name="link_rating">
<?php for ( $rating = 0; $rating <= 10; $rating++ ) {
echo '<option value="' . $rating . '"';
if ( isset( $link->link_rating ) && $link->link_rating === $rating ) {
echo ' selected="selected"';
}
echo '>' . $rating . '</option>';
} ?>
</select>
<?php
}注意事项
- 确保 $link 参数是一个有效的链接对象,否则可能导致表单字段显示错误或空值。
- 使用 esc_attr() 和 textarea_escaped 来防止 XSS 攻击,确保数据安全输出。
- 此函数主要用于后台管理界面,开发者可以自定义或扩展其功能以适配特定需求。
原文内容
Displays advanced link options form fields.
Parameters
$linkobjectrequired-
Current link object.
Source
function link_advanced_meta_box( $link ) {
?>
<table class="links-table" cellpadding="0">
<tr>
<th scope="row"><label for="link_image"></label></th>
<td><input type="text" name="link_image" class="code" id="link_image" maxlength="255" value="<?php echo ( isset( $link->link_image ) ? esc_attr( $link->link_image ) : '' ); ?>" /></td>
</tr>
<tr>
<th scope="row"><label for="rss_uri"></label></th>
<td><input name="link_rss" class="code" type="text" id="rss_uri" maxlength="255" value="<?php echo ( isset( $link->link_rss ) ? esc_attr( $link->link_rss ) : '' ); ?>" /></td>
</tr>
<tr>
<th scope="row"><label for="link_notes"></label></th>
<td><textarea name="link_notes" id="link_notes" rows="10">link_notes ) ? $link->link_notes : '' ); // textarea_escaped ?></textarea></td>
</tr>
<tr>
<th scope="row"><label for="link_rating"></label></th>
<td><select name="link_rating" id="link_rating" size="1">
link_rating ) && $link->link_rating === $rating ) {
echo ' selected="selected"';
}
echo '>' . $rating . '</option>';
}
?>
</select>
</td>
</tr>
</table>
</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#L1444">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/meta-boxes.php#L1444-L1476">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/_e/">_e()</a><code>wp-includes/l10n.php
Displays translated text.
esc_attr()wp-includes/formatting.php
Escaping for HTML attributes.
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |