Php 在Wordpress核心中进行更改的正确方法
我最近从Wordpress中删除了摘录功能,并创建了自己的功能,以便允许在摘录中显示一些HTML。我在子主题函数文件中进行了更改 它在前端工作正常,但在那之后,我在Wordpress的后端出现了问题。在Php 在Wordpress核心中进行更改的正确方法,php,wordpress,Php,Wordpress,我最近从Wordpress中删除了摘录功能,并创建了自己的功能,以便允许在摘录中显示一些HTML。我在子主题函数文件中进行了更改 它在前端工作正常,但在那之后,我在Wordpress的后端出现了问题。在/wp admin/edit.php的一篇文章摘录中,我得到了包含所有HTML标记的文章摘录,因此很难阅读文章的实际内容 为了修复这个问题,我在Wordpress核心文件/wp admin/includes/class wp posts list table.php中做了一个更改。 我删除了第10
/wp admin/edit.php
的一篇文章摘录中,我得到了包含所有HTML标记的文章摘录,因此很难阅读文章的实际内容
为了修复这个问题,我在Wordpress核心文件/wp admin/includes/class wp posts list table.php
中做了一个更改。
我删除了第1037行的内容
echo esc_html( get_the_excerpt() );
函数esc\u html
如何使此更改永久化,以便在可能的Wordpress更新后,更改不会丢失
这个零钱安全吗?我的Wordpress后端会有很多用户
更新我的问题
add_filter('get_the_extract'、'my_clean_extract');
函数wpse_allowedtags(){
//将自定义标记添加到此字符串
返回“,
,,,- ,”;
$extract\u more=应用过滤器('extract\u more',''.$extract\u end);
//$pos=strrpos($wpse_节选,',
“<?xml encoding=“utf-8””,
);
foreach($badword作为$badword){
$wpse_extract=str_replace($badword,,$wpse_extract);
}
//端部加丝机
返回$wpse_摘录;
}
返回apply_filters('wpse_custom_wp_trim_Except',$wpse_Except,$raw_Except);
}
endif;
移除过滤器(“获取摘录”、“wp修剪摘录”);
添加过滤器('get_the_extract'、'wpse_custom_wp_trim_extract');
我使用这个函数在摘录中添加html标记
我更新了数据库中的摘录。我得到了我想要的结果
但我在后端得到这个。
因此,解决这个问题的一个快速方法是转到/wp admin/includes/class wp posts list table.php
并从这行echo esc_html(获取_摘录())中删除函数esc_html
代码>为后端生成摘录的位置
所以我的问题不是如何在摘录中允许html标记,我已经做到了,而是如何使这种更改永久化
我的后端在那次改变之后。
这就是我想做的。永远不要改变WP核心。。。您将失去更新它的能力,并且您的系统将易受攻击
您可以创建插件来更改摘录行为,您可以创建自己的函数并在主题模板中使用它
有很多方法比永远不要改变WordPress的核心要好。正如Artur Luiz Oliveira提到的,WordPress第一次更新时,您将丢失这些更改,您应该知道,一旦版本变得非常过时,无论您喜欢与否,它都会自动丢失这些更改
要做你想做的事,我建议你调查一下
可能会涉及更多的编码,但这会持续下去,并且在WordPress版本更新时不会受到影响。通过制作插件,您可能会在此处获得更好的帮助,以满足您的确切需求:()并检查此项()寻求调试帮助的问题(“此代码为什么不起作用?”)必须包括所需的行为,一个特定的问题或错误,以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请看:如何创建一个最小的、可复制的示例。我更新了我的问题,现在可能更清楚了。你是对的,我如何在文件中创建一个插件来更改我的示例,我是初学者。首先,你应该去Wordpress Codex,阅读关于插件、挂钩和你想要更改的整体功能的内容。在那之后,它基本上是一个普通的PHP开发项目。谢谢你的建议,在你的回答中你说我将失去更新的能力。你的意思是,Wordpress不会自动更新,因为我已经更改了核心;或者你说我不会更新,所以我不会丢失我所做的更改;第二个选项,如果你更新你的WP,你所有的改变都会消失。
add_filter( 'get_the_excerpt', 'my_clean_excerpt' );
function wpse_allowedtags() {
// Add custom tags to this string
return '<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>,<div>,<wbr>';
}
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :
function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
$raw_excerpt = $wpse_excerpt;
if ( '' == $wpse_excerpt ) {
$wpse_excerpt = get_the_content('');
$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
//$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 75;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput = '';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_length && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
// Limit reached, continue until , ; ? . or ! occur at the end
$excerptOutput .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what's left of the token
$excerptOutput .= $token;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . ' » ' . sprintf(__( 'Read more about: %s »', 'wpse' ), get_the_title()) . '</a>';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
//else
// After the content
//$wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
// Extra filter to remove the above text from excerpt
$badwords = array(
'< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">',
'< ?xml encoding="utf-8" ?>',
);
foreach ( $badwords as $badword ) {
$wpse_excerpt = str_replace( $badword, '', $wpse_excerpt);
}
//End extra filrer
return $wpse_excerpt;
}
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');