如何在PHP外部文件中调用过滤器钩子 [英] How to call a filter hook in a PHP external file
问题描述
我试图在Wordpress的独立 PHP 文件中调用过滤器
挂钩。
这是文件的代码: my_external_file.php :
< ?php
require(dirname(__ FILE__)。'/../../../../../../../wp-load.php');
add_filter('init','test_function');
函数test_function(){
global $ global_text_to_shown;
$ global_text_to_shown ='Hello World';
}
全球$ global_text_to_shown;
$ quicktags_settings = array('buttons'=>'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close');
//这个工作很好,显示编辑器很好。
wp_editor($ global_text_to_show,'content',array('media_buttons'=> false,'tinymce'=> true,'quicktags'=> $ quicktags_settings));
//载入js并正常工作编辑器 - wp_editor函数。
wp_footer();
?>
问题是过滤器没有被执行,因此函数没有被执行。 / p>
如何在这个外部 PHP 文件上执行过滤器钩子?
第一个和主要问题 $ global_text_to_show
不是 $ global_text_to_show
$ b n
钩子 init
不是过滤器,它是一个动作: add_action('init','test_function');
。请参阅动作和过滤器不是同一件事。
加载 wp-load.php
这种方式是... 蹩脚的代码;)请参阅 Wordpress外部php文件 - 更改标题?。
第二个主要问题是 和 你需要这个吗?
无论如何, init
将不起作用,使用过滤器 the_editor_content
will。虽然我不明白这个目标:
<?php
define('WP_USE_THEMES',false);
require($ _SERVER ['DOCUMENT_ROOT']。'/ wp-load.php');
//需要PHP 5.3。创建一个在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>
I'm trying to call a filter
hook in a standalone PHP file in Wordpress.
This is the code of the file: 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();
?>
The issue is that the filter isn't get executed hence the function doesn't get executed.
How can I execute the filter hook on this external PHP file?
First and main problem $global_text_to_show
is not $global_text_to_show
.n
The hook init
is not a filter, it's an action: add_action('init', 'test_function');
. See Actions and Filters are not the same thing.
Loading wp-load.php
this way is... crappy code ;) See Wordpress header external php file - change title?.
The second main problem is why and what for do you you need this?
Anyway, init
will not work, using the filter the_editor_content
will. Although I don't understand the goal:
<?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外部文件中调用过滤器钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!