最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - add_filter does not work as expected

programmeradmin1浏览0评论

I have a wordpress plugin where i included this (with some changes)

$fep_files = array(
    'first' => 'first.php',
    'second' => 'second.php'
);

$fep_files = apply_filters('include_files', $fep_files );

foreach ( $fep_files as $fep_file )
require_once ( $fep_file );
unset ( $fep_files );

Now i added in my theme's function.php

function fep_remove ( $fep_files )
{
    if ( isset ( $fep_files['first'] ) ) {
        unset ( $fep_files['first'] );
            }
        //die($fep_files); //to check it fires
    return $fep_files;
}

add_filter( 'include_files', 'fep_remove' );

It should remove first.php but it does not. i am wrong some where, but where? Is there any better way to include files where user can include/exclude any files if needed?

I have a wordpress plugin where i included this (with some changes)

$fep_files = array(
    'first' => 'first.php',
    'second' => 'second.php'
);

$fep_files = apply_filters('include_files', $fep_files );

foreach ( $fep_files as $fep_file )
require_once ( $fep_file );
unset ( $fep_files );

Now i added in my theme's function.php

function fep_remove ( $fep_files )
{
    if ( isset ( $fep_files['first'] ) ) {
        unset ( $fep_files['first'] );
            }
        //die($fep_files); //to check it fires
    return $fep_files;
}

add_filter( 'include_files', 'fep_remove' );

It should remove first.php but it does not. i am wrong some where, but where? Is there any better way to include files where user can include/exclude any files if needed?

Share Improve this question edited Apr 6, 2015 at 14:10 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Apr 6, 2015 at 14:05 Shamim HasanShamim Hasan 2251 gold badge3 silver badges9 bronze badges 6
  • Are you sure that the plugin code is executed after the theme has been loaded? – fuxia Commented Apr 6, 2015 at 14:08
  • How to be sure? Is there any better way to include files where user can include/exclude any files if needed (using hooks)? – Shamim Hasan Commented Apr 6, 2015 at 14:18
  • I would suggest starting with making sure that you don't have a namespace collision. Call your hook something like 'myplugin_include_files' (where "myplugin" is something representing your plugin). Naming the hook too simply could end up with a conflict. – butlerblog Commented Apr 6, 2015 at 14:40
  • Your code works when I test it. That is going to make it hard to sort out where your particular problem lies. I should point out that requiring hard coding in a theme in order for a plugin to function is pretty bad form. – s_ha_dum Commented Apr 6, 2015 at 15:21
  • In real plugin code hooks are not too simple. – Shamim Hasan Commented Apr 6, 2015 at 16:40
 |  Show 1 more comment

3 Answers 3

Reset to default 1

Your plugin code is going to run before the theme code and hence will run before anything is added to the hook. You will need to the code that processes the file inclusion to some other hook that runs after all of the plugins load, like after_setup_theme

This should help: Is there a flowchart for wordpress loading sequence?

I think there needs to be an update to some of the answers on here. (Just for when I, or others, come upon this page in the future looking for an answer).

Plugin ordering seems to depend on the type of Wordpress installation you are working with. If you are working with a Multisite installation, then plugins are indeed processed in the order in which they are activated.

However, in a single wordpress installation, they are activated alphabetically. So, if you are running into an issue where you need to update an initialization filter or action for another plugin, your best bet is to name your plugin something that begins with an underscore.

For example: _my_custom_plugin/my_custom_plugin.php

For reference. please see 704-713 in wp-admin/includes/plugin.php

The previous answer was great as far as the order, but I could not figure out how to make the filter/hook work with those intructions.

My solution was creating a custom plugin containing the filter on it, making sure that it alphabetically comes before the plugin that contains the filter I want to add.

In other words, put the code in plugings/a-custom-plugin.php starting the file with

<?php
/*
Plugin Name: My Custom Plugin
*/
发布评论

评论列表(0)

  1. 暂无评论