Skip to content

Instantly share code, notes, and snippets.

@Magnacarter
Created December 1, 2017 22:30
Show Gist options
  • Save Magnacarter/198ab650752f48850735f9478ced2eb0 to your computer and use it in GitHub Desktop.
Save Magnacarter/198ab650752f48850735f9478ced2eb0 to your computer and use it in GitHub Desktop.
Implement custom-fields library class
<?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();
@Magnacarter
Copy link
Author

Magnacarter commented Dec 1, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment