• Resolved woalmoore

    (@woalmoore)


    Hello again,
    Thanks for the assistance you gave previously with displaying the title under the image. I’m still working on getting everything else to work as desired and need some help with the hooking/filtering process.

    I’ve setup a custom markup which sets “columns=5” as an argument; <table> for Open; <tr> for open row; </tr> for close row; and </table> for close. (trying to keep it as simple as I can). For the Item, I originally set it so that it creates a two-row, single-column table within the cell for the item so I can show the image and title in the top cell and the paypal button with dropdown in the bottom. I worked, in general. However, I’m trying to use a hook so that I can use the do_shortcode process so that it works a bit more seamlessly with another plugin I use for the add-to-cart and show cart buttons.

    I added this in my header document (this is a localhost website for now until I can get the situation finalized then I’ll probably move it to a different file).

    function add_paypal_button_to_images ( $title ) {
    	?> 
    	<td><table class="item_table"><tr>
    	<td class='gallery-icon'>
    		[+link+]<br />
    	<p class="item_title">[+title+]</p>
    	</td></tr>
    	<tr><td class="wp-caption-text gallery-caption">
    	<?php echo do_shortcode('[wp_cart_button name="Test Product" price="29.95"]'); ?>
    	</td>
    	</tr></table></td>
    <?php
    }
       add_action( 'mla_gallery_item_template', 'add_paypal_button_to_images');

    The Problem is that when I reload the page with this code in operation, it strips the outside table and rows from the mla_gallery shortcode (I’m using [mla_gallery post_parent=all mla_caption=”{+title+}” mla_markup=paypal mla_styles=paypal] as the shortcode). My hunch is that it is where I’m hooking but none of the other item-related hooks listed in the documentation even work this well.

    The other problem is using the MLA variables in my hooking function…what would I need to do to access the title and image variables from the plugin?

    I hope my question is clear.

    Thanks.
    Allen.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author David Lingren

    (@dglingren)

    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.

    Thread Starter woalmoore

    (@woalmoore)

    Thanks so much for your detailed reply. I think I got it working as desired… now to carefully copy it to a live site.

    Have a great week!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using Hooks with the Plugin’ is closed to new replies.