wordpress 如何从 WP 模板中的插件调用函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3781458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:39:02  来源:igfitidea点击:

How do I call functions from my Plugin in WP template?

wordpresswordpress-plugin

提问by Steven

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?

有没有一种方法可以在我的插件中调用函数而不必包含 WP 插件并创建新的类实例?

回答by John P Bloch

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.

为了在模板内执行短代码,请使用函数do_shortcode('[my-shortcode-handle]'). 您的简码需要像往常一样注册(请参阅简码 API 上的 WordPress 法典),然后才能在模板中使用它。任何属性、内部内容等也应该在那里。

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.

另外,请记住回显返回值(或至少将其分配给变量),因为它只返回简码的输出。

回答by Todd Moses

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

来自: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:

钩子由 WordPress 提供,允许您的插件“钩入”WordPress 的其余部分;也就是说,在特定时间调用插件中的函数,从而使插件运行。有两种钩子:

  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

  1. 动作:动作是 WordPress 核心在执行期间或特定事件发生时在特定点启动的钩子。您的插件可以使用 Action API 指定在这些点执行其一个或多个 PHP 函数。
  2. 过滤器:过滤器是 WordPress 启动的钩子,用于在将文本添加到数据库或将其发送到浏览器屏幕之前修改各种类型的文本。您的插件可以使用过滤器 API 指定在这些时间执行其一个或多个 PHP 函数以修改特定类型的文本。

    行动

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:

操作由 WordPress 中发生的特定事件触发,例如发布帖子、更改主题或显示管理面板页面。您的插件可以通过执行 PHP 函数来响应事件,该函数可能执行以下一项或多项操作:

* 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.
  1. 在您的插件文件中创建应在事件发生时执行的 PHP 函数。
  2. 通过调用 add_action() 连接到 WordPress 中的操作
  3. 将您的 PHP 函数放在插件文件中,并激活它。

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

连接到 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:

定义函数后,下一步是“挂钩”或将其注册到 WordPress。为此,请在插件文件的全局执行空间中调用 add_action():

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.

hook_name WordPress 提供的动作钩子的名称,它告诉你的函数应该与什么事件相关联。your_function_name 要在 hook_name 指定的事件之后执行的函数的名称。这可以是标准的 php 函数、WordPress 核心中的函数或您在插件文件中定义的函数(例如上面定义的“email_friends”)。priority 一个可选的整数参数,可用于指定与特定操作关联的函数的执行顺序(默认值:10)。较低的数字对应于较早的执行,具有相同优先级的函数按照它们添加到操作的顺序执行。Accepted_args 一个可选的整数参数,定义您的函数可以接受多少个参数(默认为 1),很有用,因为某些钩子可以将多个参数传递给您的函数。此参数是 1.5.1 版中的新参数。

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

在上面的示例中,我们将以下行放在插件文件中:

add_action ( 'publish_post', 'email_friends' );