1

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?

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)? 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. Commented Apr 6, 2015 at 16:40

3 Answers 3

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?

1
  • i had already done that and that is working perfectly now. You also give me the same solution. I am accepting your answer also. Commented Apr 7, 2015 at 14:22
2

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

1
  • 2
    Hmm, I think you should rely on a hook e.g. a high priority plugins_loaded handler rather than alphabetical order, but this is useful information - thanks
    – Rup
    Commented Dec 21, 2020 at 10:02
-1

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
*/
1
  • Plugins are not run in alphabetical order - they are in fact run in the order of activation as when activated the plugin is added to the array of active plugins stored in the database. ( wp_options -> active_plugins )
    – Bysander
    Commented Mar 19, 2020 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.