¿Algún momento has oído hablar de WordPress? Connectable functions? Caso contrario, este post debería llamar su atención. En dos palabras, las funciones conectables son WordPress main functions you can override. All these functions are in a single file: «wp-includes / pluggable.php“. Las funciones conectables se introdujeron en WordPress 1.5.1, pero en las versiones más recientes de WordPress este método ya no se utiliza. Las funciones recientes ahora utilizan filters en su salida. Pero todavía puede anular las funciones conectables, y esto es lo que me agradaría cubrir en esta publicación.
What functions?
The connectable functions are:
- auth_redirect
- check_admin_referer
- check_ajax_referer
- get_avatar
- get_currentuserinfo
- get_user_by_email
- get_user_by
- get_userdatabylogin
- get_userdata
- is_user_logged_in
- wp_authenticate
- wp_check_password
- wp_clear_auth_cookie
- wp_create_nonce
- wp_generate_auth_cookie
- wp_generate_password
- wp_get_current_user
- wp_hash_password
- wp_hash
- wp_logout
- wp_mail
- wp_new_user_notification
- wp_nonce_tick
- wp_notify_moderator
- wp_notify_postauthor
- wp_parse_auth_cookie
- wp_password_change_notification
- wp_rand
- wp_redirect
- wp_safe_redirect
- wp_salt
- wp_sanitize_redirect
- wp_set_auth_cookie
- wp_set_current_user
- wp_set_password
- wp_text_diff
- wp_validate_auth_cookie
- wp_validate_redirect
- wp_verify_nonce
You can click on the name of each function to enter its codex page.
How to Override Pluggable Features
Well, this is pretty simple, you just have to create a file inside your plugins that contains an "if (! Function_exists ()) ..." statement and then set the function again. I highly recommend that you copy and paste the original feature when you start. This way, you are sure that the function will work. Here is an empty example:
if (! function_exists ('wp_notify_postauthor')): / ** * Notify an author of a comment / trackback / pingback to one of their articles. * * @since 1.0.0 * * @param int $comment_id Comment ID * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback' * @return bool False if user email does not exist. True on completion. * / function wp_notify_postauthor ($comment_id, $comment_type = '') {/ * This is where you redefine the function * /} endif;
I'd like to talk about the "wp_notify_postauthor ()" function. This is responsible for sending an email to the authors of the publication when a new comment is added. In one of my plugins, the WordPress Issue Manager, I needed to disable this notification, but only a specific custom post type. So, I copied the whole function and basically added this:
if (! function_exists ('wp_notify_postauthor')): / ** * Notify an author of a comment / trackback / pingback to one of their articles. * * @since 1.0.0 * * @param int $comment_id Comment ID * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback' * @return bool False if user email does not exist. True on completion. * / function wp_notify_postauthor ($comment_id, $comment_type = '') {if ($post-> post_type! = 'issue'): / * content of the original function * / endif; } endif;
That's simple, but it works great without having to make big changes or create a full custom function tied to a custom action.
wp_mail ()
Como vio en el listado de funciones conectables, wp_mail () es una función conectable. Esta función es la que se usa para enviar correos electrónicos. En cualquier lugar de WordPress, cuando se envía un email, se usa esta función. Es por esto que personalizarlo puede resultar muy interesante. Por ejemplo, puede utilizar una plantilla html predeterminada para todos los correos electrónicos enviados desde su instalación de WordPress.
You can also blind copy each message to a specific email to have a kind of backup (trust me, this can be useful when someone tells you that they did not receive the message).
wp_authenticate ()
You can also modify wp_authenticate () and add some additional parameters to strengthen the security of your site (brute force attacks, for example).
auth_redirect ()
Esta función es la que comprueba si un Username está conectado y, en de lo contrario, lo redirige a la página de inicio de sesión. Sería bastante sencillo anular la función y redirigir al usuario a una página personalizada, en lugar de la página de inicio de sesión predeterminada (por ejemplo, si desea esconder la carpeta wp-admin).
wp_generate_password ()
This function is the one that automatically generates the passwords. It honestly doesn't require modifying, but now that you know what brute force attacks are, you might be interested in creating more secure passwords. Well, this is the function to improve.
conclusion
Para concluir esta breve publicación sobre las funciones conectables de WordPress, me agradaría destacar el hecho de que las nuevas funciones ya no funcionan así. Como escribí anteriormente, ahora están utilizando filtros. Pero las funciones conectables son funciones importantes en particular cuando se crean complementos verdaderamente específicos. Pero tenga cuidado al utilizar funciones conectables. Si la función recién creada no funciona estupendamente, puede romper una parte de su portal Web (en términos de funcionalidad), por lo tanto pruébelos en todas las condiciones.