Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am building a plugin and I'm getting doubts whether the code I am using is the correct way of doing things with regards to WordPress hooks and filters.

The scenario is that the code at a particular point needs to know whether to do one thing or another. Simple thing, I could use an option in the plugin's admin. But in order not to clutter the plugin's admin, and given that this option will likely be used by a very tiny percentage of users, I opted to not have a GUI option and instead have this true/false value set via code (the Genesis framework uses this methodology a lot).

We start with the default value in variable:

$force_default_thumbnail = apply_filters( 'image_priority', FALSE );
// rest of logic goes here

So the variable starts out being FALSE, and a user of the plugin can modify that by using a filter in their theme's functions.php (or as a separate plugin) as follows:

add_filter( 'image_priority', 'my_force_default_thumbnail' );

That would in turn trigger the following function which is also included in the main plugin file.

function my_force_default_thumbnail( $force ) {
    return TRUE;
}

When that function is triggered the default value changes to true and the logif of the plugin changes based on that modification.

I hope I explained it well enough, and look forward to your advice.

share|improve this question

closed as off-topic by kaiser Feb 4 at 0:35

  • This question does not appear to be about WordPress within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
It looks right. Have you tested it and does it work? –  s_ha_dum Feb 4 at 0:02
    
Yes it works, my doubt is whether there is a more elegant way to do this, I am still not 100% sure on actions vs filters in such cases. –  drtanz Feb 4 at 0:08
    
For "more elegant", please take a look at Code Review.SE site. –  kaiser Feb 4 at 0:34

1 Answer 1

up vote 0 down vote accepted

Umm. Normally, people ask questions here that are more than yes/no questions, but hey, I'm game.

Yes, that code looks fine to me.

Hope that helps! :)

Also note that there is a built in function called __return_true which is there for usage just like so:

add_filter( 'image_priority', '__return_true' );

Easy.

Also also note that "image_priority" is pretty generic for a filter name. You should ideally prefix your filter names with the plugin's name, so that filters don't conflict with each other.

share|improve this answer

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