post-page

Safest Way to Include Plugin Code in Themes

27
responses
by
 
on
August 20th, 2008
in
WordPress FAQs, WordPress Tips
heading
heading
heading
27
Responses

 

Comments

  1. Daniel K says:

    Is there a way to check and see if that plug-in is active?

    • Keith Dsouza (82 comments.) says:

      Daniel you can do that from the admin panel on the plugins page.

      • Daniel K says:

        Yes, you can. IF you can check to see if the plug in is active, not just that function though, you can set up an if statement at the start and set variable to true or false, then a tertiary(?) if to decide rather something is written or not.

    • Barry (33 comments.) says:

      Yes there is use:
      $current_plugins = get_option(‘active_plugins’);
      if (in_array(‘plugindir/pluginfile.php’, $current_plugins)) {
      // The plugin is active
      }

      • Daniel K says:

        Thank you.

        • Rajesh (35 comments.) says:

          But this is an inefficient way of doing it and not necessary…inefficient as you are doing a get_option and scanning through an array…

          • Daniel K says:

            I would say it breaks even personally. You scan the array to find it, set a session or global variable and have it use that on the page. Now if you have 15 plug-ins and your doing it to I can see where that could be a burden.

  2. Jeffro2pt0 (164 comments.) says:

    Thanks Keith for putting together this post. This seems to showcase a better example of what I tried to explain within that if, then, else post.

  3. Benedict Eastaugh (17 comments.) says:

    A better way is to just add some hooks to your theme. That way you create a generic API rather than tying your theme to a specific plugin.

  4. Anthony (1 comments.) says:

    I think this is much better way to embed some plugin code in theme. It is easily to understand. Thanks for the great knowledge.

  5. johnbillion says:

    Benedict is absolutely right. By far the best way to achieve this is to add custom hooks and actions to your theme. That way you can add multiple actions to each hook and the code is very much leaner than having if…else loops all over the place.

    The whole of the example code in this article could be rewritten with:

    <?php do_action( 'my_theme_action' ); ?>

    and then add an action to your functions.php file like this:

    <?php add_action( 'my_theme_action', 'my_theme_function' ); ?>

    and then use my_theme_function() to output the desired code.

    • Stephen R (24 comments.) says:

      johnbillion’s method is outlined in a bit more detail here.

    • Keith Dsouza (82 comments.) says:

      That would involve users having to manually add in hooks to all the plugins, which would automagically disaapear when you apply updates from plugin authors.

      On the other there are times where hooks into content do not give desisred outputs, for example I use a ratings plugin, but do not want it to show up at the end of the post, I want it to be a part of the post meta, this would mean I have to include the plugin template tag in my theme, this approach safeguards me from generating PHP errors, if for some reason I decide to deactivate the plugin and forget removing the template tag from the theme.

      • Eric Marden (16 comments.) says:

        Not true. The idea of the hook is that if its already in the theme, then users would hook the template tag from the plugin (which normally would get pasted into the theme) into the theme via functions.php.

        The only thing missing is teaching new users how this is done, since most plugins with template tags talk about pasting them in the theme.

    • Barry (33 comments.) says:

      Even better if the plugin author actually built custom actions into their plugins. So all you would have to type in the template is do_action(‘theplugins_custom_hook’);
      And if the plugin was disabled then nothing would happen.

  6. johnbillion says:

    @Keith:

    That’s not the case at all. Instead of adding < some_plugin_function(); ?&gt into your theme, you’d add < do_action( 'my_action' ); ?> and then in your theme's functions.php file you'd add < add_action( 'my_action', 'some_plugin_function' ); > (or, if the function requires parameters, you'd use a wrapper function).

    • Stephen R (24 comments.) says:

      I hadn’t thought of that — adding it the the theme instead of the plugin. Nice. :)

      • Rajesh (35 comments.) says:

        As Barry discovered, the easiest is for plugin authors to build the custom actions into their plugins…

        • Stephen R (24 comments.) says:

          True. Note that the article I linked above was targeted at plugin authors, not end users.

          I do, however, like the concept of adding an add_action to a theme’s functions.php file if you do want to do it as an end user.

      • Rajesh (35 comments.) says:

        Why should the user take the trouble of adding those hooks to theme’s functions.php file?

  7. johnbillion says:

    Oops, mucked my formatting up a bit there. The point is, you do not have to modify any plugin files.

    • Rajesh (35 comments.) says:

      No, I am saying the plugin author should add them as he needs those hooks for his plugin to generate the desired output…

  8. Jane (1 comments.) says:

    Very useful information in this post, thanks.

  9. Rajesh (35 comments.) says:

    This post is a brilliant example of various php developers brainstorming different alternatives to code and trying to arrive at the best way of doing things…though these are simple code, the post really helps to read the minds of different developers…great post and continue these sort of posts here… :)



Trackbacks/Pingbacks

  1. […] Good post on WLTC about using conditions for plugin output in themes. […]

  2. […] Dsouza von der Weblog Tools Collection gibt in seinem Artikel “Safest Way to Include Plugin Code in Themes” nun richtigerweise zu bedenken, dass eine solche Integration des Plugins in das Theme zu PHP […]

Obviously Powered by WordPress. © 2003-2013

page counter
css.php