php - 如何在 PHP 外部文件中调用过滤器钩子(Hook)

标签 php wordpress function filter hook

我正在尝试在 Wordpress 的独立 PHP 文件中调用 filter Hook 。

这是文件的代码:my_external_file.php:

<?php
require( dirname(__FILE__) . '/../../../../../../../wp-load.php');

add_filter('init', 'test_function');

function test_function (){
    global $global_text_to_shown;

    $global_text_to_shown = 'Hello World';

}

global $global_text_to_shown;

$quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close' );

//This work fine, shown editor good.
wp_editor( $global_text_to_show, 'content', array( 'media_buttons' => false, 'tinymce' => true, 'quicktags' => $quicktags_settings ) );

//Load js and work fine the editor - wp_editor function.
wp_footer();

?>

问题是过滤器没有被执行,因此函数没有被执行。

如何在此外部 PHP 文件上执行过滤器 Hook ?

最佳答案

第一个也是主要问题$global_text_to_show不是$global_text_to_show<strike>n</strike> .

钩子(Hook)init不是过滤器,而是操作: add_action('init', 'test_function'); 。请参阅Actions and Filters are not the same thing .

正在加载wp-load.php这条路是... crappy code ;)参见Wordpress header external php file - change title? .

第二个主要问题是为什么为什么你需要这个?
无论如何,init使用过滤器 the_editor_content 将不起作用将要。虽然我不明白目标:

<?php
define( 'WP_USE_THEMES', false );
require( $_SERVER['DOCUMENT_ROOT'] .'/wp-load.php' );

// Requires PHP 5.3. Create a normal function to use in PHP 5.2.
add_filter( 'the_editor_content', function(){
    return 'Hello World';
});

$quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close' );

?><!DOCTYPE html>
<html>
<head>
<?php wp_head(); ?>
</head>
<body>
<?php 
    wp_editor( 
        '', 
        'content', 
        array( 
            'media_buttons' => false, 
            'tinymce' => true, 
            'quicktags' => $quicktags_settings 
        ) 
    );
    wp_footer();
?>
</body>
</html>

关于php - 如何在 PHP 外部文件中调用过滤器钩子(Hook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18512186/

相关文章:

php - WordPress 一键发送多个 AJAX 请求

php - 关于PHP函数返回值的问题

php - 带ajax调用和php功能的Wordpress投票系统无法正常工作

php - WordPress 自定义页面模板和 WP-API

php - javascript可逆伪散列

javascript - 从图表中的数据库中选择不更新值 php

php - 广告 block 无法正确放置

javascript - 解构参数对象,但让它在主体函数中可用?

javascript - node.js:console.log() 是一个函数吗?

java - Java Servlet 是否等同于 Web 服务