How to call shortcode function directly and pass $atts

I am using the Media Library Categories plugin. It does not have much documentation, so I have concluded the way to implement it would be using the shortcode [mediacategories categories="6"].

I want to implement this directly into the theme template. So I have tried:

Read More
<?php if(function_exists(do_shortcode('[mediacategories categories="6"]'))) { ?>
  <h1>Inspiration</h1>
<?php
  do_shortcode('[mediacategories categories="6"]');
}

I also tried to implement the function directly, but could not pass the $atts correctly for the function to use them.

Related posts

Leave a Reply

2 comments

  1. do_shortcode() just parses the string. You have to use echo or print to print it out.

    function_exists() expects a string that matches a function name. After looking at the plugin, I would try this code:

    <?php
    if ( function_exists( 'mediacategories_func' ) )
    {
    ?>
    <h1>Inspiration</h1>
    <?php
        print mediacategories_func( array( 'categories' => 6 ) );
    }
    
  2. I usually add a section like this to my shortcode callbacks:

    if (is_array($atts)) {
        extract(shortcode_atts(array(
                'primary_att' => 'primary_default',
                ...
            ), $atts));
    } else {
        $primary_att = $atts;
    }
    

    This enables you to pass a ‘primary’ attribute (replace primary_att with whatever you primary attribute is called) directly into the function, without wrapping it in an array…in your case it could be ‘categories’ and you could call it like mediacategories_func(6); then.