WordPress前端提交插件textarea不支持换行及HTML的解决求助
WordPress Frontend Post Submissions Manager 自定义textarea换行/HTML支持方案
问题说明
使用Frontend Post Submissions Manager插件时,自定义textarea字段无法保留手动换行或HTML标签(如<br>、<p>),保存后内容会被清除。现有100+启用ACF的文章,无法替换为完整编辑器,需解决换行或HTML支持问题。
原字段代码:
<div class="fpsm-field-wrap"> <label>testlable</label> <div class="fpsm-field fpsm-custom-textarea"> <input type="hidden" name="fpsm_included_custom_fields[]" value="testvalue"> <textarea name="fpsm_custom_fields[local-area]">This is not working when i write line breaks and other html things like <br> <p> etc etc </textarea> </div> </div>
解决方法
方案1:仅实现换行(无需HTML)
- 保存阶段:保留原始换行符,在主题
functions.php中添加过滤钩子:add_filter('fpsm_custom_field_value_local-area', 'preserve_fpsm_line_breaks', 10, 1); function preserve_fpsm_line_breaks($value) { return wp_unslash($value); } - 输出阶段:使用WordPress内置函数
wpautop将换行转换为<p>或<br>标签:$local_area = get_post_meta(get_the_ID(), 'local-area', true); echo wpautop($local_area);
方案2:支持指定HTML标签
如果需要保留HTML格式,可通过过滤允许指定标签,在functions.php中添加:
add_filter('fpsm_custom_field_value_local-area', 'allow_fpsm_safe_html', 10, 1); function allow_fpsm_safe_html($value) { // 自定义允许的HTML标签,按需调整 $allowed_tags = [ 'br' => [], 'p' => [], 'strong' => [], 'em' => [], 'ul' => [], 'li' => [] ]; return wp_kses($value, $allowed_tags); }
- 输出时直接打印字段值,避免使用
esc_html转义:echo get_post_meta(get_the_ID(), 'local-area', true);
方案3:强制保留所有HTML(谨慎使用)
若插件强制清除所有HTML,可尝试解码原始内容(仅信任提交用户时使用,存在安全风险):
add_filter('fpsm_custom_field_value_local-area', 'decode_fpsm_raw_content', 10, 1); function decode_fpsm_raw_content($value) { return rawurldecode($value); }
内容的提问来源于stack exchange,提问作者Junologue
相关产品推荐
相关产品推荐

