• Resolved mbruxelle

    (@mbruxelle)


    Hello,

    I was wondering if I can call the gallery by using a PHP function, instead of using the shorcode [mla_gallery] in a post.

    Thanks for your answer, great plugin!

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

    (@dglingren)

    Thanks for the compliment and for your question.

    The easy way to use [mla_gallery] from PHP code is by calling the WordPress do_shortcode() function. Here are two earlier topics with some examples:

    do_shortcode categories and taxonomies not working

    album name begins with (like)

    Using do_shortcode() is the best way to insulate your code from changes in future MLA versions. If you must, you can call the MLA shortcode processing functions directly, e.g.:

    
    echo MLAShortcodes::mla_gallery_shortcode( $attr, $content );
    

    A direct call gives you some flexibility in composing the shortcode parameters and avoiding some WordPress parsing issues but is somewhat more difficult to implement.

    I am marking this topic resolved, but please update it if you have any problems or specific questions about calling [mla_gallery] from your application’s PHP code. Thanks for your interest in the plugin.

    Thread Starter mbruxelle

    (@mbruxelle)

    Hi

    Thanks a lot!
    I tried to use MLAShortcodes::mla_gallery_shortcode but just to be sure.

    My $attr array looks like :

    $attr = array(
    		'post_mime_type' => 'all',
    		'tax_query' => array ('taxonomy' => 'attachment_tag', 'field' => 'id', 'terms' => array($wp_query->query_vars['photo_id']), 'include_children' => 0
    ),
    		'mla_target' => 'blank',
    		'columns' => 2,
    		'posts_per_page' => 30,
    		'size' => 'large',
    		'link' => 'file',
    
    );

    Is it correct ?

    Then I call echo MLAShortcodes::mla_gallery_shortcode( $attr, $content );
    But I don’t see what $content is supposed to be 🙂

    For the moment, it makes my page crash and returns a blank document.

    Thanks again for your help,
    Marc

    Plugin Author David Lingren

    (@dglingren)

    Thanks for reporting your progress and for adding the PHP code composing your $attr array. The syntax of the statement is fine and compiles correctly. There is one small error in your parameters; the 'mla_target' => 'blank' parameter should be 'mla_target' => '_blank'. The underscore is required for the HTML target attribute.

    The $content parameter is optional; it contains the shortcode content when using the “enclosing shortcode” syntax. You can simply omit this parameter.

    The page crash might be because the $wp_query->query_vars['photo_id'] element is not defined. You can try something like this:

    
    if ( empty( $wp_query->query_vars ) ) {
        echo 'query_vars not defined';
    elseif ( empty( $wp_query->query_vars['photo_id'] ) ) {
         echo 'photo_id not defined';
    else {
        $attr = ...
        echo MLAShortcodes::mla_gallery_shortcode( $attr );
    }
    

    That should help identify any problems.

    Thread Starter mbruxelle

    (@mbruxelle)

    Hi
    Thanks a lot!
    Actually my tax_query was incorrect, it is supposed to be a multidimensional array, not a single one.
    All fixed now!

    Thanks again for your great support!

    Plugin Author David Lingren

    (@dglingren)

    Good point – I looked at the syntax of the array statement but I didn’t investigate whether it was a valid WordPress tax_query. I regret any inconvenience caused by my incomplete response.

    I am marking this topic resolved. Thanks for letting me know it’s working for you now.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Use PHP Function instead of shortcode ?’ is closed to new replies.