我是Wordpress的新手,正在写我的第一个主题。该主题将与一个插件大量组合。我想知道如何才能在不触及插件本身的情况下更改主题function.php中的一些插件设置。
我试着在网上查了一下,但没有找到我的问题的任何具体答案。
我使用的是:
Wordpress 4.2,Justimmo api plugin
问题是:
我正在尝试弄清楚如何在我的主题function.php
中将插件模板文件重定向/替换为我主题中的插件模板文件。
当前在插件文件夹中的是模板文件以及它们将如何在网站上呈现。我会编写自己的模板文件,确保它看起来像我的主题,并将其替换为插件模板文件。
如果我直接在插件文件夹中编辑模板文件,每当可能有更新时,它们都会被覆盖。我不觉得这是对的。
在环顾互联网后,我觉得我可以用add_filter
实现它,但不完全确定如何实现。
插件
在Github上:https://github.com/justimmo/wordpress-plugin/blob/master/justimmo.php
在原始插件中有一行(行240用于indexPage()
,行328用于getIndexUrl()
):
class JiApiWpPlugin
{
function indexPage()
{
include(JI_API_WP_PLUGIN_DIR . '/templates/index.php');
}
function getIndexUrl()
{
return $this->getUrlPrefix().'ji_plugin=search';
}
}
它指定了在哪里可以找到一个模板文件,并给出了一个插件的永久链接。
我想要的
我想添加一个过滤器,以便从我的主题中获取模板文件,并重写模板文件的路径。
如果有一个简单的方法来添加一个钩子,那么它就会覆盖它,太好了。我还发现我可以用add_filter
做一些模板替换。
已经尝试过:
add_filter( 'template_include', 'gulz_justimmo_page_template' );
function gulz_justimmo_page_template( $page_template )
{
if ( is_page( 'ji_plugin' ) ) {
$page_template = dirname( __FILE__ ) . '/justimmotemp/justimmo-index.php';
}
return $page_template;
}
但我不完全确定如何检查插件固定链接页面是否被激活。
我觉得这对用户来说应该是一件很简单的事情,用他们的主题中的插件模板文件覆盖或重定向,但我不能弄明白。
我将非常感谢所有的帮助和建议。
发布于 2015-08-30 08:18:26
不幸的是,函数indexPage()
被另一个函数调用,该函数随后被引用钩住。
add_action('template_redirect', array(&$this, 'templateRedirect'));
您需要做的是删除此函数并替换为您自己的自定义函数(从插件中复制代码并修改以调用该页面的自定义函数)。问题是您不能使用remove_action,因为没有通过add_action
传递名称,所以wp会创建一个随每次加载而变化的名称。
因此,我们需要一个函数来查找添加的函数:
function remove_anonymous_action( $name, $class, $method ){
$actions = $GLOBALS['wp_filter'][ $name];
if ( empty ( $actions ) ){
return;
}
foreach ( $actions as $prity => $action ){
foreach ( $action as $identifier => $function ){
if ( is_array( $function) && is_a( $function['function'][0], $class ) && $method === $function['function'][1]){
remove_action($tag, array ( $function['function'][0], $method ), $prity);
}
}
}
}
然后你可以做的就是在上面添加了动作之后,使用优先级参数调用这个函数(这将删除函数btw中所有页面的函数)
// the action adding the action is added in parse_query filter...use this as the point to remove the added action
add_action('parse_query', 'remove_plug_action', 50);
function remove_plug_action(){
// call our function with the name of the hook, classname and function name
remove_anonymous_action('template_redirect','JiApiWpPlugin','templateRedirect');
//add a custom function to replace the one we removed.
add_action('template_redirect', 'customtemplateRedirect');
}
修改下面的函数来调用你的基于主题的文件。
function customtemplateRedirect(){
global $wp;
global $wp_query;
if (get_query_var('ji_plugin') !== '') {
switch (get_query_var('ji_plugin'))
{
case 'property':
$this->propertyPage();
exit;
break;
case 'expose':
$this->exposeDownload();
exit;
break;
default:
$this->indexPage();
exit;
break;
}
}
}
还可以使用以下命令检查钩子是否已被删除
$hook_name = 'template_redirect';
global $wp_filter;
var_dump( $wp_filter[$hook_name] );
无名过滤器将有一个长密钥,例如12i90rkl2rkljeri (每次都是随机的)。通过删除remove_plug_action()中的add操作来分解这个过程,看看会得到什么。如果不是var,则应该删除该操作转储$_GLOBALS‘’wp_filter‘以查看它是什么样子。这可能需要一段时间的摆弄。您还需要检查if语句(提供给它的var_dump值以检查它们是通过还是失败等)。
https://stackoverflow.com/questions/32289100
复制相似问题