Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active December 26, 2022 07:02
Show Gist options
  • Save igorbenic/5aeedd70931fc4c314d44bc993f0154e to your computer and use it in GitHub Desktop.
Save igorbenic/5aeedd70931fc4c314d44bc993f0154e to your computer and use it in GitHub Desktop.
How to Create Extendable WordPress Forms | http://www.ibenic.com/create-extendable-wordpress-forms/
<?php
abstract class WordPress_Extendable_Form {
/**
* Array of errors
* @var array
*/
protected $errors = array();
/**
* Valid label
* @var boolean
*/
protected $valid = false;
/**
* Array of fields
* @var array
*/
protected $fields = array();
/**
* Form ID
* @var null | string
*/
protected $id = null;
/**
* Posted data
* @var array
*/
protected $posted = array();
public function __construct( $id ) {
$this->id = $id;
$this->errors = new WP_Error();
$this->set_default_fields();
$this->fields = apply_filters( 'ibenic_form_fields_' . $this->id, $this->fields, $this );
add_action( 'init', array( $this, 'form_submitted' ) );
}
// ...
}
<?php
abstract class WordPress_Extendable_Form {
// ...
public function form_submitted() {
if( isset( $_POST[ $this->id ] ) ) {
$this->posted = $_POST;
$this->form_validate();
if( count( $this->errors->get_error_messages() ) == 0 ) {
$this->form_is_valid();
do_action( 'ibenic_form_is_valid_' . $this->id, $this );
} else {
$this->valid = false;
}
}
}
// ...
}
<?php
abstract class WordPress_Extendable_Form {
// ...
public function form_validate() {
foreach ( $this->fields as $order => $field ) {
$field_posted = $this->posted[ $field['id'] ];
if( isset( $field['repeat'] ) && $field['repeat'] == 'true' ) {
$field_posted_repeat = $this->posted[ $field['id'] . '_repeat' ];
if( $field_posted_repeat != '' && ( $field_posted != $field_posted_repeat ) ) {
$this->errors->add( $field['id'], sprintf( __( 'Repeat %s and %s are different', 'themeasap' ), $field['label'], $field['label'] ) );
}
}
if( $field['sanitize'] != '' ) {
$sanitize_function = $field['sanitize'];
$field_posted = $sanitize_function( $field_posted );
}
if( $field['required'] == 'true' && ( $field_posted == '' || $field_posted == null) ) {
$this->errors->add( $field['id'], sprintf( __( '%s is empty', 'themeasap' ), $field['label'] ) );
}
$this->posted[ $field['id'] ] = $field_posted;
}
}
public function form_is_valid() {
}
// ...
}
<?php
abstract class WordPress_Extendable_Form {
// ...
public function form_render() {
echo '<form id="' . $this->id . '" method="post" action="" >';
foreach ( $this->fields as $order => $field ) {
echo '<p>';
echo '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
echo '<input type="' . $field['type'] . '" name="' . $field['id'] . '" id="' . $field['id'] . '" value="" />';
if( ! $this->valid ) {
$field_errors = $this->errors->get_error_messages( $field['id'] );
foreach ( $field_errors as $error ) {
echo '<span class="ibenic_form_error">' . $error . '</span>';
}
}
echo '</p>';
if( $field['repeat'] == 'true' ) {
echo '<p>';
echo '<label for="' . $field['id'] . '_repeat">' . __( 'Repeat', 'ibenic') . ' ' . $field['label'] . '</label>';
echo '<input type="' . $field['type'] . '" name="' . $field['id'] . '_repeat" id="' . $field['id'] . '_repeat" value="" />';
echo '</p>';
}
}
do_action( 'ibenic_form_render_' . $this->id );
echo '<button type="submit" class="button button-primary" name="' . $this->id . '">' . __( 'Submit', 'ibenic' ) . '</button>';
echo "</form>";
}
public function set_default_fields() {
}
}
<?php
class IBenic_Form_Register extends WordPress_Extendable_Form {
public function __construct( $id ) {
parent::__construct($id);
add_action('ibenic_register_form_display', array( $this, 'form_render') );
}
// ...
}
<?php
class IBenic_Form_Register extends WordPress_Extendable_Form {
// ...
public function set_default_fields() {
$this->fields = array(
0 => array(
'id' => 'ibenic_email',
'sanitize' => 'sanitize_email',
'required' => 'true',
'type' => 'email',
'label' => __( 'Email', 'ibenic' )
),
1 => array(
'id' => 'ibenic_username',
'sanitize' => 'sanitize_text_field',
'required' => 'true',
'type' => 'text',
'label' => __( 'Username', 'ibenic' )
),
2 => array(
'id' => 'ibenic_password',
'sanitize' => '',
'required' => 'true',
'repeat' => 'true',
'type' => 'password',
'label' => __( 'Password', 'ibenic' )
));
}
// ...
}
<?php
class IBenic_Form_Register extends WordPress_Extendable_Form {
// ...
/**
* Form is Valid, let's create our user
*/
public function form_is_valid() {
$email = $this->posted[ 'ibenic_email'];
$username = $this->posted[ 'ibenic_username'];
$password = $this->posted[ 'ibenic_password'];
$user_id = wp_insert_user( array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email
));
if( ! is_wp_error( $user_id ) ) {
$credentials = array(
'user_login' => $username,
'user_password' => $password);
$secure_cookie = false;
if( is_ssl() ) {
$secure_cookie = true;
}
$redirect_url = get_home_url();
$redirect_url = apply_filters( 'ibenic_register_form_redirect_link', $redirect_url );
// Send registration email
wp_new_user_notification( $user_id, $password );
// Login the user
wp_signon( $credentials, $secure_cookie );
// Redirect user
wp_redirect( $redirect_url );
exit;
} else {
$this->valid = false;
$this->add_error( $user_id->get_error_code(), $user_id->get_error_message() );
}
}
}
<?php
add_filter( 'ibenic_form_fields_register_form', 'ibenic_register_form_add_field' );
function ibenic_register_form_add_field( $fields ) {
$fields[4] => array(
'id' => 'ibenic_new_field',
'required' => 'false',
'type' => 'text',
'label' => __( 'New Field', 'ibenic' )
);
return $fields;
}
<?php
add_filter( 'the_content', 'ibenic_form_register_render' );
function ibenic_form_register_render( $content ){
if( is_page( 'register' ) ) {
ob_start();
do_action( 'ibenic_register_form_display' );
$content = ob_get_clean();
ob_end_flush();
}
return $content;
}
<?php
new IBenic_Form_Register( 'register_form' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment