Pluggable Functions

来自WordPress中文文档
Zhuozuran讨论 | 贡献2013年8月2日 (五) 04:22的版本 (1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

Pluggable functions were introduced in WordPress 1.5.1 These functions let you override certain core functions via plugins. The most up-to-date list of core functions that WordPress allows plugins to override is available at onlyincludecodewp-includes/pluggable.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn. WordPress loads the built-in functions only if they are undefined after all plugins have been loaded.

Pluggable functions are no longer being added to WordPress core. All new functions instead use filters on their output to allow for similar overriding of their functionality.

Note: A function can only be reassigned this way once, so you can’t install two plugins that plug the same function for different reasons. For safety, it is best to always wrap your functions with if ( !function_exists() ), otherwise you will produce fatal errors on plugin activation.


Full list of Pluggable Functions:

Version 3.5

Reference

get_currentuserinfo() 
Grabs the information of the current logged in user, if there is one. Essentially a wrapper for get_userdata(), but it also stores information in global variables.
get_userdata($userid) 
Pulls user information for the specified user from the database.
wp_mail($to, $subject, $message, $headers = '') 
A convenient wrapper for PHP's mail function.
wp_login($username, $password, $already_md5 = false) 
Returns true if the specified username and password correspond to a registered user.
auth_redirect() 
If a user is not logged in, he or she will be redirected to WordPress' login page before being allowed to access content on the page from which this function was called. Upon successfully logging in, the user is sent back to the page in question.
wp_redirect($location) 
Redirects a browser to the absolute URI specified by the $location parameter.
wp_notify_postauthor($comment_id, $comment_type='')
Emails the author of the comment's post the content of the comment specified.
wp_notify_moderator($comment_id) 
Informs the administrative email account that the comment specified needs to be moderated. See Administration > Settings > General.

Example

An example of what you can do with a pluggable function is replace the default email handler. To do this, you'd need to write a plugin that defines a wp_mail() function. The default wp_mail() function looks like this:

function wp_mail( $to, $subject, $message, $headers = '' ) {
  if( $headers == '' ) {
    $headers = "MIME-Version: 1.0\n" .
      "From: " . get_settings('admin_email') . "\n" . 
      "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
  }

  return @mail( $to, $subject, $message, $headers );
}

But, for example, if you wanted to CC all your mail to another address, you could use this code in a plugin:

if( ! function_exists('wp_mail') ) {
  function wp_mail( $to, $subject, $message, $headers = '' ) {
    if( $headers == '' ) {
      $headers = "MIME-Version: 1.0\n" .
        "From: " . get_settings('admin_email') . "\n" . 
        "Cc: dummy@example.com\n" .
        "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
    }

    return @mail($to, $subject, $message, $headers);
  }
}

Notice that if you plug a core function like this the original is no longer available. I.e., the elegant solution here would have been to write a function that tacks our Cc header on the end of the exising $headers string then call the original wp_mail() with the extra header. However this would not work as the original wp_mail() does not exist if you plug it.