WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] A "do stuff with this picture" link in the Media Gallery (7 posts)

  1. johngorenfeld
    Member
    Posted 3 years ago #

    Let's say you use the Media_Row_Actions filter to add a new action to the Media Library. Your new option will be a link reading, "Do stuff with this picture." When the user clicks this, it sets off a new function, a function that acts on that picture URL.

    What's the best way to code the plugin so that the link calls upon some function, feeding the image URL into it, to perform some task (like adding that picture URL into the CSS)?

  2. frankindelicato
    Member
    Posted 3 years ago #

    I am researching the media_row_actions filter and have added a "Add to Database" link.
    The problem I am having is understanding the arguments passed by the filter code. I set up the following:

    add_filter('media_row_actions', 'add_media_action');
    function add_media_action( $actions, $post ) {
    	$newaction['adddata'] = '<a href="" title="Add To Database" rel="permalink">' . __('Add To Database') . '</a>';
    	return array_merge($actions,$newaction);
    }

    The call for the filter is in wp-admin/upload.php on line 429:

    $actions = apply_filters( 'media_row_actions', $actions, $post );

    I assumed when my filter was called that it would be passed $actions and $post but I get an error when I use the $post field in the add_media_action function. Any ideas on how this works?

  3. johngorenfeld
    Member
    Posted 3 years ago #

    Yeah, it seems like an obscure filter without much documentation that I could find. In a similar plugin I saw, there was no $post, just $action in the parameters.

  4. frankindelicato
    Member
    Posted 3 years ago #

    Ok. You have to specify the priority and number of args in the add_filter function:
    add_filter('media_row_actions', 'add_media_action',10,2);
    and now the link in the action can be put in so the ID of the media will be passed.
    $newaction['adddata'] = '<a href="' . get_permalink($post->ID) . '" title="Add To Database" rel="permalink">' . __('Add To Database') . '</a>';

  5. johngorenfeld
    Member
    Posted 3 years ago #

    What I wonder about is the best way to handle the form input from our new action. I am working on a method that hooks into admin_footer and runs code when it receives a form, but I wonder if there is a more elegant solution.

  6. johngorenfeld
    Member
    Posted 3 years ago #

    [this reply deleted, I figured out the problem]

  7. frankindelicato
    Member
    Posted 3 years ago #

    johngorenfeld,
    I am just modifying the Edit Media page by adding fields to it that I save in a database so my HREF just goes to the Edit Media page and I pass an extra argument to let the page know to set up and save my fields:

    $extra='&myfield=true';
    	$newaction['adddata'] = '<a href="' . get_edit_post_link($post->ID, true) . $extra . '" title="Add to Database" rel="permalink">' . __('Add to Database') . '</a>';

    I check for the extra argument in the $_GET array and if it is present I add the extra fields using a filter:

    add_filter('attachment_fields_to_edit', 'cm_fields_to_edit',10,2);

    I hope that helps you out...

Topic Closed

This topic has been closed to new replies.

About this Topic