【问题标题】:Remove automatic p tag in WordPress删除 WordPress 中的自动 p 标签
【发布时间】:2017-06-14 08:49:02
【问题描述】:

首先,我已经找到了一种方法来做到这一点。但它仍然发生。

所以我使用 Creative 主题,我有一个联系表单,使用 Contact Form 7 完成。要在正确的位置显示它,它需要放在一个小部件区域中。

我已将表单的简码插入到文本小部件、简码小部件以及 Contact Form 7 小部件中。

对于每种情况,输入和标签都包装在p 标签中,我猜它会破坏提交功能。

所以我尝试了一些方法来禁用此功能。

在我的function.php中,最后:

//* Disable automatic p tag insertion
remove_filter( 'the_content', 'wpautop' );
remove_filter('the_excerpt', 'wpautop');
add_filter( 'the_content', 'disable_wpautop_cpt', 0 );
function disable_wpautop_cpt( $content ) {
  'your_cpt_slug' === get_post_type() && remove_filter( 'the_content', 'wpautop' );
return $content;
}

在我的 wp-config.php 中,最后也是:

/** Disable automatic p tag insertion */
define( 'WPCF7_AUTOP', false );

另外,我在表单所在的页面添加了一个插件来禁用WP的这个功能。

但标签仍然存在。帮助

编辑:添加屏幕。

Form code

HTML in inspector

【问题讨论】:

  • 在联系表格 7 中,您想删除 p 标签。为此,您只需从联系表格的 From 部分中删除 p 标签 7
  • @AoNoLoki -- 你找到好的解决方案了吗?

标签: wordpress


【解决方案1】:

在你的functions.php文件中添加这个

 function reformat_auto_p_tags($content) {
        $new_content = '';
        $pattern_full = '{(\[raw\].*?\[/raw\])}is';
        $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
        $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
        foreach ($pieces as $piece) {
            if (preg_match($pattern_contents, $piece, $matches)) {
                $new_content .= $matches[1];
            } else {
                $new_content .= wptexturize(wpautop($piece));
            }
        }

        return $new_content;
    }

    remove_filter('the_content', 'wpautop');
    remove_filter('the_content', 'wptexturize');

    add_filter('the_content', 'reformat_auto_p_tags', 99);
    add_filter('widget_text', 'reformat_auto_p_tags', 99);

然后在您的帖子编辑器上用原始简码包装您的联系表格 7 简码 例如

[raw][contact-form-7 id="1" title="Contact Us"][/raw]

【讨论】:

  • 我要添加一个 p 标签位置的屏幕,格式为
  • 我的表单中没有p标签,但是WordPress会在显示代码时添加它们
  • 你能分享你的网站来检查我吗
  • 该网站目前处于开发阶段,尚未发布,抱歉
  • 不起作用,即使所有解决方案都处于活动状态,也会添加 p 标签
【解决方案2】:

使用 wpautop 过滤器删除内容或摘录中的自动 p 标签

<?php wpautop( $foo, $br ); ?> 

例子:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

更多参考:https://codex.wordpress.org/Function_Reference/wpautop

【讨论】:

    【解决方案3】:

    你也可以使用 JS。以上代码将帮助您从页面中删除所有空的 p 标签

    jQuery('p:empty').remove();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 2014-05-07
      • 2021-07-23
      • 1970-01-01
      相关资源
      最近更新 更多