How do I call functions from my plugin in a WP template?

I created a calendar plugin and now I want to show a list of events 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 that I can call functions inside my plugin without having to enable the WP plugin and create a new instance of the class?

+5
source share
2 answers

To execute short code inside a template, use the function do_shortcode('[my-shortcode-handle]'). Your shortcode must be registered as normal ( see the WordPress codex on the shortcode API ) before you can use it in the template. Any attributes inside the content, etc. Must also be there.

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

, (, , ), .

+7

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

WordPress, "" WordPress; . :

  • : - , ​​WordPress . , PHP , Action API.
  • : - , WordPress , . , PHP , API .

    < >

, WordPress, , . , PHP, :

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

( ):

  • PHP, .
  • WordPress, add_action()
  • PHP .

:

function email_friends($post_ID)  {
    $friends = 'bob@example.org,susie@example.org';
    mail($friends, "sally blog updated", 
      'I just put something on my blog: http://blog.example.com');
    return $post_ID;
}

WordPress

, , "" WordPress. add_action() :

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

:

hook_name    , WordPress, , . your_function_name    , , , hook_name. php, , WordPress, , (, "email_friends", ).    , , , ( : 10). , , . accepted_args    , , ( 1), , . 1.5.1.

:

add_action ( 'publish_post', 'email_friends' );
+2

All Articles