• Resolved brveljkovic

    (@brveljkovic)


    I want to add multiple textareas to my plugin settings page. Now, the client want to have an array in the beginning where we would define all texareas. So it would be array(‘Banner HTML’,’GIF Banner’) and so one.
    After that we would add fields and callback functions dinamically, looping through an array.
    Has anyone done something similar? Do we have some best practice instructions on how to do this?
    Branko

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The Settings API is the recommended way to create settings. Many of the examples in the linked Handbook illustrate variables as parameters. It’s not much of a stretch to adapt these to array variables. Take the Add a Setting example, adapt it like so:

    register_setting( 
        $data[$i]['option_group'], 
        $data[$i]['option_name'], 
        $data[$i]['sanitize_callback'] = ''
    );

    Where $data is your predefined array of setting parameters and the above function call is within a for loop that increments $i for every major $data array element, which is structured as an indexed array of associative arrays. The structure can be deepened to accommodate section and field parameters as well.

    Thread Starter brveljkovic

    (@brveljkovic)

    thanks for reply. I have tried with some custom code, but not very happy with that, specially have problems to call calbackfunction in foreach loop. I would like to do everything according to best code practice. Do we have some examples where I could check on the code.
    Basically what I am after is this:
    I define a number of textareas in the top of settings.php file and then automatically add that many textareas
    thanks again

    Thread Starter brveljkovic

    (@brveljkovic)

    this worked for me, though I still didnt brush up my code

    <?php
    add_action( 'admin_menu', 'cmby_add_admin_menu' );
    add_action( 'admin_init', 'cmby_settings_init' );
    
    $settingfields = array('NA','HTML Banner','GIF Banner','Booking Form','Udemy Course', 'Test Missile');
    
    function cmby_add_admin_menu(  ) { 
    
        add_options_page( 'Cambrickyard Settings', 'Cambrickyard', 'manage_options', 'cambrickyard', 'cmby_options_page' );
    
    };
    
    function cmby_settings_init(  ) { 
        global $settingfields;
    	register_setting( 'pluginPage', 'cmby_settings' );
        
    	add_settings_section(
    		"cmby_pluginPage_section", 
    		__( "Cambrick Yard Plugin Settings Page", "wordpress" ), 
    		"cmby_settings_section_callback", 
    		'pluginPage'
    	);
    
        foreach ($settingfields as $id => $item) {
            $i=0;
    	add_settings_field( 
    		'cmby_textarea_field_'.$id, 
    		__( "Enter widget code", "wordpress" ), 
    		'cmby_textarea_render_field', 
    		"pluginPage", 
    		"cmby_pluginPage_section",
            array (
                'indicator' => $id
            )
    	);
            $i++;
            }
    }
            
        function cmby_textarea_render_field($args) {
        $options = get_option( 'cmby_settings' );
        echo $args['indicator'];
        echo '<textarea cols="60" rows="5" name="cmby_settings[cmby_textarea_field_'.$args['indicator'].']">';
        echo $options['cmby_textarea_field_'.$args['indicator']];
        //echo "test";
        echo '</textarea>'; 
    };
       
    
    function cmby_settings_section_callback(  ) { 
    
        echo __( 'Use fields below to enter the code to be displayed when shortcode called out', 'wordpress' );
    
    }
    
    function cmby_options_page(  ) { 
    
        ?>
    	<form action='options.php' method='post'>
    
    		<?php
    		settings_fields( 'pluginPage' );
    		do_settings_sections( 'pluginPage' );
    		submit_button();
    		?>
    
    	</form>
    	<?php
    
    }
    
    ?>

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

    Moderator bcworkz

    (@bcworkz)

    That’s very good! Perfectly acceptable as is. If you are interested in some small improvements, one is to avoid using globals. The array could be passed as a parameter to the function instead of declaring it global. Avoiding globals when possible is always a good idea.

    If you had made use of $i, it would always be equal to 0 because you initialized it inside the loop. Since you are not using it, related code should be removed.

    Because this meets a specific requirement for a specific site, there is no need to take this any further. You can probably imagine this form by array definition concept could be carried further to be a general tool where any type of field can be defined in any other plugin you might create. OTOH, if you make it too generalized, the definition process becomes more cumbersome and there is less to gain in doing so at all.

    But if it is decided to be beneficial, to be the most useful in other plugins, it would be good to objectify the entire process so it can be a module easily incorporated into any plugin. Going this far is way beyond any immediate need and a more advanced concept. More looking to the future if this even makes sense and you are at all interested. ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Plugins Settings Page – Add dynamically’ is closed to new replies.