Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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

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 在页面显示之前获取页面内容_Php_Wordpress_Yoothemes - Fatal编程技术网

Php 在页面显示之前获取页面内容

Php 在页面显示之前获取页面内容,php,wordpress,yoothemes,Php,Wordpress,Yoothemes,我正在为wordpress编写一个简单的插件,它可以更改页面或帖子上的单个单词,使其变为粗体 例如:vlbs->vlbs 它适用于具有以下代码的普通Wordpress页面和帖子: defined('ABSPATH') or die('You can\'t enter this site'); class VLBS { function __construct() { } function activate() { flush_rewrite_rul

我正在为wordpress编写一个简单的插件,它可以更改页面或帖子上的单个单词,使其变为粗体

例如:vlbs->vlbs

它适用于具有以下代码的普通Wordpress页面和帖子:

defined('ABSPATH') or die('You can\'t enter this site');

class VLBS {

    function __construct() {

    }

    function activate() {
        flush_rewrite_rules();
    }

    function deactivate() {
        flush_rewrite_rules();
    }

    function unstinstall() {
    }

function new_content($content) {
                return $content = str_replace('vlbs','<strong style="color:#00a500">vlbs</strong>', $content);    
}

}

if(class_exists('VLBS')){
    $VLBS = new VLBS();

}

add_filter('the_content', array($VLBS, 'new_content'));

//activation
register_activation_hook(__FILE__, array($VLBS, 'activate'));

//deactivation
register_deactivation_hook(__FILE__, array($VLBS, 'deactivate'));
defined('ABSPATH')或die('You't not enter this site');
类VLB{
函数_u构造(){
}
函数激活(){
刷新或重写规则();
}
函数停用(){
刷新或重写规则();
}
函数unstall(){
}
函数新内容($content){
返回$content=str_replace('vlbs','vlbs,$content);
}
}
如果(类_存在('VLB')){
$VLBS=新VLBS();
}
添加_过滤器(“_内容”,数组($VLB,“新_内容”);
//活化
寄存器激活钩子(uuu文件,数组($VLBS,'activate'));
//失活
寄存器停用挂钩(uuu文件,数组($VLB,'deactivate');
但是,它在使用Yootheme Pro Pagebuilder构建的页面上不起作用。在函数new_content()中执行的任何操作都将在加载内容后进行处理。因此,在它显示给用户之前,我无法对其进行操作

因此,问题是:如何在页面显示之前获取页面内容?是否有与Wordpress“内容”相同的版本?

非常感谢您的帮助!事先非常感谢

致意 费边

约瑟姆:1.22.5
Wordpress:5.2.4
PHP:7.3

浏览器:在Chrome、Firefox、Edge、Internet Explorer上进行了测试。在您的代码中,您确定这是添加过滤器内容的良好用法吗

,第二个参数是字符串,而不是数组:

add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );

function filter_the_content_in_the_main_loop( $content ) {

    // Check if we're inside the main loop in a single post page.
    if ( is_single() && in_the_loop() && is_main_query() ) {
        return $content . esc_html__("I'm filtering the content inside the main loop", "my-textdomain");
    }

    return $content;
}
在wordpress中,函数显示内容。还有另外一个功能

转到页面文件并获取内容。之后,您可以使用str_替换和回显新内容

示例single.php:

if ( $query->have_posts() ) :               
    while( $query->have_posts() ) : $query->the_post();

        $content = get_the_content();

        $new_content = str_replace(search, replace, $content);

        echo $new_content;

    endwhile;
endif;
如果不可能,请尝试使用这些功能。如果你需要使用这个函数,我会说出来,我会在这一部分做更多的开发。但是,请在之前测试上述解决方案


哦,WP有一个特别的社区,您的问题将更加相关:

您好,欢迎光临。看看函数:)。和
ob_get_content()
ob_end_clean()谢谢你的热情欢迎:)嘿,谢谢你的帮助。非常感谢。我测试了你推荐的所有解决方案。我必须直接编辑themes page.php以使其正常工作。这可能只是一个临时解决办法,因为其他已安装的插件(例如,membersarea插件无法阻止对使用yootheme pro构建的页面的访问,因为它只能在内容已经显示后执行)也不起作用。然而,对于这个特殊的问题,这是正确的答案。所以非常感谢:)