自定义wp_strip_all_tags()WordPress函数

时间:2013-06-30 05:00:02

标签: 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_breaks )
    $string = preg_replace('/[\r\n\t ]+/', ' ', $string);

  return trim( $string );
}

为:

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

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

  return trim( $string );
}

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

1 个答案:

答案 0 :(得分:1)

覆盖/重载任何WordPress核心功能必须在当前主题的functions.php中完成。

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

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

function new_function() {
    //code here
}

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

希望这是有道理的。

编辑:Here是有关如何修改the_excerpt()格式的好教程。