Читать книгу Professional WordPress Plugin Development - Brad Williams - Страница 20

Why Reinvent the Wheel?

Оглавление

Another advantage to building plugins is the structure that already exists for your plugin. Many of the common features have already been developed and are ready for use in your plugin. For example, you can take advantage of the built‐in user roles in WordPress. Using the user roles, you can easily restrict your code to execute only if a user is an administrator. Look at this example:

<?php if ( current_user_can( 'manage_options' ) ) { //any code entered here will only be executed IF //user is an administrator } ?>

As you can see, it's easy to verify that a user has proper permissions prior to executing any code in your plugin. You will learn about user accounts and roles in Chapter 9, “Users and User Data.”

As another example, look at sending an email in WordPress. Sure, you could create a new function in your plugin to send email, but why? WordPress has a handy function called wp_mail()for sending email. Look at this example:

<?php $email_to = 'you@example.com'; $email_subject = 'Plugin email example'; $email_message = 'How do you like my new plugin?'; wp_mail( $email_to, $email_subject, $email_message ); ?>

As you can see, sending an email in WordPress couldn't be easier. Unless your plugin needs some customized emailing functionality, you don't need to re‐create this function from scratch. Using this function also ensures the widest adoption for sending emails from WordPress because you use the built‐in function.

Using the available built‐in features of WordPress can greatly reduce the time to develop a plugin. Another advantage of not reinventing the wheel is that this approach more often than not will allow for your plugins to work across a greater number of servers and setups, thereby maximizing compatibility. Don't reinvent the wheel with features that already exist in WordPress.

Professional WordPress Plugin Development

Подняться наверх