Created
December 1, 2017 22:30
-
-
Save Magnacarter/198ab650752f48850735f9478ced2eb0 to your computer and use it in GitHub Desktop.
Implement custom-fields library class
This file contains 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 | |
/** | |
* Widget custom fields class | |
* | |
* Extends Custom_Fields class | |
* | |
* @since 1.0.0 | |
* @author Stellar Blue Technologies | |
* @licence GNU-2.0+ | |
*/ | |
/** | |
* Class Widget custom fields | |
*/ | |
class Widget_Custom_Fields extends Custom_Field { | |
/** | |
* Hold instance of class | |
* | |
* @var $instance | |
*/ | |
public static $instance; | |
/** | |
* @var $widget_prefix | |
*/ | |
public $widget_prefix = 'widget_'; | |
/** | |
* @var array $widget_meta_fields | |
*/ | |
public $widget_meta_fields = []; | |
/** | |
* Widget Custom Fields constructor. | |
* | |
* @since 1.0.0 | |
*/ | |
public function __construct() { | |
} | |
/** | |
* Get custom meta fields | |
* | |
* Define the custom fields for the CPT | |
* | |
* @since 1.0.0 | |
* @return array $custom_meta_fields | |
*/ | |
public function get_widget_meta_fields() { | |
$prefix = $this->widget_prefix; | |
$this->licensee_meta_fields = array( | |
array( | |
'label' => 'Widget', | |
'desc' => 'Do something with the Widget text.', | |
'id' => $prefix . 'region', | |
'type' => 'text' | |
), | |
array( | |
'label' => 'Widget Paragraph', | |
'desc' => 'Tell us about the Widget in more detail.', | |
'id' => $prefix . 'paragraph', | |
'type' => 'textarea' | |
), | |
array( | |
'label' => 'Widget Image', | |
'desc' => 'Add a portrait for the Widget.', | |
'id' => $prefix . 'image', | |
'type' => 'image' | |
), | |
); | |
return $this->licensee_meta_fields; | |
} | |
/** | |
* Render widget fields | |
* | |
* Loop through the array of custom fields assigned in custom-field.php | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
public function render_widget_fields( $user ) { | |
$custom_fields = $this->get_widgets_meta_fields(); | |
$post_type = 'user'; | |
$id = $user->ID; | |
?> | |
<input type="hidden" name="widget_meta_box_nonce" value="<?php echo wp_create_nonce( basename( __FILE__ ) ); ?>"/> | |
<h3>Widget Fields</h3> | |
<?php | |
$this->render_custom_fields( $custom_fields, $post_type, $id ); | |
} | |
/** | |
* Return active instance of Licensee Custom Fields, create one if it doesn't exist | |
* | |
* @since 1.0.0 | |
* @return object Licensee_Custom_Fields | |
*/ | |
public static function get_instance() { | |
if ( empty( self::$instance ) ) { | |
$class = __CLASS__; | |
self::$instance = new $class; | |
} | |
return self::$instance; | |
} | |
} | |
Widget_Custom_Fields::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// js
https://gist.github.com/Magnacarter/68259b98590027bdb4d55596e8f2f53c
// custom fields library
https://gist.github.com/Magnacarter/ad8e2fa66fd2018017165ffe573a9067