清理书签字段。

原型

sanitize_bookmark_field( string $field, mixed $value, int $bookmark_id, string $context )

描述

根据字段名称清理书签字段。如果该字段具有严格的值集,则将对其进行测试,否则将应用更通用的过滤。应用更严格的过滤器后,如果$ context为’raw’,则立即返回该值。

参数

$field (string) (Required) 书签字段。

$value (mixed) (Required) 书签字段值。

$bookmark_id (int) (Required) 书签ID。

$context (string) (Required) 如何过滤字段值。接受’raw’,‘edit’,‘attribute’,‘js’,‘db’或’display’

返回值

(mixed)  过滤后的值。

源文件

路径:wp-includes/bookmark.php

<?php
...
function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) {
	switch ( $field ) {
	case 'link_id' : // ints
	case 'link_rating' :
		$value = (int) $value;
		break;
	case 'link_category' : // array( ints )
		$value = array_map('absint', (array) $value);
		// We return here so that the categories aren't filtered.
		// The 'link_category' filter is for the name of a link category, not an array of a link's link categories
		return $value;
	case 'link_visible' : // bool stored as Y|N
		$value = preg_replace('/[^YNyn]/', '', $value);
		break;
	case 'link_target' : // "enum"
		$targets = array('_top', '_blank');
		if ( ! in_array($value, $targets) )
			$value = '';
		break;
	}
	if ( 'raw' == $context )
		return $value;
	if ( 'edit' == $context ) {
		/** This filter is documented in wp-includes/post.php */
		$value = apply_filters( "edit_{$field}", $value, $bookmark_id );
		if ( 'link_notes' == $field ) {
			$value = esc_html( $value ); // textarea_escaped
		} else {
			$value = esc_attr($value);
		}
	} elseif ( 'db' == $context ) {
		/** This filter is documented in wp-includes/post.php */
		$value = apply_filters( "pre_{$field}", $value );
	} else {
		/** This filter is documented in wp-includes/post.php */
		$value = apply_filters( "{$field}", $value, $bookmark_id, $context );
		if ( 'attribute' == $context ) {
			$value = esc_attr( $value );
		} elseif ( 'js' == $context ) {
			$value = esc_js( $value );
		}
	}
	return $value;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/sanitize_bookmark_field/