This last section contains various suggestions regarding Plugin development.
- The code of a WordPress Plugin should follow the WordPress Coding Standards. Please consider the Inline Documentation Standards as well.
- All the functions in your Plugin need to have unique names that are different from functions in the WordPress core, other Plugins, and themes. For that reason, it is a good idea to use a unique function name prefix on all of your Plugin’s functions. A far superior possibility is to define your Plugin functions inside a class (which also needs to have a unique name).
- Do not hardcode the WordPress database table prefix (usually “wp_”) into your Plugins. Be sure to use the $wpdb->prefix variable instead.
- Database reading is cheap, but writing is expensive. Databases are exceptionally good at fetching data and giving it to you, and these operations are (usually) lightning quick. Making changes to the database, though, is a more complex process, and computationally more expensive. As a result, try to minimize the amount of writing you do to the database. Get everything prepared in your code first, so that you can make only those write operations that you need.
- Use WordPress’ APIs instead of using direct SQL where possible. For example, use get_posts() or new WP_Query() instead of SELECT * FROM {$wpdb->prefix}_posts.
- Use the existing database tables instead of creating new custom tables if possible. Most use-cases can be accomplished with custom post types and meta data, custom taxonomy and/or one of the other standard tables and using the standard tables provides a lot of UI and other functionality “for free.” Think very carefully before adding a table because it adds complexity to your plugin that many users and site builders prefer to avoid.
- SELECT only what you need. Even though databases fetch data blindingly fast, you should still try to reduce the load on the database by only selecting that data which you need to use. If you need to count the number of rows in a table don’t SELECT * FROM, because all the data in all the rows will be pulled, wasting memory. Likewise, if you only need the post_id and the post_author in your Plugin, then just SELECT those specific fields, to minimize database load. Remember: hundreds of other processes may be hitting the database at the same time. The database and server each have only so many resources to spread around amongst all those processes. Learning how to minimize your Plugin’s hit against the database will ensure that your Plugin isn’t the one that is blamed for abuse of resources.
- Eliminate PHP errors in your plugin. Add define('WP_DEBUG', true); to your wp-config.php file, try all of your plugin functionality, and check to see if there are any errors or warnings. Fix any that occur, and continue in debug mode until they have all been eliminated.
- Try not to echo <script> and <style> tags directly – instead use the recommended wp_enqueue_style() and wp_enqueue_script() functions. They help eliminate including duplicate scripts and styles as well as introduce dependency support. See posts by the following people for more info: Ozh Richard, Artem Russakovskii, and Vladimir Prelovac.
Source: https://codex.wordpress.org/Plugins
External Resources
- Anatomy of a WordPress Plugin Featuring Hello Dolly (09March11)
- Top 10 Most Common Coding Mistakes in WordPress Plugins (11SEP09)
- WordPress 2.0.3: Nonces (Secure your forms with nonces) (02JUN06)
- Simplified AJAX For WordPress Plugin Developers using Jquery(10APR08)
- “Criando Plugins para WordPress” by Feijão Costa (in Brazilian Portuguese) (10JUN15)
- 12 part “How to Write a WordPress Plugin” at DevLounge.net by Ronald Huereca
- How to create WordPress Plugin from a scratch (9AUG07)
- HookPress, a plugin that enables extending WordPress in languages other than PHP via webhooks. (26SEP09)
- How To Include CSS and JavaScript Conditionally And Only When Needed By The Posts (13JAN10)
- Demonstrating how to use the Settings API, WP_Http, and Pseudo-cron (01MAR10)
- Make WordPress Plugins – official blog with information for plugin authors (25Jun12)
- WordPress Generator – GenerateWP provides user-friendly tools for developers to create advanced systems built on WordPress. (15JAN13)
- WP Settings API Generator – WP Settings API is a tool for developers to easily create options pages for your plugins. (10JUL14)
- How to write a WordPress Plugin – A simple how-to guide to writing a class based WordPress plugin with a settings page, custom post type and metaboxes that separates business and template logic in an easy to understand way. (31Jan13)
http://www.hongkiat.com/blog/beginners-guide-to-wordpress-plugin-development/
liked this article?
- only together we can create a truly free world
- plz support dwaves to keep it up & running!
- (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
- really really hate advertisement
- contribute: whenever a solution was found, blog about it for others to find!
- talk about, recommend & link to this blog and articles
- thanks to all who contribute!