我最近开始开发插件和主题,我发现我需要在这两者上使用的几个功能。
有时,我想检查函数/类在声明之前是否存在,如本文所述:When to check if a function exists
但这被认为是不好的做法。预防冲突和保留主题的最佳做法是什么&;插件在没有安装一个主题/插件的情况下独立工作?
我最近开始开发插件和主题,我发现我需要在这两者上使用的几个功能。
有时,我想检查函数/类在声明之前是否存在,如本文所述:When to check if a function exists
但这被认为是不好的做法。预防冲突和保留主题的最佳做法是什么&;插件在没有安装一个主题/插件的情况下独立工作?
<?php
/** Plugin Name: (#68117) Print Hello! */
function wpse68117_print_hello()
{
echo "Hello World!";
}
add_action( \'wpse68117_say\', \'wpse68117_print_hello\' );
主题内部:<?php
/** Template Name: Test »Print Hello!« Plugin */
get_header();
// Now we call the plugins hook
do_action( \'wpse68117_say\' );
现在发生了什么/kool kid这样我们就不必检查函数、文件、类、方法甚至是(不要这样做!)全球的$variable
. WP intern global已经为我们提供了这一功能:它检查挂钩名称是否是当前过滤器,并附加它。如果它不存在,什么也不会发生。<?php
/** Plugin Name: (#68117) Print Thing! */
function wpse68117_print_thing_cb( $thing )
{
return "Hello {$thing}!";
}
add_filter( \'wpse68117_say_thing\', \'wpse68117_print_thing_cb\' );
主题内部:<?php
/** Template Name: Test »Print Thing!« Plugin */
get_header();
// Now we call the plugins hook
echo apply_filter( \'wpse68117_say_thing\', \'World\' );
这一次,我们为用户/开发人员提供了添加参数的可能性。他也可以echo/print
输出,甚至进一步处理它(如果您得到一个数组作为回报)。<?php
/** Plugin Name: (#68117) Print Alot! */
function wpse68117_alot_cb( $thing, $belongs = \'is mine\' )
{
return "Hello! The {$thing} {$belongs}";
}
add_filter( \'wpse68117_grab_it\', \'wpse68117_alot_cb\' );
主题内部:<?php
/** Template Name: Test »Print Alot!« Plugin */
get_header();
// Now we call the plugins hook
$string_arr = implode(
" "
,apply_filter( \'wpse68117_grab_it\', \'World\', \'is yours\' )
);
foreach ( $string_arr as $part )
{
// Highlight the $thing
if ( strstr( \'World\', $part )
{
echo "<mark>{$part} </mark>";
continue;
}
echo "{$part} ";
}
这个插件现在允许我们插入两个参数。我们可以把它保存到$variable
并进一步处理。function_*/class_*/method_*/file_exists
或使用in_array()
约1k(?)筛选搜索)。您还可以避免所有那些不必要的关于未设置变量等的通知,因为插件关心这一点。(所谓“页面模板”,我指的是具有\"Template Name\" header 可在管理页面的“模板”下拉字段中选择。)在多个实例中,我构建了页面模板,可以通过挂接到\\u内容或pre\\u get\\u帖子或类似的内容来完成。这让我想知道是否有一种方法可以在functions.php 或者不创建主题文件本身的插件。我想这样做的原因:在我想到的场景中,我通常只是复制页面。php几乎一字不差。这意味着将来对页面的任何更改。php需要制作两次。随着儿童主题的更新,这更加令人痛苦</我喜欢“模板”字段U