Make WordPress Core

Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#36094 closed enhancement (maybelater)

Solving the Globals Problem... Introduce Dependency Injection For Filters and Actions

Reported by: seancjones's profile seancjones Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Plugins Keywords:
Focuses: Cc:

Description

This has been bugging me for a while. I'm hoping to tease this idea out further with the help of all you wonderful people.

Presently, the only way to pass values in WordPress is via the global scope. For large projects, I am sure we have all had that "banging your head against the wall" moment where we might wish there was a better way.

I'm proposing a change to the WordPress core: Adding a 5th argument to add_filter and add_action called $dependencies. Like $priority and $accepted_args, this variable would be optional. If passed, it will expect an array. Any passed variable should be treated like an array, even if it's just a single variable.

Developers seeking to make use of dependencies can forego globals and pass their dependencies via this array, calling them in the filter after all accepted args of an action are called.

Example:

$title = $wpdb->get_results( 'SELECT title FROM customtable' );
add_filter( 'the_title', 'get_custom_title', 10, 1, array( 'title' => $title, 'post' => $post ) );

//...
// The Loop
the_title(); ?>
Welcome to the page called: <?php echo $title[0];  ?>

<?php
// Function
function get_custom_title($title, $dependencies) {
    $title = $dependencies['title'];
    $post = $dependencies['post'];
    $post->ID = $post->ID%100; //not changing the global object
    return $title . ' (Last 2 digits of post ID: ' . $post->ID . ')';
}

The $dependencies variable would allow a plugin developer to pass filters along.

What do you think? Good? Bad? Terrible? Would love some feedback.

Thanks,

Sean

Change History (5)

#1 @chriscct7
9 years ago

  • Version trunk deleted

#2 @seancjones
8 years ago

  • Resolution set to maybelater
  • Status changed from new to closed

#3 @chriscct7
8 years ago

@seancjones why did you close this?

#4 follow-up: @seancjones
8 years ago

I brought it up as my first ticket. I think it's a little awkward. A combination of no interest and me not even sure I'd back the proposal as is anymore, made me feel like I wanted to clean it from "My Tickets". Maybe doing this wrong. Not worried either way about it.

#5 in reply to: ↑ 4 @helen
8 years ago

  • Milestone Awaiting Review deleted

Replying to seancjones:

I brought it up as my first ticket. I think it's a little awkward. A combination of no interest and me not even sure I'd back the proposal as is anymore, made me feel like I wanted to clean it from "My Tickets". Maybe doing this wrong. Not worried either way about it.

Nope, nothing wrong here. Glad to see follow up on your own ticket, and sorry it languished.

Note: See TracTickets for help on using tickets.