Question

I'm developing a very simple custom plugin for a local league who need something to manage the teams and players along with their standings and fixtures. I'm planning to go with the post_type, meta_box and taxonomy functions to develop the plugin. I'm not an expert but I know the basic usage of the functions. I'm stuck at the point where I need multiple inputs displayed in the backend to fill out the teams' standing properties; wins, draws, losses, points for example. I was able to make the fields displayed in the admin panel but I just know how one input field data can be saved but since there are several data I tried almost all the methods I know and a lot of sites out there but couldn't get them saved. Please have a look at the codes,

I can successfully save one input field data by these codes

<?php 

/*
Plugin Name: Manage Clubs
Plugin URI: https://www.odesk.com/users/~01a9f6045443021f0c
Description: A simple lightweight plugin to manage your legue.
Author: Abubakkar Roby
Author URI: https://www.odesk.com/users/~01a9f6045443021f0c
Version: 1.0 
*/

class RN_Manage_Clubs {

    public function __construct() {

        $this->register_post_type();
        $this->statistics();
    }

    public function register_post_type() {

        $args = array(

            'labels' => array(
                'name' => 'Manage Clubs',
                'all_items' => 'Clubs',
                'singular_name' => 'Club',
                'add_new' => 'Add New Club',
                'add_new_item' => 'Add New Club',
                'edit_item' => 'Edit Club',
                'new_item' => 'Add New Item',
                'view_item' => 'View Club',
                'search_items' => 'Search Clubs',
                'not_found' => 'No Clubs Found',
                'not_found_in_trash' => 'No Clubs Found In Trash'
            ),
            'query_var' => 'Clubs',
            'rewrite' => array( 'slug' => 'clubs/'),
            //'menu_position' => 5,
            'menu_icon' => admin_url() . 'images/icon_soccer.png',
            'supports' => ['title', 'thumbnail'],
            'public' => true
        );

        register_post_type('RN_Manage_Clubs', $args);
    }

    public function statistics() {

        add_action('add_meta_boxes', function() {

            //css id, title, cb func, assoc. page/post, priority, cb func args
            add_meta_box('rn_club_stat', 'Club Standing', 'club_stats', 'RN_Manage_Clubs');
        });

        function club_stats($post) { 
            $POS = get_post_meta($post->ID, 'rn_club_stat', true);
        ?>

            <input type="text" name="rn_club_stat" id="rn_club_stat" value="<?php echo esc_attr($POS); ?>" size="1" tabindex="-1"  />

            <?php
        }

        add_action('save_post', function($id) {

            if( isset($_POST['rn_club_stat']) ) {

                update_post_meta(
                    $id, 
                    'rn_club_stat',
                    strip_tags($_POST['rn_club_stat']) 
                );
            }
        });
    }
}

add_action('init', function() {
    new RN_Manage_Clubs();
});

But how I do save data from multiple input fields like these fields

<?php 

/*
Plugin Name: Manage Clubs
Plugin URI: https://www.odesk.com/users/~01a9f6045443021f0c
Description: A simple lightweight plugin to manage your legue.
Author: Abubakkar Roby
Author URI: https://www.odesk.com/users/~01a9f6045443021f0c
Version: 1.0 
*/

class RN_Manage_Clubs {

    public function __construct() {

        $this->register_post_type();
        $this->statistics();
    }

    public function register_post_type() {

        $args = array(

            'labels' => array(
                'name' => 'Manage Clubs',
                'all_items' => 'Clubs',
                'singular_name' => 'Club',
                'add_new' => 'Add New Club',
                'add_new_item' => 'Add New Club',
                'edit_item' => 'Edit Club',
                'new_item' => 'Add New Item',
                'view_item' => 'View Club',
                'search_items' => 'Search Clubs',
                'not_found' => 'No Clubs Found',
                'not_found_in_trash' => 'No Clubs Found In Trash'
            ),
            'query_var' => 'Clubs',
            'rewrite' => array( 'slug' => 'clubs/'),
            //'menu_position' => 5,
            'menu_icon' => admin_url() . 'images/icon_soccer.png',
            'supports' => ['title', 'thumbnail'],
            'public' => true
        );

        register_post_type('RN_Manage_Clubs', $args);
    }

    public function statistics() {

        add_action('add_meta_boxes', function() {

            //css id, title, cb func, assoc. page/post, priority, cb func args
            add_meta_box('rn_club_stat', 'Club Standing', 'club_stats', 'RN_Manage_Clubs');
        });

        function club_stats($post) { 
            $POS = get_post_meta($post->ID, 'rn_club_stat', true);
        ?>
            <table>
                <thead>
                    <tr>
                        <td>&nbsp;</td>
                        <th>POS</th>
                        <th>W</th>
                        <th>D</th>
                        <th>L</th>
                        <th>F</th>
                        <th>A</th>
                        <th>GD</th>
                        <th>PTS</th>
                    </tr>
                </thead>

                <tfoot>
                    <tr>
                        <th>Total</th>
                        <td><input type="text" name="POS" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="W" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="D" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="L" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="F" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="A" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="GD" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="PTS" value="" size="1" tabindex="-1" readonly /></td>
                    </tr>
                </tfoot>

                <tbody>
                    <tr>
                        <td>Values</td>
                        <td><input type="text" name="POS" id="POS" value="" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="W" value="" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="D" value="" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="L" value="" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="F" value="" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="A" value="" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="GD" value="" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="PTS" value="" size="1" tabindex="-1"  /></td>
                    </tr>
                </tbody>
            </table>

            <?php
        }

        add_action('save_post', function($id) {

            if( isset($_POST['rn_club_stat']) ) {

                update_post_meta(
                    $id, 
                    'rn_club_stat',
                    strip_tags($_POST['rn_club_stat']) 
                );
            }
        });
    }
}

add_action('init', function() {
    new RN_Manage_Clubs();
});

@gorirrajoe, I have found out a solution for that using get_post_custom() function. But I will try your codes as well. Here's the snippet that did the job for me

<?php

/*
Plugin Name: Manage League
Plugin URI: https://www.odesk.com/users/~01a9f6045443021f0c
Description: A simple lightweight plugin to manage your legue.
Author: Abubakkar Roby
Author URI: https://www.odesk.com/users/~01a9f6045443021f0c
Version: 1.0 
*/

class RN_Manage_Clubs {

    public function __construct() {

        $this->register_post_type();
        $this->statistics();
    }

    public function register_post_type() {

        $args = array(

            'labels' => array(
                'name' => 'Manage Clubs',
                'all_items' => 'Clubs',
                'singular_name' => 'Club',
                'add_new' => 'Add New Club',
                'add_new_item' => 'Add New Club',
                'edit_item' => 'Edit Club',
                'new_item' => 'Add New Item',
                'view_item' => 'View Club',
                'search_items' => 'Search Clubs',
                'not_found' => 'No Clubs Found',
                'not_found_in_trash' => 'No Clubs Found In Trash'
            ),
            'query_var' => 'Clubs',
            'rewrite' => array( 'slug' => 'clubs/'),
            //'menu_position' => 5,
            'menu_icon' => admin_url() . 'images/icon_soccer.png',
            'supports' => ['title', 'editor', 'thumbnail'],
            'public' => true
        );

        register_post_type('RN_Manage_Clubs', $args);
    }

    public function statistics() {

        add_action('add_meta_boxes', function() {

            //css id, title, cb func, assoc. page/post, priority, cb func args
            add_meta_box('rn_club_stat', 'Club Standing', 'club_stats', 'RN_Manage_Clubs');
        });

        function club_stats($post) { 
            //$value = get_post_meta($post->ID, 'rn_club_stat', false);
            global $post;
            $custom = get_post_custom($post->ID);
            $POS = $custom['POS'][0];
            $P = $custom['P'][0];
            $W = $custom['W'][0];
            $D = $custom['D'][0];
            $L = $custom['L'][0];
            $B = $custom['B'][0];
            $PTS = $custom['PTS'][0];

        ?>
            <table>
                <thead>
                    <tr>
                        <td>&nbsp;</td>
                        <th>POS</th>
                        <th>P</th>
                        <th>W</th>
                        <th>D</th>
                        <th>L</th>
                        <th>B</th>
                        <th>PTS</th>
                    </tr>
                </thead>

                <tfoot>
                    <tr>
                        <th>Total</th>
                        <td><input type="text" name="POS" id="POS" value="<?php echo $POS; ?>" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="P" id="P" value="<?php echo $P; ?>" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="W" value="<?php echo $W; ?>" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="D" value="<?php echo $D; ?>" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="L" value="<?php echo $L; ?>" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="F" value="<?php echo $B; ?>" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="PTS" value="<?php echo $PTS; ?>" size="1" tabindex="-1" readonly /></td>
                    </tr>
                </tfoot>

                <tbody>
                    <tr>
                        <td>Values</td>
                        <td><input type="text" name="POS" id="POS" value="<?php echo $POS; ?>" size="1" tabindex="-1" /></td>
                        <td><input type="text" name="P" id="P" value="<?php echo $P; ?>" size="1" tabindex="-1" /></td>
                        <td><input type="text" name="W" id="W" value="<?php echo $W; ?>" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="D" value="<?php echo $D; ?>" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="L" value="<?php echo $L; ?>" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="B" value="<?php echo $B; ?>" size="1" tabindex="-1"  /></td>
                        <td><input type="text" name="PTS" value="<?php echo $PTS; ?>" size="1" tabindex="-1"  /></td>
                    </tr>
                </tbody>
            </table>

            <?php
        }

        add_action('save_post', function($id) {

            if( isset($_POST['POS']) ) {
                update_post_meta($id,'POS', $_POST['POS']);
            }

            if( isset($_POST['P']) ) {
                update_post_meta($id,'P', $_POST['P']);
            }

            if( isset($_POST['W']) ) {
                update_post_meta($id,'W', $_POST['W']);
            }   

            if( isset($_POST['D']) ) {
                update_post_meta($id,'D', $_POST['D']);
            }

            if( isset($_POST['L']) ) {
                update_post_meta($id,'L', $_POST['L']);
            }

            if( isset($_POST['B']) ) {
                update_post_meta($id,'B', $_POST['B']);
            }

            if( isset($_POST['PTS']) ) {
                update_post_meta($id,'PTS', $_POST['PTS']);
            }
        });
    }
}

add_action('init', function() {
    new RN_Manage_Clubs();
});
Was it helpful?

Solution

Your code is only looking to see if "rn_club_stat" is set. You'll need to have it check for all of your fields.

<?php 

/*
Plugin Name: Manage Clubs
Plugin URI: https://www.odesk.com/users/~01a9f6045443021f0c
Description: A simple lightweight plugin to manage your legue.
Author: Abubakkar Roby
Author URI: https://www.odesk.com/users/~01a9f6045443021f0c
Version: 1.0 
*/

class RN_Manage_Clubs {

    public function __construct() {

        $this->register_post_type();
        $this->statistics();
    }

    public function register_post_type() {

        $args = array(

            'labels' => array(
                'name' => 'Manage Clubs',
                'all_items' => 'Clubs',
                'singular_name' => 'Club',
                'add_new' => 'Add New Club',
                'add_new_item' => 'Add New Club',
                'edit_item' => 'Edit Club',
                'new_item' => 'Add New Item',
                'view_item' => 'View Club',
                'search_items' => 'Search Clubs',
                'not_found' => 'No Clubs Found',
                'not_found_in_trash' => 'No Clubs Found In Trash'
            ),
            'query_var' => 'Clubs',
            'rewrite' => array( 'slug' => 'clubs/'),
            //'menu_position' => 5,
            'menu_icon' => admin_url() . 'images/icon_soccer.png',
            'supports' => ['title', 'thumbnail'],
            'public' => true
        );

        register_post_type('RN_Manage_Clubs', $args);
    }

    public function statistics() {

        add_action('add_meta_boxes', function() {

            //css id, title, cb func, assoc. page/post, priority, cb func args
            add_meta_box('rn_club_stat', 'Club Standing', 'club_stats', 'RN_Manage_Clubs');
        });

        function club_stats($post) { 
        ?>
            <table>
                <thead>
                    <tr>
                        <td>&nbsp;</td>
                        <th>POS</th>
                        <th>W</th>
                        <th>D</th>
                        <th>L</th>
                        <th>F</th>
                        <th>A</th>
                        <th>GD</th>
                        <th>PTS</th>
                    </tr>
                </thead>

                <tfoot>
                    <tr>
                        <th>Total</th>
                        <td><input type="text" name="POS_total" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="W_total" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="D_total" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="L_total" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="F_total" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="A_total" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="GD_total" value="" size="1" tabindex="-1" readonly /></td>
                        <td><input type="text" name="PTS_total" value="" size="1" tabindex="-1" readonly /></td>
                    </tr>
                </tfoot>

                <tbody>
                    <tr>
                        <td>Values</td>
                        <td><input type="text" name="pos" id="pos" value="<?php echo get_post_meta($post->ID, '_pos', true); ?>" size="1" tabindex="1"  /></td>
                        <td><input type="text" name="w" value="<?php echo get_post_meta($post->ID, '_w', true); ?>" size="1" tabindex="2"  /></td>
                        <td><input type="text" name="d" value="<?php echo get_post_meta($post->ID, '_d', true); ?>" size="1" tabindex="3"  /></td>
                        <td><input type="text" name="l" value="<?php echo get_post_meta($post->ID, '_l', true); ?>" size="1" tabindex="4"  /></td>
                        <td><input type="text" name="f" value="<?php echo get_post_meta($post->ID, '_f', true); ?>" size="1" tabindex="5"  /></td>
                        <td><input type="text" name="a" value="<?php echo get_post_meta($post->ID, '_a', true); ?>" size="1" tabindex="6"  /></td>
                        <td><input type="text" name="gd" value="<?php echo get_post_meta($post->ID, '_gd', true); ?>" size="1" tabindex="7"  /></td>
                        <td><input type="text" name="pts" value="<?php echo get_post_meta($post->ID, '_pts', true); ?>" size="1" tabindex="8"  /></td>
                    </tr>
                </tbody>
            </table>

            <?php
        }

        add_action('save_post', function($id) {
          $new_meta_value = ( isset( $_POST['pos'] ) ? $_POST['pos'] : '' );
          $meta_value = get_post_meta( $id, '_pos', true );
          if ( $new_meta_value && '' == $meta_value )
            add_post_meta( $id, '_pos', $new_meta_value, true );
          elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $id, '_pos', $new_meta_value );
          elseif ( '' == $new_meta_value && $meta_value )
            delete_post_meta( $id, '_pos', $meta_value );

          $new_meta_value = ( isset( $_POST['w'] ) ? $_POST['w'] : '' );
          $meta_value = get_post_meta( $id, '_w', true );
          if ( $new_meta_value && '' == $meta_value )
            add_post_meta( $id, '_w', $new_meta_value, true );
          elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $id, '_w', $new_meta_value );
          elseif ( '' == $new_meta_value && $meta_value )
            delete_post_meta( $id, '_w', $meta_value );

          $new_meta_value = ( isset( $_POST['d'] ) ? $_POST['d'] : '' );
          $meta_value = get_post_meta( $id, '_d', true );
          if ( $new_meta_value && '' == $meta_value )
            add_post_meta( $id, '_d', $new_meta_value, true );
          elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $id, '_d', $new_meta_value );
          elseif ( '' == $new_meta_value && $meta_value )
            delete_post_meta( $id, '_d', $meta_value );

          $new_meta_value = ( isset( $_POST['l'] ) ? $_POST['l'] : '' );
          $meta_value = get_post_meta( $id, '_l', true );
          if ( $new_meta_value && '' == $meta_value )
            add_post_meta( $id, '_l', $new_meta_value, true );
          elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $id, '_l', $new_meta_value );
          elseif ( '' == $new_meta_value && $meta_value )
            delete_post_meta( $id, '_l', $meta_value );

          $new_meta_value = ( isset( $_POST['f'] ) ? $_POST['f'] : '' );
          $meta_value = get_post_meta( $id, '_f', true );
          if ( $new_meta_value && '' == $meta_value )
            add_post_meta( $id, '_f', $new_meta_value, true );
          elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $id, '_f', $new_meta_value );
          elseif ( '' == $new_meta_value && $meta_value )
            delete_post_meta( $id, '_f', $meta_value );

          $new_meta_value = ( isset( $_POST['a'] ) ? $_POST['a'] : '' );
          $meta_value = get_post_meta( $id, '_a', true );
          if ( $new_meta_value && '' == $meta_value )
            add_post_meta( $id, '_a', $new_meta_value, true );
          elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $id, '_a', $new_meta_value );
          elseif ( '' == $new_meta_value && $meta_value )
            delete_post_meta( $id, '_a', $meta_value );

          $new_meta_value = ( isset( $_POST['gd'] ) ? $_POST['gd'] : '' );
          $meta_value = get_post_meta( $id, '_gd', true );
          if ( $new_meta_value && '' == $meta_value )
            add_post_meta( $id, '_gd', $new_meta_value, true );
          elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $id, '_gd', $new_meta_value );
          elseif ( '' == $new_meta_value && $meta_value )
            delete_post_meta( $id, '_gd', $meta_value );

          $new_meta_value = ( isset( $_POST['pts'] ) ? $_POST['pts'] : '' );
          $meta_value = get_post_meta( $id, '_pts', true );
          if ( $new_meta_value && '' == $meta_value )
            add_post_meta( $id, '_pts', $new_meta_value, true );
          elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $id, '_pts', $new_meta_value );
          elseif ( '' == $new_meta_value && $meta_value )
            delete_post_meta( $id, '_pts', $meta_value );
        });
    }
}

add_action('init', function() {
    new RN_Manage_Clubs();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top