You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 13:05:15