我想在WordPress主题中使用HTML5,如何关闭wptexturize?
我不介意WP添加中断,但是我希望它们成为,<br>
而不是<br />
。如何控制这些中断在代码中的显示方式?
编辑:我只真正关心<br>
标签问题,我不介意它所做的印刷更改。
EDIT2:实际上,我猜<img>
标记也很重要。任何自动关闭的独立标签在这里都很重要。因此,<hr>
可能也是一个问题。更不用说诸如各种标签之类的wp_head()
物品了。<link>
<meta>
我想在WordPress主题中使用HTML5,如何关闭wptexturize?
我不介意WP添加中断,但是我希望它们成为,<br>
而不是<br />
。如何控制这些中断在代码中的显示方式?
编辑:我只真正关心<br>
标签问题,我不介意它所做的印刷更改。
EDIT2:实际上,我猜<img>
标记也很重要。任何自动关闭的独立标签在这里都很重要。因此,<hr>
可能也是一个问题。更不用说诸如各种标签之类的wp_head()
物品了。<link>
<meta>
Answers:
换行符由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
文件就像一个插件文件。其中的任何代码将自动执行。只要有效的PHP,它在哪里都没有关系。
干得好:
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更快地完成了它。
刚刚找到它;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
可以通过使用功能(http://codex.wordpress.org/Function_Reference/remove_filter)在主题的function.php文件中禁用此remove_filter()
功能
remove_filter("the_content", "wptexturize");
wptexturize()
,我无法找到任何其他可行的解决方案。
<br />
-替换为<br>
?
我有一个适用于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上了解更多,而不是框架!
禁用WPtexturize插件对我有用:禁用WPtexturize
不过,这很简单:
remove_filter('the_content', 'wptexturize');
remove_filter('the_excerpt', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
<br />
?