zp_register_filter

This is less of a support or announcement and more of a discussion for input.

I'm bringing all my plugins up to 1.3.1.2-6003 compliance and am removing the reliance on any deprecated functions (addpluginscript for one).

The way I currently have the filter-function setup is an if check, then load. I guess my question comes from if this is really even needed or the best approach.

Does zp_register_filter load the same function multiple times if it's the same name? Or will it load it the first time, and skip it without error after, which is what I think is currently happening as the function loads once (which is the desired effect).

I'm just trying to figure out if I even need this ifcheck to begin with on it.

Comments

  • I'm not to sure what you're currently doing, but you should register the filter once for each plugin. The way that filters work is that you register a filter and associate it with a callback function. When the filter gets called, it will call all the associated callbacks. So it doesn't hurt to have it registered more than once, but there's really no reason to have it registered more than once per plugin.
  • Yeah it's only once per plugin, but the filter function is named the same in each of the plugins, so it's only loading the metadata once (which is the functionality I want). I guess maybe instead of 1 plugin for each of the zenFBSuite components I might think about combining them into one large plugin... just seems like a waste of resource though for those that only want specific parts of it.
  • acrylian Administrator, Developer
    You do know that you can't have multiple functions with the same name anyway? You will get an error about redeclaring and make it impossible to use more than one plugin at the time. (except you encapsulate them as methods of different classes).

    If your plugins are all quite similiar joing them to one plugin is probably a good idea to save unnecessarily code duplication anyway. You could make options for the several parts.
  • yeah I know that you can't redeclare it, which is why I have the if-check. I'll look at thecose when I get home. combining them to one plugin should be easiest overall. I guess that's why I was confused how the function for the filter works. I SHOULD be getting the redeclare error, but I'm not.
  • There is, essentially, an entry in a filter list for each combination of filter, function, and priority. So if you use the same zp_register_filter() call multiple times it will result in only one instance. But you can register the same function for multiple filters and apparently with multiple priorities.

    The function, of course, is only declared when your plugin loads. If you have multiple plugins with the same function name and load them all you will get the redeclare error. Not sure what class of error redeclare is, maybe it is supressed in your install.
  • Yeah... Not sure what's going on. I admit looking back at the code now that I should be seeing redeclare errors. But everything is loading and functioning just fine with no errors displayed. my php is set to show all errors right now.

    Because looking at what I'm using now, with a non-foggy brain I can see that the if-check I used was completely useless.

    It is however only loading one instance of the meta-keys.

    I guess at this point combining all the plug-ins into one larger plug-in would make more sense, but I kind of like the ability for users to just download and use the parts they want.

    So here's how it's setup currently.

    zenFBSuite of plug-ins load all the required meta-keys for the OpenGraph data that makes them function properly. They all use the same meta-keys so they only need to load once. Previously (before switching to this filter usage) it was loading each set of meta keys equal to the number of the plug-ins used.

    It was messy but I figured the minor overhead usage was better than one large plug-in with all the options and then confusion on which to set for which plugin.

    So now, I've converted all the meta-keys from addpluginscript functions to one function called zenFBOpenGraphJS().

    Each plug-in has the same function, and each function uses a `zp_register_filter('theme_head', 'zenFBOpenGraphJS');`

    After enabling all plug-ins at once and checking the header, it's only loading one set of meta-keys, which is ideal. I'm just worried I'm not going about it properly, since now that I look back at the code, the if-check is really not stopping the function call.

    They all use the above from codeblock the same, so if I'm understanding what you said sbillard, it functions that way properly since they're all the same priority? So since they're all the same priority, they all are combined into once instance, so I shouldn't be getting the redeclare error, right?(which I'm not currently)
  • Well, registering filters does not declare the processing function. So if there is only one function declaration for the processing function, then there is no redeclare.

    I guess what I would do is make a separate script for the js part that has the register filter call and the function declaration. Then each other plugin would just do a `require_once()` on that script. This way, only one function declaration and only one filter registration no matter how many of the other plugins are enabled.
Sign In or Register to comment.