1

I'm struggle with implementing a custom input field on the screen of a Site Info in a Network. I've managed to create a field with help of this post but i have problems with saving the value.

When i output the $_POST variable in the admin_footer-function, it is an empty array.

<?php
add_action('admin_footer', 'pg_custom_site_options');
function pg_custom_site_options(){

    global $pagenow;

    if( 'site-info.php' == $pagenow ) {

        global $details;

        $blog_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
        if ( ! $blog_id ) wp_die( __('Invalid site ID.') );

        $saved_value = get_blog_option( $blog_id, 'blog_order');

        ?>
        <table>
            <tr id="user16975_custom_options">
                <th scope="row">Order</th>
                <td><input type="text" size="5" name="blog[blog_order]" value="<?php echo esc_attr( $saved_value ) ?>" ></td>
            </tr>
        </table>
        <script>
            jQuery(function($){
                $('.form-table tbody').append($('#user16975_custom_options'));
            });
        </script>
        <?php


        // DON'T USE IT - SEE MY THE ANSWER BEHIND
        // ---------------------------------------

        // Use a default value here if the field was not submitted.
        // $new_field_value = '0';

        //global $_POST; // Just a Test

        // echo var_dump($_POST); // Is an empty array :(

        // if ( !empty($_POST['blog']['blog_order']) ){
        //     $new_field_value = $_POST['blog']['blog_order'];
        // }else{
        //     echo "nothing to do here";
        // }

        // save option into the database
        // update_option( 'blog_order', $new_field_value);
    }
}
?>

The strange part is, all values are presented after submit / reload of the site in Chrome Developer Tools or Firebug.

  • Updated the code for getting the saved value – Paul Geisler Oct 9 '14 at 11:33
0

The problem was a redirect in site-info.php. After that the $_POST variable was empty in the code inserting in the admin_footer.

I just need to use a further hook 'admin_init' and save the value here.

add_action('admin_init', 'pg_save_custom_site_options');
function pg_save_custom_site_options(){

    global $pagenow;

    if( 'site-info.php' == $pagenow &&
        isset($_REQUEST['action']) &&
        'update-site' == $_REQUEST['action']
    ) {
        // Use a default value here if the field was not submitted.
        $new_field_value = '0';

        if ( isset( $_POST['blog']['blog_order'] ) ) {
            $new_field_value = intval( $_POST['blog']['blog_order'] );

            // save option into the database
            if( is_int($new_field_value) ){
                update_blog_option( $_POST['id'], 'blog_order', $new_field_value );
            }

        }

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.