Title: Use PHP Function instead of shortcode ?
Last modified: March 1, 2017

---

# Use PHP Function instead of shortcode ?

 *  Resolved [mbruxelle](https://wordpress.org/support/users/mbruxelle/)
 * (@mbruxelle)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/use-php-function-instead-of-shortcode/)
 * 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](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/use-php-function-instead-of-shortcode/#post-8866657)
 * 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](https://wordpress.org/support/topic/do_shortcode-categories-and-taxonomies-not-working/)
 * [album name begins with (like)](https://wordpress.org/support/topic/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](https://wordpress.org/support/users/mbruxelle/)
 * (@mbruxelle)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/use-php-function-instead-of-shortcode/#post-8870740)
 * 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](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/use-php-function-instead-of-shortcode/#post-8871468)
 * 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.
    -  This reply was modified 9 years, 2 months ago by [David Lingren](https://wordpress.org/support/users/dglingren/).
    -  This reply was modified 9 years, 2 months ago by [David Lingren](https://wordpress.org/support/users/dglingren/).
 *  Thread Starter [mbruxelle](https://wordpress.org/support/users/mbruxelle/)
 * (@mbruxelle)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/use-php-function-instead-of-shortcode/#post-8872433)
 * 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](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/use-php-function-instead-of-shortcode/#post-8877109)
 * 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.

 * ![](https://ps.w.org/media-library-assistant/assets/icon-256x256.png?rev=973502)
 * [Media Library Assistant](https://wordpress.org/plugins/media-library-assistant/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/media-library-assistant/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/media-library-assistant/)
 * [Active Topics](https://wordpress.org/support/plugin/media-library-assistant/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/media-library-assistant/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/media-library-assistant/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [David Lingren](https://wordpress.org/support/users/dglingren/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/use-php-function-instead-of-shortcode/#post-8877109)
 * Status: resolved