如何关闭WordPress中标记的自动关闭标签(例如,对于HTML5或HTML4)?


18

我想在WordPress主题中使用HTML5,如何关闭wptexturize?

我不介意WP添加中断,但是我希望它们成为,<br>而不是<br />。如何控制这些中断在代码中的显示方式?

编辑:我只真正关心<br>标签问题,我不介意它所做的印刷更改。

EDIT2:实际上,我猜<img>标记也很重要。任何自动关闭的独立标签在这里都很重要。因此,<hr>可能也是一个问题。更不用说诸如各种标签之类的wp_head()物品了。<link><meta>


1
怎么了<br />
Scott M.

2
很好,但是如果我想使用HTML5的非XML版本,则不需要XML样式的/>结尾。
artlung 2010年

我以为<br />是有效的html和xhtml?什么时候不?
瑞安·吉本斯

5
我相信这个问题极具误导性。wptexturize不会以任何方式阻止网站符合HTML 5。
瑞安·吉本斯

2
有人可以按照“如何从Wordpress生成的标记中的自闭合元素中删除斜杠”的标题重新命名吗?
鲍比·杰克

Answers:


21

换行符由wpautop()而不是加上wptexturize()wpautop()也是自动添加段落标签的功能。

<br />替换过滤器相比,最好修复。由于wpautop()运行优先级为10,因此您可以在此之后挂接并进行修复。

add_filter( 'the_content', 'html5_line_breaks', 25 );

function html5_line_breaks( $content ) {
    return str_replace( '<br />', '<br>', $content );
}

OP更新后进行编辑:

WordPress函数旨在输出XHTML。为了消除站点范围内的尾部斜杠,您将必须使用输出缓冲区。您可以使用与上述过滤器类似的过滤器来替换帖子内容中的斜杠,但这不会引起您的头,侧边栏等。

这有点丑陋,可能会对性能产生小的影响,但是您可以将其放到这里(将其放入插件或主题functions.php文件中):

if ( !is_admin() && ( ! defined('DOING_AJAX') || ( defined('DOING_AJAX') && ! DOING_AJAX ) ) ) {
    ob_start( 'html5_slash_fixer' );
    add_action( 'shutdown', 'html5_slash_fixer_flush' );
}

function html5_slash_fixer( $buffer ) {
    return str_replace( ' />', '>', $buffer );
}

function html5_slash_fixer_flush() {
    ob_end_flush();
}

该代码表明,如果您不在管理区域中并且未进行AJAX请求处理,则开始通过过滤器缓冲输出,然后使用WordPress shutdown钩子输出该缓冲。


我还没有时间破解开放的functions.php,但是您能详细说明一下如果块去了那儿吗?一旦我有机会打开该文件,这也许很明显,但是我认为我会解决这个问题。
托马斯·欧文斯

@Thomas:您主题的functions.php文件就像一个插件文件。其中的任何代码将自动执行。只要有效的PHP,它在哪里都没有关系。
Viper007Bond

啊。有趣。我是WordPress开发的新手,所以我仍然学到很多东西。感谢您清理。
Thomas Owens

8

干得好:

function my_awesome_tag_fixer( $input ){
  return preg_replace( '/(<.+)\s\/>/', '$1>', $input );
}

foreach( array('the_content', 'the_excerpt', 'comment_text') as $filter )
  add_filter( $filter, 'my_awesome_tag_fixer', 12 );

这不是最优雅的解决方案,但是它比重写wpautop和wptexturize更快地完成了它。


1
+1我对HTML 4.01严格合规做了非常相似的事情。
Trevor Bramble

7

刚刚找到它;void元素上的自动关闭标签是有效的html。

In HTML5 we've allowed the / on void elements (like <meta>, <img>, <br>, <input>, etc), to ease migration to and from XML.

http://lists.whatwg.org/pipermail/help-whatwg.org/2008-August/000137.html

更多信息:

http://wiki.whatwg.org/wiki/FAQ#Should_I_close_empty_elements_with_.2F.3E_or_.3E.3F


1
“但是,由于广泛使用XHTML1的尝试,大量页面使用尾部斜杠。因此,为了简化从XHTML1到HTML5的迁移,在HTML的空白元素中允许使用尾部斜杠语法。 ” 允许作为遗产。我认为前进的方向是放弃多余的“ /”,因此是我的问题。我认为WordPress需要允许创建xhtml或html4.01或html5的代码的选项。
artlung 2010年

这就是您正在读的内容的问题。斜杠是允许的,这意味着它是有效的语法。您正在猜测它将被删除?为什么要猜测标准的去向,并为解决不存在的问题而开展工作?
瑞安·吉本斯

我什么都没猜测。我什么都没猜到。规范不需要/标记,我希望可以选择在WordPress中将其删除。
artlung 2010年

我喜欢XHTML,我们是否必须回到旧HTML的混乱外观。
阿伦·贝勒

Arlen,我喜欢井井有条的html,我喜欢xhtml。我是验证者的忠实粉丝。我已经编写了自己的文档类型来对自己的代码进行验证。我用过html 3.2,html 4、4.01,甚至html 2.0!lab.artlung.com/html-2.0-但我想选择在WordPress中删除自动关闭标签。似乎应该没什么大不了的。我觉得与我的问题的前提争论是非常无益的。
artlung 2010年

6

可以通过使用功能(http://codex.wordpress.org/Function_Reference/remove_filter)在主题的function.php文件中禁用此remove_filter()功能

remove_filter("the_content", "wptexturize");

1
我可以对此进行更详细的控制吗?如果这样做,我会不会失去印刷标记?
artlung 2010年

我不知道有什么简单的方法可以解决,但是让我看看我能为您找到什么。
thomasjo

没有再现所需的功能wptexturize(),我无法找到任何其他可行的解决方案。
thomasjo

我想知道是否有一种方法可以简单地将<br />-替换为<br>
artlung 2010年

5

我有一个适用于html5和WordPress的入门主题,还有一个不是wptexturize的函数,而是wpautop()的函数。还包括html的其他元素,例如thead,tfoot,aside,并使用html5的语法,例如
and

/**
 * wpautop for HTML5, allowed: table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|header|footer|hgroup|figure|details|figcaption|summary)
 * @link http://nicolasgallagher.com/using-html5-elements-in-wordpress-post-content/
 * @author nicolas@nicolasgallagher.com
 */
function html5wpautop($pee, $br = 1) {
    if ( trim($pee) === '' )
            return '';

    $pee = $pee . "\n"; // just to make things a little easier, pad the end
    $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
    // Space things out a little
    // *insertion* of section|article|aside|header|footer|hgroup|figure|details|figcaption|summary
    $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend|section|article|aside|header|footer|hgroup|figure|details|figcaption|summary)';
    $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
    $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    if ( strpos($pee, '<object') !== false ) {
            $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
            $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
    }
    $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
    // make paragraphs, including one at the end
    $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
    $pee = '';
    foreach ( $pees as $tinkle )
            $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
    $pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
    // *insertion* of section|article|aside
    $pee = preg_replace('!<p>([^<]+)</(div|address|form|section|article|aside)>!', "<p>$1</p></$2>", $pee);
    $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
    $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
    $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
    $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
    $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
    if ($br) {
            $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
            $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
            $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
    }
    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
    // *insertion* of img|figcaption|summary
    $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol|img|figcaption|summary)[^>]*>)!', '$1', $pee);
    if (strpos($pee, '<pre') !== false)
            $pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee );
    $pee = preg_replace( "|\n</p>$|", '</p>', $pee );

    return $pee;
}

// remove the original wpautop function
remove_filter('the_excerpt', 'wpautop');
remove_filter('the_content', 'wpautop');

// add our new html5autop function
add_filter('the_excerpt', 'html5wpautop');
add_filter('the_content', 'html5wpautop');

在html5入门主题的svn上了解更多,而不是框架!


3

禁用WPtexturize插件对我有用禁用WPtexturize

不过,这很简单:

remove_filter('the_content', 'wptexturize');
remove_filter('the_excerpt', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.