Last active
July 15, 2016 02:47
-
-
Save ramiabraham/eacbefa24ed918ec3a9acb88cd444d7f to your computer and use it in GitHub Desktop.
A simple, fairly un-opinionated starter abstract class and subclass example, for generating user meta in WordPress.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: User meta starter class. | |
* Plugin URI: https://dennys.com | |
* Description: User meta | |
* Author: ramiabraham | |
* Version: 1.0 | |
* Text Domain: user-meta-starter | |
* Domain Path: languages | |
*/ | |
// Exit if accessed directly | |
if ( ! defined('ABSPATH') ) { | |
exit; | |
} | |
/** | |
* Copy or extend below | |
*/ | |
abstract class User_Meta_Starter { | |
public $fieldset_title; | |
public $field_1_title; | |
public $field_1_key; | |
public $field_1_type; | |
public $field_2_title; | |
public $field_2_key; | |
public $field_2_type; | |
public $field_3_title; | |
public $field_3_key; | |
public $field_3_type; | |
public function __construct() { | |
$this->set_fields(); | |
$this->init(); | |
} | |
abstract public function set_fields(); | |
public function init() { | |
add_action( 'personal_options_update', array( $this, 'save' ) ); | |
add_action( 'edit_user_profile_update', array( $this, 'save' ) ); | |
add_action( 'show_user_profile', array( $this, 'profile_fields' ) ); | |
add_action( 'edit_user_profile', array( $this, 'profile_fields' ) ); | |
} | |
public function save( $user_id ) { | |
update_user_meta( $user_id, $this->field_1_key, sanitize_text_field( $_POST[ $this->field_1_key ] ) ); | |
update_user_meta( $user_id, $this->field_2_key, sanitize_text_field( $_POST[ $this->field_2_key ] ) ); | |
update_user_meta( $user_id, $this->field_3_key, sanitize_text_field( $_POST[ $this->field_3_key ] ) ); | |
} | |
public function profile_fields( $user ) { | |
?> | |
<h3><?php echo sanitize_text_field( $this->fieldset_title ); ?></h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="<?php echo $this->field_1_key; ?>"><?php echo sanitize_text_field( $this->field_1_title ); ?></label></th> | |
<td> | |
<?php if ( $this->field_1_type === 'text' ) { ?> | |
<input type="text" name="<?php echo $this->field_1_key; ?>" value="<?php echo esc_attr( get_the_author_meta( $this->field_1_key, $user->ID ) ); ?>" class="regular-text" /> | |
<?php } else { ?> | |
<textarea rows="5" cols="30" name="<?php echo $this->field_1_key; ?>" value="<?php echo esc_attr( get_the_author_meta( $this->field_1_key, $user->ID ) ); ?>" class="paragraph-text" /> | |
<?php echo esc_attr( get_the_author_meta( $this->field_1_key, $user->ID ) ); ?> | |
</textarea> | |
<?php } ?> | |
</td> | |
</tr> | |
<tr> | |
<th><label for="<?php echo $this->field_2_key; ?>"><?php echo sanitize_text_field( $this->field_2_title ); ?></label></th> | |
<td> | |
<?php if ( $this->field_2_type === 'text' ) { ?> | |
<input type="text" name="<?php echo $this->field_2_key; ?>" value="<?php echo esc_attr(get_the_author_meta( $this->field_2_key, $user->ID ) ); ?>" class="regular-text" /> | |
<?php } else { ?> | |
<textarea rows="5" cols="30" name="<?php echo $this->field_2_key; ?>" value="<?php echo esc_attr( get_the_author_meta( $this->field_2_key, $user->ID ) ); ?>" class="paragraph-text" /> | |
<?php echo esc_attr( get_the_author_meta( $this->field_2_key, $user->ID ) ); ?> | |
</textarea> | |
<?php } ?> | |
</td> | |
</tr> | |
<tr> | |
<th><label for="<?php echo $this->field_3_key; ?>"><?php echo sanitize_text_field( $this->field_3_title ); ?></label></th> | |
<td> | |
<?php if ( $this->field_3_type === 'text' ) { ?> | |
<input type="text" name="<?php echo $this->field_3_key; ?>" value="<?php echo esc_attr(get_the_author_meta( $this->field_3_key, $user->ID ) ); ?>" class="regular-text" /> | |
<?php } else { ?> | |
<textarea rows="5" cols="30" name="<?php echo $this->field_3_key; ?>" value="<?php echo esc_attr( get_the_author_meta( $this->field_3_key, $user->ID ) ); ?>" class="paragraph-text" /> | |
<?php echo esc_attr( get_the_author_meta( $this->field_3_key, $user->ID ) ); ?> | |
</textarea> | |
<?php } ?> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
} | |
/** | |
* Customize | |
*/ | |
class User_Meta_Custom extends User_Meta_Starter { | |
public function set_fields() { | |
$this->field_1_type = 'text'; | |
$this->field_2_type = 'text'; | |
$this->field_3_type = 'textarea'; | |
$this->fieldset_title = __( 'Here are some fields', 'user-meta-starter' ); | |
$this->field_1_title = __( 'Here is field one.', 'user-meta-starter' ); | |
$this->field_1_key = 'user_meta_starter_1'; | |
$this->field_2_title = __( 'Here is field two.', 'user-meta-starter' ); | |
$this->field_2_key = 'user_meta_starter_2'; | |
$this->field_3_title = __( 'Here is field three.', 'user-meta-starter' ); | |
$this->field_3_key = 'user_meta_starter_3'; | |
} | |
} | |
new User_Meta_Custom; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment