Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active July 18, 2024 21:00
Show Gist options
  • Save Crocoblock/5ce2bc0ad8e0db7b6ce7b26ddc27e9bd to your computer and use it in GitHub Desktop.
Save Crocoblock/5ce2bc0ad8e0db7b6ce7b26ddc27e9bd to your computer and use it in GitHub Desktop.
JetEngine Register meta box for CPT/taxonomy/user programmatically
<?php
add_action( 'jet-engine/meta-boxes/register-instances', function( $meta ) {
//all field types are given for the reference, you can add any amount of fields of any type
$meta_fields = array (
array (
'title' => 'reference-text',
'name' => 'reference-text',
'object_type' => 'field',
'width' => '100%',
'options' =>array (),
'type' => 'text',
'description' => 'description',
'max_length' => '123',
'default_val' => '1',
'quick_editable' => true,
),
array (
'title' => 'reference-textarea',
'name' => 'reference-textarea',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'textarea',
),
array (
'title' => 'reference-wysiwyg',
'name' => 'reference-wysiwyg',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'wysiwyg',
),
array (
'title' => 'reference-date',
'name' => 'reference-date',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'date',
),
array (
'title' => 'reference-date-timestamp',
'name' => 'reference-date-timestamp',
'object_type' => 'field',
'options' =>
array (
),
'type' => 'date',
'width' => '100%',
'is_timestamp' => true,
),
array (
'title' => 'reference-time',
'name' => 'reference-time',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'time',
),
array (
'title' => 'reference-datetime',
'name' => 'reference-datetime',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'datetime-local',
),
array (
'title' => 'reference-datetime-timestamp',
'name' => 'reference-datetime-timestamp',
'object_type' => 'field',
'options' =>
array (
),
'type' => 'datetime-local',
'width' => '100%',
'is_timestamp' => true,
),
array (
'title' => 'reference-switcher',
'name' => 'reference-switcher',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'switcher',
),
array (
'title' => 'reference-checkbox',
'name' => 'reference-checkbox',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
array (
'key' => '1',
'value' => 'One',
),
array (
'key' => '2',
'value' => 'Two',
),
),
'type' => 'checkbox',
'check_radio_layout' => 'horizontal',
),
array (
'title' => 'reference-checkbox-plain-array',
'name' => 'reference-checkbox-plain-array',
'object_type' => 'field',
'options' =>
array (
array (
'key' => '1',
'value' => 'One',
),
array (
'key' => '2',
'value' => 'Two',
),
),
'type' => 'checkbox',
'width' => '100%',
'is_array' => true,
),
array (
'title' => 'reference-iconpicker',
'name' => 'reference-iconpicker',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'iconpicker',
),
array (
'title' => 'reference-media-id',
'name' => 'reference-media-id',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'media',
'value_format' => 'id',
),
array (
'title' => 'reference-media-url',
'name' => 'reference-media-url',
'object_type' => 'field',
'options' =>
array (
),
'type' => 'media',
'width' => '100%',
'value_format' => 'url',
),
array (
'title' => 'reference-media-array-id-url',
'name' => 'reference-media-array-id-url',
'object_type' => 'field',
'options' =>
array (
),
'type' => 'media',
'width' => '100%',
'value_format' => 'both',
),
array (
'title' => 'reference-gallery-id',
'name' => 'reference-gallery-id',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'gallery',
'value_format' => 'id',
),
array (
'title' => 'reference-gallery-url',
'name' => 'reference-gallery-url',
'object_type' => 'field',
'options' =>
array (
),
'type' => 'gallery',
'width' => '100%',
'value_format' => 'url',
),
array (
'title' => 'reference-gallery-array-id-url',
'name' => 'reference-gallery-array-id-url',
'object_type' => 'field',
'options' =>
array (
),
'type' => 'gallery',
'width' => '100%',
'value_format' => 'both',
),
array (
'title' => 'reference-radio',
'name' => 'reference-radio',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
array (
'key' => '1',
'value' => 'One',
),
array (
'key' => '2',
'value' => 'Two',
),
),
'type' => 'radio',
),
array (
'title' => 'reference-repeater',
'name' => 'reference-repeater',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'repeater',
'repeater-fields' =>
array (
array (
'title' => 'field-1',
'name' => 'field-1',
'type' => 'text',
),
array (
'title' => 'field-2',
'name' => 'field-2',
'type' => 'checkbox',
'options' =>
array (
array (
'key' => '1',
'value' => 'One',
),
array (
'key' => '2',
'value' => 'Two',
),
),
'is_array' => true,
),
),
'repeater_collapsed' => true,
'repeater_title_field' => 'field-1',
),
array (
'title' => 'reference-select',
'name' => 'reference-select',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
array (
'key' => '1',
'value' => 'One',
'id' => 2878,
),
array (
'key' => '2',
'value' => 'Two',
),
),
'type' => 'select',
),
array (
'title' => 'reference-select-multiple',
'name' => 'reference-select-multiple',
'object_type' => 'field',
'options' =>
array (
array (
'key' => '1',
'value' => 'One',
),
array (
'key' => '2',
'value' => 'Two',
),
),
'type' => 'select',
'width' => '100%',
'is_multiple' => true,
),
array (
'title' => 'reference-number',
'name' => 'reference-number',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'number',
'isNested' => false,
),
array (
'title' => 'reference-colorpicker',
'name' => 'reference-colorpicker',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'colorpicker',
),
array (
'title' => 'reference-posts',
'name' => 'reference-posts',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'posts',
'search_post_type' =>
array (
'post',
'page',
),
),
array (
'title' => 'reference-posts-multiple',
'name' => 'reference-posts-multiple',
'object_type' => 'field',
'options' =>
array (
),
'type' => 'posts',
'width' => '100%',
'search_post_type' =>
array (
'post',
'page',
),
'is_multiple' => true,
),
array (
'title' => 'reference-html',
'name' => 'reference-html',
'object_type' => 'field',
'width' => '100%',
'options' =>
array (
),
'type' => 'html',
'html' => 'some html',
'html_css_class' => 'some-classes',
),
);
$title = 'Reference meta';
$post_type = 'post';
$args = array(
'object_type' => 'post',
'allowed_post_type' => $post_type,
'name' => $title,
);
if ( ! class_exists( 'Jet_Engine_CPT_Meta' ) ) {
require $meta->component_path( 'post.php' );
}
$meta->store_fields( $post_type, $meta_fields, 'post_type' );
new Jet_Engine_CPT_Meta( $post_type, $meta_fields, $title, 'normal', 'high', $args );
$taxonomy = 'category';
$tax_args = array(
'object_type' => 'taxonomy',
'allowed_tax' => array( $taxonomy ),
'name' => $title,
);
if ( ! class_exists( 'Jet_Engine_CPT_Tax_Meta' ) ) {
require $meta->component_path( 'tax.php' );
}
$meta->store_fields( $taxonomy, $meta_fields, 'taxonomy' );
new Jet_Engine_CPT_Tax_Meta( $taxonomy, $meta_fields, $tax_args );
$user_args = array (
'object_type' => 'user',
'name' => $title,
'allowed_user_screens' => 'edit-profile',
);
if ( ! class_exists( 'Jet_Engine_CPT_User_Meta' ) ) {
require $meta->component_path( 'user.php' );
}
$object_name = $user_args['name'] . ' ' . __( '(User fields)', 'jet-engine' );
$meta->store_fields( $object_name, $meta_fields, 'user' );
new Jet_Engine_CPT_User_Meta( $user_args, $meta_fields );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment