Using functions from a plugin in your theme

I am a beginner to wordpress (and to php for that matter).

I am trying to understand some of the basics, and building a little e-commerce store using a plugin called “Jigoshop.”

Read More

I’m reading through the source files and seeing a bunch of useful functions- things like a “is_featured” function that returns true if the product has a product meta that marks it as featured; or a reference to all products that I can use without having to set up my own by querying the posts and filtering out those that are not products.

I have a custom-template file that I am using for the storefront (homepage), and I would like to access some of these functions to simplify the code I have to write, but it seems I don’t have access to them.

My questions is:

Are functions and objects defined in a plugin useable in my theme templates (or custom templates; I’m using a child theme right now)?

Related posts

Leave a Reply

2 comments

  1. Yes, you can use functions from plugins in your theme. Please use the function_exists() function to make sure that the function does exit. I used the Breadcrumbs Plus in one of the themes like this:

    <?php
    if (function_exists('breadcrumbs_plus'))
    {
        $breadcrumb_options = array(
                'prefix' => '<div id="breadcrumb">',
                'suffix' => '</div>',
                'title' => 'Du er her: ',
                'home' => 'Forside',
                'sep' => '/',
                'front_page' => false,
                'bold' => false,
                'blog' => __('Blog', 'rev'),
                'echo' => true
        );
    
        breadcrumbs_plus($breadcrumb_options);
    }
    ?>
    
  2. The short answer is yes, if a plugin sets a function, you can use it in your theme. Like @Box describes, definitely use the if (function_exists(”)) wrapper so that if something happens to the plugin in the future, or you have to disable it for testing purposes, the lack of the function won’t break your site. (It would be a nightmare to debug in that scenario)

    If the plugin uses classes and objects, then it gets a little trickier to figure out what the object is to reference the function, but what I’ve done is looked for filters that I can use to hook into and often these will help me get the information I need.

    If you give an example or reference the plugin you are trying to use, we may be able to help you figure out how to get access to its functions.