Dynamic form fields

PHOTO EMBED

Thu Mar 23 2023 22:52:49 GMT+0000 (Coordinated Universal Time)

Saved by @khalidlogi #php


function kh_plugin_settings_page() {

    // Check user capabilities

    if ( ! current_user_can( 'manage_options' ) ) {

        return;

    }

    // Add error/update messages

    settings_errors( 'kh_plugin_messages' );

    ?>

    <div class="wrap">

        <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>

        <form action="options.php" method="post">

            <?php

            // Output security fields for the registered setting "kh_plugin_options"

            settings_fields( 'kh_plugin_options' );

            // Output setting sections and their fields

            do_settings_sections( 'kh_plugin' );

            // Output save settings button

            submit_button( __( 'Save Settings', 'kh_plugin' ) );

            ?>

        </form>

    </div>

    <?php

}

function kh_plugin_settings_init() {

    // Register a new setting for the plugin

    register_setting( 'kh_plugin_options', 'kh_plugin_settings' );

    // Add a new section to the settings page

    add_settings_section(

        'kh_plugin_section',

        __( 'KH Plugin Settings', 'kh_plugin' ),

        'kh_plugin_section_callback',

        'kh_plugin'

    );

    // Add a text field to the settings section

    add_settings_field(

        'kh_plugin_text_field',

        __( 'Text Field', 'kh_plugin' ),

        'kh_plugin_text_field_callback',

        'kh_plugin',

        'kh_plugin_section'

    );

    // Add a checkbox field to the settings section

    add_settings_field(

        'kh_plugin_checkbox_field',

        __( 'Checkbox Field', 'kh_plugin' ),

        'kh_plugin_checkbox_field_callback',

        'kh_plugin',

        'kh_plugin_section'

    );

}

add_action( 'admin_init', 'kh_plugin_settings_init' );

// Callback function for the section

function kh_plugin_section_callback() {

    echo '<p>' . __( 'Configure your KH Plugin settings.', 'kh_plugin' ) . '</p>';

}

// Callback function for the text field

function kh_plugin_text_field_callback() {

    $options = get_option( 'kh_plugin_settings' );

    echo '<input type="text" name="kh_plugin_settings[text_field]" value="' . esc_attr( $options['text_field'] ) . '" />';

function my_plugin_shortcode( $atts ) {

    ob_start();

    

    $defaults = array(

        'form_id' => 0,

        'button_text' => 'Add field',

    );

    $args = shortcode_atts( $defaults, $atts );

    

    $form_fields = get_post_meta( $args['form_id'], 'form_fields', true );

    ?>

    <form id="my-form">

        <?php foreach ( $form_fields as $field ) : ?>

            <div class="form-group">

                <label for="<?php echo esc_attr( $field['name'] ); ?>"><?php echo esc_html( $field['label'] ); ?></label>

                <?php if ( $field['type'] === 'text' ) : ?>

                    <input type="text" name="<?php echo esc_attr( $field['name'] ); ?>" value="<?php echo esc_attr( $field['value'] ); ?>">

                <?php elseif ( $field['type'] === 'textarea' ) : ?>

                    <textarea name="<?php echo esc_attr( $field['name'] ); ?>"><?php echo esc_html( $field['value'] ); ?></textarea>

                <?php elseif ( $field['type'] === 'select' ) : ?>

                    <select name="<?php echo esc_attr( $field['name'] ); ?>">

                        <?php foreach ( $field['options'] as $option ) : ?>

                            <option value="<?php echo esc_attr( $option['value'] ); ?>"<?php selected( $option['value'], $field['value'] ); ?>><?php echo esc_html( $option['label'] ); ?></option>

                        <?php endforeach; ?>

                    </select>

                <?php endif; ?>

            </div>

        <?php endforeach; ?>

        <button type="button" id="add-field"><?php echo esc_html( $args['button_text'] ); ?></button>

    </form>

    <?php

    $output = ob_get_clean();

    return $output;

}

add_shortcode( 'my-plugin-form', 'my_plugin_shortcode' );

content_copyCOPY