or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress Plugin Application Program Interface (API). http://codex.wordpress.org/Writing_a_Plugin
guidelines Code formatting. WordPress has Coding Standards (PHP, HTML, CSS, and JavaScript) but at the very least: be consistent with your formatting http://codex.wordpress.org/WordPress_Coding_Standards Comments! WordPress is open source, write comments as though a complete stranger is going to read them. Break up code into files/folders as appropriate (e.g. classes, assets, etc.) Continued… Are there rules to writing plugins?
you as possible (it will do a ton) Good function/variable names: prefixed (if not in a class), descriptive In other words: do everything in your power to prevent collisions with other code, especially WordPress core itself Use hooks! Learn by example: well written plugins like bbPress Are there rules to writing plugins?
Where can we find out about what hooks are available? http://codex.wordpress.org/Plugin_API http://codex.wordpress.org/Plugin_API/Action_Reference http://codex.wordpress.org/Plugin_API/Filter_Reference How do you use hooks? Hooks make extending WordPress possible
to swap ‘hello’ with ‘hey there’ add_filter(); // you utilize a filter in place apply_filters(); // you implement a hook-able filter How do you write your own filters? Applying filters where necessary to make customization available Filters aim to modify data
code base that allow us to essentially enqueue our own functions to happen at certain points in time during a WordPress page execution. This allows us to effectively add our function to WordPress core without editing the files themselves. That’s a big deal. Hook-able points in time during execution
properly enqueue a custom stylesheet https://codex.wordpress.org/Function_Reference/wp_enqueue_style#Examples How to use actions Actions make my aspects of plugin development possible
concept of ‘enqueueing’ your functions into WordPress core Remember that filters filter, actions are actionable Keep in mind data integrity, return the expected type else you can wreak havoc down the line. They’re kind of a big deal
your plugin a UI UI is nice but hooks can usually suffice and shouldn’t be thought of as an inconvenience, they make WordPress WordPress Hooks are not the end-all-be-all, sometimes you want to create a settings screen or even an entire Menu section (which is honestly super rare) — be humble when making that decision.
settings The WordPress admin has a Settings menu we can utilize WordPress also has a Settings API. It needs some love, but it’s there. https://codex.wordpress.org/Settings_API
about it? Mind the database, don’t abuse it WordPress has built in functionality that makes data storage simple, automatic serialization/unserialization Use as few records as possible (minding database performance) Read and write as few times as possible http://kovshenin.com/2012/the-wordpress-settings-api/
baked right into WordPress __() and _e() make everything work Language packs can be programmatically generated by native speakers and sent to you for inclusion http://codex.wordpress.org/Writing_a_Plugin#Internationalizing_Your_Plugin
settings? WordPress has you covered with object metadata Posts, Comments and Users have a dedicated metadata API, use it wisely. http://codex.wordpress.org/Metadata_API NOTE: While the generic Metadata API exists, you should not use it directly! There are meta functions for each object type available.
metadata? Metadata is great but it’s slow, as a result it’s often abused Keep in mind that it’s very valuable to have metadata be stored in such a way that facilitates outside queries (i.e. keep metadata separated) That goes against optimization in a way, but it increases the usefulness of your plugin if other developers can use your metadata in other ways (e.g. WP_Query meta_query) If your data is locked inside a serialized array, you lose all benefit of querying by specific meta_values when running custom queries http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Storage of comment and user metadata works in the same way. WordPress has hooks available to customize both the forms used for editing those objects, and you can manipulate the workflow in the same way.
sanitization (and escaping) is extremely important Do not try and do it yourself, WordPress has you covered https://codex.wordpress.org/Data_Validation NO data is trustworthy, especially when you consider hooks.
nothing wrong with procedural plugins, in fact there are some reasons to use procedural patterns when building WordPress plugins. Utilizing classes, however, makes things a bit more organized and put together. Let’s convert No Hello into a class-based plugin…
available — .org? GitHub? • Unique (but helpful) actions — e.g. plugin activation • Database queries (WordPress has an awesome class for this) • Debugging and optimizing your plugin • Where to go when you have no idea where to start • WordPress Cron … anything else?