Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自定义wp_strip_all_tags()WordPress函数_Php_Wordpress - Fatal编程技术网

Php 自定义wp_strip_all_tags()WordPress函数

Php 自定义wp_strip_all_tags()WordPress函数,php,wordpress,Php,Wordpress,我想在我的WordPress摘录中加入换行符 要实现这一点,我可以更改此功能: function wp_strip_all_tags($string, $remove_breaks = false) { $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string ); $string = strip_tags($string); if ( $remove_brea

我想在我的WordPress摘录中加入换行符

要实现这一点,我可以更改此功能:

function wp_strip_all_tags($string, $remove_breaks = false) {
  $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
  $string = strip_tags($string);

  if ( $remove_breaks )
    $string = preg_replace('/[\r\n\t ]+/', ' ', $string);

  return trim( $string );
}
函数wp\u strip\u all\u标记($string,$remove\u breaks=false){
$string=preg_replace('@]*?>.@si',''$string);
$string=带标签($string);
如果($remove_breaks)
$string=preg_replace('/[\r\n\t]+/',''$string);
返回修剪($string);
}
致:

函数wp\u strip\u all\u tags\u breaks($string,$remove\u breaks=false){
$string=preg_replace('@]*?>.@si',''$string);
$string=strip_标签($string,);
如果($remove_breaks)
$string=preg_replace('/[\r\n\t]+/',''$string);
返回修剪($string);
}

修改我的主题以切换函数并提供此功能的最佳方法是什么?

必须在当前主题的
functions.php
中重写/重载任何WordPress核心函数

首先必须在
functions.php
中定义新函数(名称应不同于原始wpcore函数名称),然后删除旧函数并将新函数添加到相应的钩子/过滤器中

如果是
摘录()
,应该这样做:

function new_function() {
    //code here
}

remove_filter('get_the_excerpt', 'old_function');
add_filter('get_the_excerpt', 'new_function');
希望这是有道理的


编辑:是一个很好的教程,介绍如何编辑
摘录()
格式。

所有标签的
wp\u strip\u来自哪里?它看起来像是在尝试清理HTML,但失败得很惨……它看起来像是在获取\u the_extract->wp_trim_extract->wp_trim_words->wp_strip_all_tags->strip_tags,其中摘录会丢失换行符(wp includes/default-filters.php&wp includes/formatting.php)。我看不到更好的方法来将段落修改回摘录,而不复制一堆代码并将其全部放入主题函数中。php.Hmm,我的文本编辑器中有类似的内容,但不太确定在代码中添加一个
,“”
修改所需的最小调整量。理想情况下,我只需要在现有代码中添加大约6个字符即可使其正常工作。我想我可以将
wp\u trim\u extract()
复制/粘贴到一个新函数中,该函数使用修改后的
wp\u trim\u words()
(另一个复制/粘贴),然后使用我的自定义
wp\u strip\u all\u tags\u breaks()
函数(另一个复制/粘贴),然后在
获取theme的functions.php文件中的\u extract上使用remove/add\u过滤器,但是,这有很多冗余。所以,我将其添加到我的主题的functions.php文件中。可以,但是,它看起来很粗糙。
function new_function() {
    //code here
}

remove_filter('get_the_excerpt', 'old_function');
add_filter('get_the_excerpt', 'new_function');