views:

39

answers:

2

I've created a calendar plugin and now I want to show a event list in one of my templates. The code I'm using now, is this:

include_once(WP_CAL_PLUGIN_DIR.'eventcal.class.php');

$calendar = new EventCalendar();
$events = $calendar->getMultipleEvents('5');

(...)

<table>
<?php foreach($events as $event) : ?>
  <tr>
    <td><span><?php echo $calendar->formatEventTime($event->startTime,'dm'); ?></span></td>
    <td><span><?php echo $calendar->formatEventTime($event->startTime,'time'); ?></span></td>
    <td><?php echo $event->name; ?></td>
  </tr>
<?php endforeach; ?>
</table>

Is there a way I can call functions within my plugin without having to include the WP plugin and creating a new class instance?

A: 

From: http://codex.wordpress.org/Plugin_API

Hooks are provided by WordPress to allow your plugin to 'hook into' the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion. There are two kinds of hooks:

  1. Actions: Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API.
  2. Filters: Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. Your plugin can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API.

    Actions

Actions are triggered by specific events that take place in WordPress, such as publishing a post, changing themes, or displaying a page of the admin panel. Your plugin can respond to the event by executing a PHP function, which might do one or more of the following:

* Modify database data
* Send an email message
* Modify what is displayed in the browser screen (admin or end-user) 

The basic steps to making this happen (described in more detail below) are:

  1. Create the PHP function that should execute when the event occurs, in your plugin file.
  2. Hook to the action in WordPress, by calling add_action()
  3. Put your PHP function in a plugin file, and activate it.

EXAMPLE:

function email_friends($post_ID)  {
    $friends = '[email protected],[email protected]';
    mail($friends, "sally's blog updated", 
      'I just put something on my blog: http://blog.example.com');
    return $post_ID;
}

Hook to WordPress

After your function is defined, the next step is to "hook" or register it with WordPress. To do this, call add_action() in the global execution space of your plugin file:

add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] );

where:

hook_name The name of an action hook provided by WordPress, that tells what event your function should be associated with. your_function_name The name of the function that you want to be executed following the event specified by hook_name. This can be a standard php function, a function present in the WordPress core, or a function defined by you in the plugin file (such as 'email_friends' defined above). priority An optional integer argument that can be used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. accepted_args An optional integer argument defining how many arguments your function can accept (default 1), useful because some hooks can pass more than one argument to your function. This parameter is new in release 1.5.1.

In the example above, we would put the following line in the plugin file:

add_action ( 'publish_post', 'email_friends' );
Todd Moses
I know about hooks. But here thre is no trigger. I'm just retrieving data from a DB table when I load a specific template. So I think Johns tip on using `do_shortcode('shortcode-handle')` is the best way to go.
Steven
+1  A: 

In order to execute shortcode inside a template, use the function do_shortcode('[my-shortcode-handle]'). Your shortcode needs to be registered as like normal (see WordPress codex on shortcode API) before you can use this in the template. Any attributes, inside content, etc. should be in there as well.

echo do_shortcode( '[my-shortcode foo="bar"]Shortcode content[/my-shortcode]' );

Also, remember to echo the return (or at least assign it to a variable), since it only returns the shortcode's output.

John P Bloch