Add Metabox based on select data in Custom Post Type

Question

This will sound a bit strange at first, but it will make sense later on. So I have a metabox, and I am using Metabox.io to create Metaboxes.

Now, I have a metabox which has a dropdown, and a button. Based on the dropdown data I need to add_filter() of the created function metabox.

After a lot of experiments, I am getting a lot of error, first jQuery is not getting implemented on the click of the Metabox Button.

I have also the task of removing the metabox too, when the remove button from the particular metabox is hit.

But first I have to make the adding thing work.

Code:

<?php

//For template select
function story_template_select_box( $meta_boxes ) {
    $prefix = 'prefix-';

    $meta_boxes[] = array(
        'id' => 'story_template_select',
        'title' => esc_html__( 'Template Select', 'metabox-online-generator' ),
        'context' => 'advanced',
        'priority' => 'default',
        'autosave' => 'false',
        'post_types' => 'story',
        'fields' => array(
            array(
                'id' => $prefix . 'template_select',
                'name' => esc_html__( 'Template', 'metabox-online-generator' ),
                'type' => 'select_advanced',
                'placeholder' => esc_html__( 'Select a template', 'metabox-online-generator' ),
                'options' => array(
                    'template1' => esc_html__( 'Template 1', 'metabox-online-generator' )
                )
            ),
            array(
                'id' => $prefix . 'story_template_button',
                'type' => 'button',
                'std' => 'Add Template',
                'attributes' => array(
                    'data-section' => 'advanced-section',
                    'class' => 'js-add-template'
                )
            )

        )
    );

    return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'story_template_select_box' );

function template_one_meta_box( $meta_boxes ) {
    $prefix = 'prefix-';

    $meta_boxes[] = array(
        'id' => '',
        'title' => esc_html__( 'Assets', 'metabox-online-generator' ),
        'context' => 'advanced',
        'priority' => 'default',
        'autosave' => 'false',
        'post_types' => 'story',
        'fields' => array(
           //fields are there
        )
?>

<script>
  jQuery( function( $ ) {
    $( '.js-add-template' ).on( 'click', function() {
        //here want to add my template-1 based upon the select data
    } );
  } );
</script>

Note: The templates functions must be having different fields, so we cannot use plugins like Metabox Group

Please help me on this. Any alternative solutions are welcome here. Please pardon me for any mistakes, I am new to WordPress and working hard to learn Custom Post Types development.

Right now, this much I am having in my Custom Post Type

Resultant Image

0
, Alok 2 years 2019-10-22T06:04:41-05:00 0 Answers 78 views 0

Leave an answer

Browse
Browse