Php 在我的插件中,我想更改函数内容

Php 在我的插件中,我想更改函数内容,php,wordpress,Php,Wordpress,我想更改驻留在/wp includes/post-template.php中的内容 function the_content($more_link_text = null, $stripteaser = false) { $content = get_the_content($more_link_text, $stripteaser); $content = apply_filters('the_content', $content); $content = str_re

我想更改驻留在/wp includes/post-template.php中的内容

function the_content($more_link_text = null, $stripteaser = false) {
    $content = get_the_content($more_link_text, $stripteaser);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    echo $content;
}
进入


我如何在我的插件中实现这一点而不接触wordpress代码(保持代码升级兼容)?我知道某些功能是可以替换的,但这一个?

Wordpress有一个核心功能列表,主题和插件可能会被过度使用;它们被称为可插拔函数:

目录()不在该列表中,因此不能直接替换


如果你不想编辑WordPress代码,没有简单的方法。唯一的选择是创建函数的本地副本(称为myTheme The_content()
,或类似的东西),并确保更改主题中的引用以调用该函数。

答案在WordPress代码本身中:
$content=apply_filters('The_content',$content')。看

您可以挂接到该筛选器并修改内容:

<?php
/* Plugin Name: Modify Content */

add_filter( 'the_content', 'mod_content_so_17749899' );
function mod_content_so_17749899( $content )
{
    // http://codex.wordpress.org/Conditional_Tags
    if( is_admin() ) // Prevent this hook in admin area
        return $content;

    // Manipulate $content
    return $content;
}

我很好奇,这对你来说是个问题吗?我试着记住它的用途…是的。我在wp\u head中注入了一个google跟踪标记,它包含一个CDATA结束标记,该标记被th str\u replace函数替换。CDATA和
wp\u head
详细信息应该在问题中。
与内容无关,是吗?您的代码不起作用,因为主功能中的str\u replace过滤器应用于建议的过滤器之后。这就是我需要替换函数的原因。@AleV,当然可以。我觉得自己忽略了一些东西。我还发现,在我的wp_head或woocommerce_thankyou函数(但在footer.php中)中根本没有考虑这个添加的_过滤器。困惑的
<?php
/* Plugin Name: Modify Content */

add_filter( 'the_content', 'mod_content_so_17749899' );
function mod_content_so_17749899( $content )
{
    // http://codex.wordpress.org/Conditional_Tags
    if( is_admin() ) // Prevent this hook in admin area
        return $content;

    // Manipulate $content
    return $content;
}