函数文档

edit_term_link()

💡 云策文档标注

概述

edit_term_link() 函数用于显示或检索分类法术语的编辑链接,支持自定义锚文本和前后缀。它检查用户权限并返回格式化后的 HTML 链接。

关键要点

  • 函数参数包括可选的锚文本($link)、前后缀($before, $after)、术语对象或 ID($term)以及是否直接输出($display)。
  • 默认锚文本为 'Edit This',若 $term 为 null 则自动从查询对象获取。
  • 通过 current_user_can('edit_term', $term->term_id) 检查用户编辑权限,无权限时返回空。
  • 使用 apply_filters('edit_term_link', $link, $term->term_id) 钩子允许过滤链接锚标签。
  • 返回类型为 string 或 void,取决于 $display 参数设置。

代码示例

$term = get_term_by('slug', 'uncategorized', 'category');
edit_term_link(__( 'Edit', 'textdomain' ), '', '', $term);

📄 原文内容

Displays or retrieves the edit term link with formatting.

Parameters

$linkstringoptional
Anchor text. If empty, default is ‘Edit This’. Default empty.
$beforestringoptional
Display before edit link. Default empty.
$afterstringoptional
Display after edit link. Default empty.
$termint|WP_Term|nulloptional
Term ID or object. If null, the queried object will be inspected.

Default:null

$displaybooloptional
Whether or not to echo the return.

Default:true

Return

string|void HTML content.

Source

function edit_term_link( $link = '', $before = '', $after = '', $term = null, $display = true ) {
	if ( is_null( $term ) ) {
		$term = get_queried_object();
	} else {
		$term = get_term( $term );
	}

	if ( ! $term ) {
		return;
	}

	$tax = get_taxonomy( $term->taxonomy );
	if ( ! current_user_can( 'edit_term', $term->term_id ) ) {
		return;
	}

	if ( empty( $link ) ) {
		$link = __( 'Edit This' );
	}

	$link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';

	/**
	 * Filters the anchor tag for the edit link of a term.
	 *
	 * @since 3.1.0
	 *
	 * @param string $link    The anchor tag for the edit link.
	 * @param int    $term_id Term ID.
	 */
	$link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;

	if ( $display ) {
		echo $link;
	} else {
		return $link;
	}
}

Hooks

apply_filters( ‘edit_term_link’, string $link, int $term_id )

Filters the anchor tag for the edit link of a term.

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes