Build WordPress plugins from scratch: actions and filters, shortcodes, settings pages, custom post types, meta boxes, AJAX, REST endpoints, custom tables, cron and blocks.
Lessons
Secure plugins follow one rule for data: sanitise and validate everything that comes in — from forms, URLs, cookies and APIs — and escape everything that goes out to the browser, at the moment it is printed.
| Step | When | Question | Example |
|---|---|---|---|
| Validate | Input | Is this acceptable at all? | Is the ISBN 10 or 13 digits? |
| Sanitise | Input | Make it safe to store | Strip tags from a name |
| Escape | Output | Make it safe for this exact spot in HTML | esc_attr() inside an attribute |
WordPress adds backslashes to everything in $_POST, $_GET and $_COOKIE for historical reasons. Remove them with wp_unslash() before sanitising, or a name like O'Brien is stored as O\'Brien.
<?php
$title = isset( $_POST['gdc_title'] )
? sanitize_text_field( wp_unslash( $_POST['gdc_title'] ) )
: '';| Input is | Sanitise with |
|---|---|
| A single line of text | sanitize_text_field() |
| Multi-line text | sanitize_textarea_field() |
| An email address | sanitize_email() |
| A URL to store | esc_url_raw() |
| A positive integer / ID | absint() |
| A key or slug | sanitize_key() / sanitize_title() |
| A hex colour | sanitize_hex_color() |
| A file name | sanitize_file_name() |
| HTML that may contain post-safe tags | wp_kses_post() |
Sanitising makes data safe; validating makes it correct. An ISBN with letters in it is "safe" after sanitize_text_field() but still wrong:
<?php
function gdc_books_validate_isbn( $raw ) {
$isbn = preg_replace( '/[^0-9X]/', '', strtoupper( $raw ) );
if ( ! in_array( strlen( $isbn ), array( 10, 13 ), true ) ) {
return new WP_Error( 'gdc_books_bad_isbn', __( 'An ISBN must have 10 or 13 digits.', 'gdc-books' ) );
}
return $isbn;
}
$isbn = gdc_books_validate_isbn( wp_unslash( $_POST['gdc_isbn'] ?? '' ) );
if ( is_wp_error( $isbn ) ) {
// Show $isbn->get_error_message() and do not save.
}Tip: Prefer allow-lists to block-lists. Checking a value is one of array( 'date', 'title' ) is airtight; trying to list every bad value never is.Even data you sanitised on the way in must be escaped on the way out — it might have been changed in the database by another plugin, or saved before your validation existed:
<?php $isbn = get_post_meta( $post->ID, '_gdc_books_isbn', true ); ?>
<input type="text" name="gdc_isbn" value="<?php echo esc_attr( $isbn ); ?>">
<a href="<?php echo esc_url( 'https://openlibrary.org/isbn/' . rawurlencode( $isbn ) ); ?>">
<?php echo esc_html( $isbn ); ?>
</a>Sanitising text does not make it safe for SQL. Any query that includes a variable must use $wpdb->prepare() (lesson 20):
<?php
global $wpdb;
// Wrong — SQL injection:
// $wpdb->get_results( "SELECT * FROM {$wpdb->posts} WHERE post_title = '$title'" );
// Right:
$rows = $wpdb->get_results(
$wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", $title, 'gdc_book' )
);WP_Error and sanitises.<script>alert(1)</script> and confirm it is stored without tags and displayed as text.$_POST and $_GET in your plugin and check each passes through wp_unslash() and a sanitiser.In accordance with Section 6 & Section 7 of the DPDP Act 2023, third-party analytics and marketing trackers are strictly gated behind your affirmative consent. You may withdraw consent at any time under Section 6(4).
Essential for session authentication (ci_session), Cross-Site Request Forgery security (csrf_cookie_name), and Cloudflare DDoS/bot prevention. Cannot be disabled.
Gated under DPDP Section 7. Allows Google Analytics 4 (Google Consent Mode v2) to measure aggregate, anonymized tool usage.
Enables browser local storage to preserve your theme selection (dark/light mode) and code editor layout between visits.
Used to display developer sponsorships and native tool partner notices.