函数文档

wp_get_comment_fields_max_lengths()

💡 云策文档标注

概述

wp_get_comment_fields_max_lengths() 函数用于获取评论表单字段的最大字符长度。它返回一个以字段名作为键、最大长度作为值的数组,并支持通过数据库查询动态调整默认值。

关键要点

  • 函数返回一个关联数组,包含 comment_author、comment_author_email、comment_author_url 和 comment_content 字段的默认最大长度。
  • 如果数据库是 MySQL,函数会通过 wpdb::get_col_length() 查询实际列长度来覆盖默认值,并考虑字节类型调整。
  • 可通过 apply_filters('wp_get_comment_fields_max_lengths', $lengths) 钩子过滤返回的长度数组。

代码示例

function wp_get_comment_fields_max_lengths() {
    global $wpdb;

    $lengths = array(
        'comment_author'       => 245,
        'comment_author_email' => 100,
        'comment_author_url'   => 200,
        'comment_content'      => 65525,
    );

    if ( $wpdb->is_mysql ) {
        foreach ( $lengths as $column => $length ) {
            $col_length = $wpdb->get_col_length( $wpdb->comments, $column );
            $max_length = 0;

            if ( is_wp_error( $col_length ) ) {
                break;
            }

            if ( ! is_array( $col_length ) && (int) $col_length > 0 ) {
                $max_length = (int) $col_length;
            } elseif ( is_array( $col_length ) && isset( $col_length['length'] ) && (int) $col_length['length'] > 0 ) {
                $max_length = (int) $col_length['length'];

                if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) {
                    $max_length = $max_length - 10;
                }
            }

            if ( $max_length > 0 ) {
                $lengths[ $column ] = $max_length;
            }
        }
    }

    return apply_filters( 'wp_get_comment_fields_max_lengths', $lengths );
}

注意事项

  • 函数在 WordPress 4.5.0 版本中引入,主要用于 wp_check_comment_data_max_lengths() 等内部函数。
  • 默认长度值基于数据库列设计,但实际使用时可能受数据库配置影响,建议通过过滤器进行自定义调整。

📄 原文内容

Retrieves the maximum character lengths for the comment form fields.

Return

int[] Array of maximum lengths keyed by field name.

Source

function wp_get_comment_fields_max_lengths() {
	global $wpdb;

	$lengths = array(
		'comment_author'       => 245,
		'comment_author_email' => 100,
		'comment_author_url'   => 200,
		'comment_content'      => 65525,
	);

	if ( $wpdb->is_mysql ) {
		foreach ( $lengths as $column => $length ) {
			$col_length = $wpdb->get_col_length( $wpdb->comments, $column );
			$max_length = 0;

			// No point if we can't get the DB column lengths.
			if ( is_wp_error( $col_length ) ) {
				break;
			}

			if ( ! is_array( $col_length ) && (int) $col_length > 0 ) {
				$max_length = (int) $col_length;
			} elseif ( is_array( $col_length ) && isset( $col_length['length'] ) && (int) $col_length['length'] > 0 ) {
				$max_length = (int) $col_length['length'];

				if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) {
					$max_length = $max_length - 10;
				}
			}

			if ( $max_length > 0 ) {
				$lengths[ $column ] = $max_length;
			}
		}
	}

	/**
	 * Filters the lengths for the comment form fields.
	 *
	 * @since 4.5.0
	 *
	 * @param int[] $lengths Array of maximum lengths keyed by field name.
	 */
	return apply_filters( 'wp_get_comment_fields_max_lengths', $lengths );
}

Hooks

apply_filters( ‘wp_get_comment_fields_max_lengths’, int[] $lengths )

Filters the lengths for the comment form fields.

Changelog

Version Description
4.5.0 Introduced.