我总是为客户端更改Wordpress插件。但是如果插件更新,这些更改总是有丢失的危险。
有没有办法在Wordpress中为另一个插件制作插件?是否有办法在每次插件更新后保留更改或重新应用更改。
我总是为客户端更改Wordpress插件。但是如果插件更新,这些更改总是有丢失的危险。
有没有办法在Wordpress中为另一个插件制作插件?是否有办法在每次插件更新后保留更改或重新应用更改。
我认为最好的方法是通过操作和过滤器,比如我们扩展WordPress核心本身。
其他选项如@helgatheviking pointed,如果插件是类,则可以对其进行扩展。
不幸的是,并不是所有的插件开发人员都会在代码中提供有用的过滤器和操作,大多数插件都不是以面向对象的方式编写的。保存插件更新修改的唯一方法是创建原始插件的副本,更改插件名称。我通常在原名前加前缀。Mamaduka Twitter Connect
, 但使用此解决方案,您需要手动更新原始插件的代码。
如果你认为插件需要更多的过滤器/操作,你可以联系作者,让他将这些挂钩包含到核心中。
一种简单的方法是在插件中定义可以钩住的自定义钩子。内置挂钩系统允许您创建自己的挂钩,然后像普通Wordpress挂钩一样绑定到挂钩上。这个Wordpress Codex 有关于do\\u action函数以及如何使用它创建自定义挂钩的很好的示例和说明。插件和主题开发人员严重忽视了Wordpress中的一个功能。
我坚信hooks系统是开发其他插件可以扩展的插件所需的全部,但正如我所说的,90%的Wordpress开发人员都严重忽视了这一点。
See below for an example (taken from the Wordpress Codex link provided):
<?php
# ======= Somewhere in a (mu-)plugin, theme or the core ======= #
/**
* You can have as many arguments as you want,
* but your callback function and the add_action call need to agree in number of arguments.
* Note: `add_action` above has 2 and \'i_am_hook\' accepts 2.
* You will find action hooks like these in a lot of themes & plugins and in many place @core
* @see: http://codex.wordpress.org/Plugin_API/Action_Reference
*/
// Define the arguments for the action hook
$a = array(
\'eye patch\' => \'yes\'
,\'parrot\' => true
,\'wooden leg\' => (int) 1
);
$b = \'And hook said: "I ate ice cream with peter pan."\';
// Defines the action hook named \'i_am_hook\'
do_action( \'i_am_hook\', $a, $b );
# ======= inside for eg. your functions.php file ======= #
/**
* Define callback function
* Inside this function you can do whatever you can imagine
* with the variables that are loaded in the do_action() call above.
*/
function who_is_hook( $a, $b )
{
echo \'<code>\';
print_r( $a ); // `print_r` the array data inside the 1st argument
echo \'</code>\';
echo \'<br />\'.$b; // echo linebreak and value of 2nd argument
}
// then add it to the action hook, matching the defined number (2) of arguments in do_action
// see [http://codex.wordpress.org/Function_Reference/add_action] in the Codex
// add_action( $tag, $function_to_add, $priority, $accepted_args );
add_action( \'i_am_hook\', \'who_is_hook\', 10, 2 );
# ======= output that you see in the browser ======= #
Array (
[\'eye patch\'] => \'yes\'
[\'parrot\'] => true
[\'wooden leg\'] => 1
)
And hook said: "I ate ice cream with peter pan."
FWIW,您可以增加插件版本号,这样它就不会自动更新。虽然这不是最好的解决方案,但它会解决眼前的问题。您还可以更改命名和文件名,使它们不再是“相同”的插件。
我正在制作一个插件,它需要一个可以从外部访问的页面,非常像一个API,并且有这样的url,http://xxxxx/custom_method?parameter=xxxxx&something=xxxx有没有干净的方法可以做到这一点?提前谢谢。