• So I am trying to create a dynamic form to control my front page I have created a dynamic form that when updated creates a string in one form element that gets saved when I try and save the page redirects to the all settings page and nothing gets saved.

    add_action('admin_init','grays_initilize_category_options');
    
    function grays_create_menu_page() {
    add_menu_page(
    'Grays Options',  // The title to be displayed on the corresponding page for this menu
    'Grays',  // The text to be displayed for this actual menu item
    'administrator',// Which type of users can see this menu
    'grays',  // The unique ID - that is, the slug - for this menu item
    'grays_menu_page_display',// The name of the function to call when rendering the menu for this page
    ''
    );
    }
    
    function grays_menu_page_display() {
    ?><div class="wrap">
    <h2>grays</h2>
    <?php
    settings_fields( 'grays' );
    do_settings_sections( 'grays' ); ?>
     </div>
     <?php
    
    }
    
    function grays_sanitize_category($input){
    $output=$input;
    return apply_filters( 'grays_sanitize_social_options', $output, $input );
    }
    function grays_catfield_callback(){
    ?>
    <h2> Select categories for front page.</h2>
    <?php settings_errors(); ?>
    <?php
    echo "blah".get_option('categorys');
    $args = array(
    'type' => 'post',
    'child_of' => 0,
    'parent'   => '',
    'orderby'  => 'name',
    'order'=> 'ASC',
    'hide_empty'   => 1,
    'hierarchical' => 1,
    'exclude'  => '',
    'include'  => '',
    'number'   => '',
    'taxonomy' => 'category',
    'pad_counts'   => false 
    
     );
    $cats = get_categories($args); ?>
    
    <table class="form-table">
    <input type="hidden" name="update_settings" value="Y" />
    <ul id="featured-posts-list">
    
    </ul>
    <input type="hidden" name="element-max-id" value="" />
    <li class="front-page-element" id="front-page-element-placeholder" style="display:none">
    <label for="element-page-id">category:</label>
    <select onchange="changer()" class="selection" name="element-page-id">
    <option value="">
    
    </option>
    <?php foreach ($cats as $cat) : ?>
    <option value="<?php echo $cat->ID; ?>">
    <?php echo $cat->name; ?>
    </option>
    <?php endforeach; ?>
    </select>
    <a href="#">Remove</a>
    </li>
    <a href="#" id="add-featured-post">Add category</a>   
    
    <input type="hidden" name="update_settings" value="Y" />
    <form method="post" action="options.php">
    <input id="Grays_categorys" class="text" style="display:block" type="textarea"></input>
    <?php submit_button();  ?>
    </form>
    </div>
    <script type="text/javascript">
    var elementCounter = jQuery("input[name=element-max-id]").val();
    
    function changer(){
    //console.log("hi");
    var main ="";
    jQuery('.selection ').each(function(){
    
    main=main+this.options[this.selectedIndex].text+',';
    //console.log(main);
    });
    //console.log("here");
    main=main.replace(",,","");
    jQuery('#Grays_categorys').val(main);
    
    }
    jQuery(document).ready(function() {
    
    jQuery("#add-featured-post").click(function() {
    
    var elementRow = jQuery("#front-page-element-placeholder").clone();
    var newId = "front-page-element-" + elementCounter;
    
    elementRow.attr("id", newId);
    elementRow.show();
    
    var inputField = jQuery("select", elementRow);
    inputField.attr("name", "element-page-id-" + elementCounter);
    
    var labelField = jQuery("label", elementRow);
    labelField.attr("for", "element-page-id-" + elementCounter);
            var removeLink = jQuery("a", elementRow).click(function() {
                 removeElement(elementRow);
             return false;
                });
    elementCounter++;
    jQuery("input[name=element-max-id]").val(elementCounter);
    
    jQuery("#featured-posts-list").append(elementRow);
    
    return false;
    });
    });
    
    function removeElement(element) {
    jQuery(element).remove();
    }
    </script>
    <?
    
    }

The topic ‘Settings api opens settings page’ is closed to new replies.