Thanks for the detailed description of your application and the code sample; very helpful. You’ve got the right idea, but the implementation needs some adjustments.
The description of the hooks in the Documentation tab is rather terse. You can find much more information about them by examining the “MLA Gallery Hooks Example” plugin, which has code for all of the hooks showing their arguments, etc. Some of the functions also include examples of using the hooks to solve earlier support questions.
For your application, note that the mla_gallery_item_template
hook is a filter, not an action. It takes the Item portion of the markup template as an argument and it must return that argument or an updated value from the function so the [mla_gallery]
code can use the template to generate its results. So, the simplest implementation would be something like:
function add_paypal_button_to_images ( $template ) {
return $template;
}
add_filter( 'mla_gallery_item_template', 'add_paypal_button_to_images');
Your code echoes the updated template directly to the browser, giving MLA no opportunity to process it. For your application you can do something simple like:
function add_paypal_button_to_images ( $template ) {
$template = '<td><table class="item_table"><tr>';
$template .= '<td class="gallery-icon">';
$template .= ' [+link+]<br />';
$template .= '<p class="item_title">[+title+]</p>';
$template .= '</td></tr>';
$template .= '<tr><td class="wp-caption-text gallery-caption">';
$template .= do_shortcode('[wp_cart_button name="Test Product" price="29.95"]');
$template .= '</td>';
$template .= '</tr></table></td>';
return $template;
}
add_filter( 'mla_gallery_item_template', 'add_paypal_button_to_images');
You asked “what would I need to do to access the title and image variables from the plugin?” There are at least three techniques you can use to access the values. First, the WordPress global $post;
object is available when the filter is called, so you can get some values such as the Title in that object. You can also use the ID property of the object to call an MLA function that returns any of the “Attachment-specific substitution parameters for the markup template Item part” listed in the Documentation tab:
global $post;
// You can use MLAShortcodes::mla_get_data_source() to get anything available.
$my_setting = array(
'data_source' => 'thumbnail_content',
'option' => 'raw'
);
$thumbnail_content = MLAShortcodes::mla_get_data_source( $post->ID, 'single_attachment_mapping', $my_setting, NULL );
Second, you can hook another MLA filter to get a copy of the attachment-specific values as an array:
function save_item_specific_values ( $item_values ) {
global $my_item_specific_values;
return $my_item_specific_values = $item_values;
}
add_filter( 'mla_gallery_item_values', 'save_item_specific_values');
function add_paypal_button_to_images ( $template ) {
global $my_item_specific_values;
$template = '<td><table class="item_table"><tr>';
$template .= '<td class="gallery-icon">';
$template .= ' [+link+]<br />';
$template .= '<p class="item_title">[+title+]</p>';
$template .= '</td></tr>';
$template .= '<tr><td class="wp-caption-text gallery-caption">';
$template .= do_shortcode('[wp_cart_button name="' . $my_item_specific_values['title'] . '" price="29.95"]');
$template .= '</td>';
$template .= '</tr></table></td>';
$my_item_specific_values = NULL;
return $template;
}
add_filter( 'mla_gallery_item_template', 'add_paypal_button_to_images');
Finally, you could use the mla_gallery_item_values
filter to add the PayPal button to the item values array and then just use it in your markup template, avoiding the need for the template hook:
function save_item_specific_values ( $item_values ) {
$item_values['paypal_button'] = do_shortcode('[wp_cart_button name="' . $item_values['title'] . '" price="29.95"]');
return $item_values;
}
add_filter( 'mla_gallery_item_values', 'save_item_specific_values');
The final alternative lets you use [+paypal_button+]
in your custom markup template just like any other attachment-specific value.
There are lots of possible improvements to the sample code but I hope it gives you a sense of what’s possible. I am marking this topic resolved, but please update it if you have any problems or further questions regarding the above suggestions.