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> </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> </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();
});