I am building a wordpress plugin with the boilerplate framework (https://wppb.me/). That all works find. I have an annoying issues which I cannot solve.
I have a table with a Boolean field AM_ACTIVE
In the page layout I want this to be an checkbox. See method below. A array is initialized and with the shortcode_atts the array and the request parameters are merged. For an add new this works. But if I edit it. The edit page is showing the checked/non checked correctly. But save will always result in false. I have tried a million things. With True/false 0/1. Putting the value with cache values, item Values, request values. Most of the time I get an PHP Warning: A non-numeric value encountered in /var/www/vhosts/marcelvreuls.eu/httpdocs/wp-content/plugins/am
So I assume the checkbox is not know when it is not touched or something. I have no clue. Can anybody help me out. I just want to activated/deactived a record đ.
Any help will be highly appreciated
First the page code, after that the save code
function amarre_maingroup_form_meta_box_handler($item)
{
?>
<table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table">
<tbody>
<tr class="form-field">
<th valign="top" scope="row">
<label for="am_name"><?php _e('Name', 'amarre')?></label>
</th>
<td>
<input id="am_name" name="am_name" type="text" style="width: 50%" value="<?php echo esc_attr($item['am_name'])?>"
size="50" class="code" placeholder="<?php _e('am_maingroup', 'amarre')?>" required>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row">
<label for="am_url"><?php _e('Afbeelding', 'amarre')?></label>
</th>
<td>
<input id="am_url" name="am_url" type="text" style="width: 50%" value="<?php echo esc_attr($item['am_url'])?>"
size="50" class="code" placeholder="<?php _e('Afbeelding', 'amarre')?>" required>
</td>
</tr>
<tr class="form-field">
<th valign="top" scope="row">
<label for="am_active"><?php _e('am_active', 'amarre')?></label><?php echo $_POST['am_active']?>
</th>
<td>
<?php
if ($item['am_active'] == 1)
{
echo('<input name="am_active" type="checkbox" value="1" checked="checked">');
}
else
{
echo('<input name="am_active" type="checkbox" value="0">' );
}
?>
</td>
</tr>
</tbody>
</table>
<?php
}
function amarre_maingroup_form_page_handler()
{
global $wpdb;
$table_name = $wpdb->prefix . 'am_maingroup';
$message = '';
$notice = '';
// this is default $item which will be used for new records
$default = array(
'id' => 0,
'am_name' => '',
'am_active' => '',
'am_url' => ''
);
// here we are verifying does this request is post back and have correct nonce
if ( isset($_REQUEST['nonce']) && wp_verify_nonce($_REQUEST['nonce'], basename(__FILE__))) {
// combine our default item with request params
$item = shortcode_atts($default, $_REQUEST);
error_log( implode( ", ", $_REQUEST ));
error_log( implode( ", ", $item ));
// validate data, and if all ok save item to database
// if id is zero insert otherwise update
$item_valid = $this->amarre_validate_maingroup($item);
if ($item_valid === true) {
error_log( $item['am_name']);
error_log( $_REQUEST['am_active']);
if ($item['id'] == 0) {
$result = $wpdb->insert($table_name, $item);
$item['id'] = $wpdb->insert_id;
if ($result) {
$message = __('Item was successfully saved', 'amarre');
} else {
$notice = __('There was an error while saving item' + $result, 'amarre');
}
} else {
$result = $wpdb->update($table_name, $item, array('id' => $item['id']));
if ($result) {
$message = __('Item was successfully updated', 'amarre');
} else {
$notice = __('There was an error while updating item' + $result, 'amarre');
}
}
} else {
// if $item_valid not true it contains error message(s)
$notice = $item_valid;
}
}
else {
// if this is not post back we load item to edit or give new one to create
$item = $default;
if (isset($_REQUEST['id'])) {
$item = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $_REQUEST['id']), ARRAY_A);
if (!$item) {
$item = $default;
$notice = __('Item not found', 'amarre');
}
}
}
Aucun commentaire:
Enregistrer un commentaire