Customizing Wordpress's Media Library -- should I use a hook?
April 20, 2011 4:44 PM   Subscribe

Question about Wordpress plugins and hooks.

I've been trying to write code to add a new, clickable option to the Wordpress Media Library. The idea is 1) to let the user choose a picture, and then automatically 2) do stuff with that picture. (For example, assigning that picture's URL to a custom field.)

After reading the Wordpress Codex and various other people's plugins, here is what I am confused about: OK, so I have successfully added a link to the Media Gallery, "do stuff with this picture." And so it is all ready to send GET info to a PHP function, somewhere.

But where? I don't know how to make that info go to a hook that I can hang a function on, or to some other PHP that can deal with it.

I'm still learning the Hook system, and I'm wondering if there is a nice Hook that would handle a situation like this through add_action. If I only knew which hook that was, or, if this isn't the best solution, what other method you might suggest for letting a user click on the Dashboard to reach a new function to handle his/her input.

I thought about just creating a separate form-handling PHP script but wasn't sure if this was proper Wordpress programming form.

Any help much appreciated.
posted by Victorvacendak to Computers & Internet (4 answers total) 2 users marked this as a favorite
 
Best answer: So you're basically asking how to intercept a form submission in your plugin?

Here's how I do it (and I'd love to hear about a better way; this isn't elegant, but then Wordpress is often inelegant):

First, include a hidden field in your form that you can look for at init time. (Also, make sure your form doesn't use variable names that are reserved by Wordpress, such as "name". This can cause weird bugs.) I typically name my hidden field something like "mypluginname_action".

Next, do something like this:

add_action( 'admin_init', 'your_intercept_function' );

function my_intercept_function() {
    if ( isset( $_REQUEST['mypluginname_action'] ) ) {
        switch ( $_REQUEST['mypluginname_action'] ) {
            case 'update':
                updateEntry( $_REQUEST['entry_id'], $_REQUEST['entry_name'] );
                break;
            case 'delete':
                deleteEntry( $_REQUEST['entry_id'] );
                break;
        }
    }
}


This will run for every admin page load, but will only do something if mypluginname_action exists in the form post.

(For forms on the public side of the site, use "init" instead of "admin_init". Except that I'm not 100% positive, at the moment, that the init hooks are the right ones—you might need actions that occur later in the page's lifetime. But it should definitely be an action that happens before Wordpress actually starts rendering the page.)
posted by ixohoxi at 5:21 PM on April 20, 2011


Best answer: Er, and "your_intercept_function" should read "my_intercept_function", of course.
posted by ixohoxi at 5:22 PM on April 20, 2011


Response by poster: Yeah, I guess Wordpress is kind of inelegant under the surface, isn't it? So right, I am trying to intercept a form submission, then use that form data in a class method.
posted by Victorvacendak at 5:36 PM on April 20, 2011


Response by poster: Thanks, this ended up working great. While I couldn't get init to hook onto your handler method, I ended up hooking it successfully to admin-footer...which after all is the hook immortalized by the default Hello Dolly plugin.

(It makes the text show up at the bottom of the admin screen.)
posted by Victorvacendak at 2:58 PM on April 21, 2011


« Older Help me find a new home in the Philadelphia...   |   Indie Songs of Sex Newer »
This thread is closed to new comments.