xwp/wp-safe-input

0.0.1 2019-02-12 13:30 UTC

This package is auto-updated.

Last update: 2024-04-13 02:55:10 UTC


README

Build Status Coverage Status

A helper library for validating and sanitizing form input data.

Setup

Use Composer to add this library to your project:

composer require xwp/wp-safe-input

Usage

See the sample plugin in the example directory.

The core logic looks like this:

use XWP\SafeInput\PostMeta;
use XWP\SafeInput\Request;

add_action( 'save_post', function ( $post_id ) {
	$request = new Request( INPUT_POST );
	$meta = new PostMeta( $post_id );

	if ( $request->verify_nonce( 'nonce-action', 'nonce-input-name' ) && $meta->can_save() ) {
		if ( 'on' === $request->param( 'input-field-name' ) ) {
			// Update post meta value.
		} else {
			// Delete post meta value.
		}
	}
} );

TODO: Document what happens in the example above.