admin管理员组文章数量:1433161
I created a custom post type with custom date, time and checkbox fields. I'm having a problem with the date and time fields that do not register in WordPress Administration or in the database. And also there is a problem with a custom checkbox field that gets me a value but if I check two values, only one value appears, and no registration in the Wordpress administrator.
I've attached my code:
class Fastcat_Metabox
{
private $id;
private $title;
private $post_type;
private $fields = [];
//Ajout uploader image parti organisateur
public static function addJS(){
add_action('admin_enqueue_scripts', function(){
wp_register_script('uploaderjs', get_template_directory_uri() . '/assets/js/uplaoder.js');
wp_enqueue_script('uploaderjs');
});
}
/**
* Fastcat_Metabox constructor.
* @param $id ID de la boite
* @param $title Titre de la boite
* @param $post_type Post type
*/
// Construction de la function
public function __construct($id , $title, $post_type)
{
add_action('admin_init', array(&$this, 'create'));
add_action('save_post', array(&$this, 'save'));
$this->id = $id;
$this->title = $title;
$this->post_type = $post_type;
}
// Création de la fontion
public function create(){
if(function_exists('add_meta_box')) {
add_meta_box($this->id , $this->title, array(&$this, 'render'), $this->post_type);
}
//Sauvegarde des données et vérification des permission et du nonce
public function save($post_id){
//On ne fais rien en cas de save Ajax
if(
(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ||
(defined('DOING_AJAX') && DOING_AJAX)
){
return false;
}
//Vérifier permission
if(!current_user_can('edit_post', $post_id)){
return false;
}
//On vérifie le nonce
if (!wp_verify_nonce($_POST[$this->id . '_nonce'], $this->id)){
return false;
}
// Vérification de toutes les publications , suppression pour remplacement si déjà existant , ajout si nouveau etc
foreach($this->fields as $field) {
$meta = $field['id'];
if(isset($_POST[$meta])){
$value = $_POST[$meta];
if(get_post_meta($post_id, $meta)){
update_post_meta($post_id, $meta, $value);
} else {
if ($value === '') {
delete_post_meta($post_id, $meta);
} else {
add_post_meta($post_id, $meta , $value );
}
}
}
}
}
// Affichage de tout les nouveaux champs ajouter dans le dashboard
public function render(){
global $post;
foreach($this->fields as $field) {
extract($field);
$value = get_post_meta($post->ID, $id , true);
if($value == ''){
$value = $default;
}
require __DIR__ .'/'. $field['type'] . '.php';
}
echo '<input type="hidden" name="' .$this->id .'_nonce" value="' .wp_create_nonce($this->id). '">';
}
// Création des nouveaux champs
public function add($id, $label, $type = 'text', $default='') {
$this->fields[] = [
'id' => $id,
'name' => $label,
'type' => $type,
'default' => $default
];
return $this;
}
}
Fastcat_Metabox::addJS();
// CUSTOM POST TYPE -> COMPETITION -> Ajout des parties Information et Date supplémentaire
$box = new Fastcat_Metabox('compet' , 'Compétition', 'competition');
$box->add('fastcat_adresse','<strong> Adresse: </strong> ')
->add('fastcat_date', '<strong>Date </strong> ', 'date')
->add('fastcat_starttime', '<strong>Heure de début </strong> ', 'time')
->add('fastcat_endtime', ' <strong>Heure de fin </strong>', 'timeend')
->add('fastcat_choice', '<strong>Choix compétitions </strong>', 'checkbox')
->add('fastcat_description', '<strong>Description de la compétition </strong>', 'textarea')
->add('fastcat_space', '<strong>Nombre de place </strong> ', 'number')
->add('fastcat_rate', '<strong>Tarif </strong>', 'rate');
$box = new Fastcat_Metabox('ition' , 'Date Supplémentaire', 'competition');
$box->add('fastcat_date2', '<strong>Date</strong> ', 'date2')
->add('fastcat_starttime2', '<strong>Heure de début</strong> ', 'time2')
->add('fastcat_endtime2', '<strong> Heure de fin </strong>', 'timeend2')
->add('fastcat_date3', '<strong>Date</strong> ', 'date3')
->add('fastcat_starttime3', '<strong>Heure de début</strong> ', 'time3')
->add('fastcat_endtime3', '<strong> Heure de fin</strong> ', 'timeend3');
$box = new Fastcat_Metabox('cat' , 'Organisateur', 'competition');
$box->add('fastcat_organisateur','<strong>Nom de l\'organisateur</strong>','name_organisateur')
->add('fastcat_picture', '<strong Image de l\'organisateur</strong>','picture_organisateur')
->add('fastcat_club', '<strong>Numéro de club</strong> ', 'number_club')
->add('fastcat_adresse','<strong> Adresse: </strong> ', 'text')
->add('fastcat_phone', '<strong> Téléphone </strong>', 'phone')
->add('fastcat_mail', '<strong>Email</strong> ', 'mail')
->add('fastcat_siteweb', '<strong>Site internet</strong> ', 'web')
->add('fastcat_description_organisateur', '<strong>Description de la compétition </strong>', 'description');
//CHECKBOX
<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
<?= $name; ?>
</div>
<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">
<input type="checkbox" id="<?= $id; ?>" name="<?= $id; ?>" value="Cat">
<label for="<?= $value; ?>" style="padding-right:1rem;">CAT</label>
<input type="checkbox" id="<?= $id; ?>" name="<?= $id; ?>" value="FastCat">
<label for="<?= $value; ?>">FASTCAT</label>
</div>
//DATE
<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
<?= $name; ?>
</div>
<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">
<input type="text" name="<?= $id; ?>" id="<?= $id; ?>" value="<?= $value; ?>">
</div>
//TIME
<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
<?= $name; ?>
</div>
<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">
<input type="text" name="<?= $id; ?>" id="<?= $id; ?>" value="<?= $value; ?>">
</div>
Can you help me?
I created a custom post type with custom date, time and checkbox fields. I'm having a problem with the date and time fields that do not register in WordPress Administration or in the database. And also there is a problem with a custom checkbox field that gets me a value but if I check two values, only one value appears, and no registration in the Wordpress administrator.
I've attached my code:
class Fastcat_Metabox
{
private $id;
private $title;
private $post_type;
private $fields = [];
//Ajout uploader image parti organisateur
public static function addJS(){
add_action('admin_enqueue_scripts', function(){
wp_register_script('uploaderjs', get_template_directory_uri() . '/assets/js/uplaoder.js');
wp_enqueue_script('uploaderjs');
});
}
/**
* Fastcat_Metabox constructor.
* @param $id ID de la boite
* @param $title Titre de la boite
* @param $post_type Post type
*/
// Construction de la function
public function __construct($id , $title, $post_type)
{
add_action('admin_init', array(&$this, 'create'));
add_action('save_post', array(&$this, 'save'));
$this->id = $id;
$this->title = $title;
$this->post_type = $post_type;
}
// Création de la fontion
public function create(){
if(function_exists('add_meta_box')) {
add_meta_box($this->id , $this->title, array(&$this, 'render'), $this->post_type);
}
//Sauvegarde des données et vérification des permission et du nonce
public function save($post_id){
//On ne fais rien en cas de save Ajax
if(
(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ||
(defined('DOING_AJAX') && DOING_AJAX)
){
return false;
}
//Vérifier permission
if(!current_user_can('edit_post', $post_id)){
return false;
}
//On vérifie le nonce
if (!wp_verify_nonce($_POST[$this->id . '_nonce'], $this->id)){
return false;
}
// Vérification de toutes les publications , suppression pour remplacement si déjà existant , ajout si nouveau etc
foreach($this->fields as $field) {
$meta = $field['id'];
if(isset($_POST[$meta])){
$value = $_POST[$meta];
if(get_post_meta($post_id, $meta)){
update_post_meta($post_id, $meta, $value);
} else {
if ($value === '') {
delete_post_meta($post_id, $meta);
} else {
add_post_meta($post_id, $meta , $value );
}
}
}
}
}
// Affichage de tout les nouveaux champs ajouter dans le dashboard
public function render(){
global $post;
foreach($this->fields as $field) {
extract($field);
$value = get_post_meta($post->ID, $id , true);
if($value == ''){
$value = $default;
}
require __DIR__ .'/'. $field['type'] . '.php';
}
echo '<input type="hidden" name="' .$this->id .'_nonce" value="' .wp_create_nonce($this->id). '">';
}
// Création des nouveaux champs
public function add($id, $label, $type = 'text', $default='') {
$this->fields[] = [
'id' => $id,
'name' => $label,
'type' => $type,
'default' => $default
];
return $this;
}
}
Fastcat_Metabox::addJS();
// CUSTOM POST TYPE -> COMPETITION -> Ajout des parties Information et Date supplémentaire
$box = new Fastcat_Metabox('compet' , 'Compétition', 'competition');
$box->add('fastcat_adresse','<strong> Adresse: </strong> ')
->add('fastcat_date', '<strong>Date </strong> ', 'date')
->add('fastcat_starttime', '<strong>Heure de début </strong> ', 'time')
->add('fastcat_endtime', ' <strong>Heure de fin </strong>', 'timeend')
->add('fastcat_choice', '<strong>Choix compétitions </strong>', 'checkbox')
->add('fastcat_description', '<strong>Description de la compétition </strong>', 'textarea')
->add('fastcat_space', '<strong>Nombre de place </strong> ', 'number')
->add('fastcat_rate', '<strong>Tarif </strong>', 'rate');
$box = new Fastcat_Metabox('ition' , 'Date Supplémentaire', 'competition');
$box->add('fastcat_date2', '<strong>Date</strong> ', 'date2')
->add('fastcat_starttime2', '<strong>Heure de début</strong> ', 'time2')
->add('fastcat_endtime2', '<strong> Heure de fin </strong>', 'timeend2')
->add('fastcat_date3', '<strong>Date</strong> ', 'date3')
->add('fastcat_starttime3', '<strong>Heure de début</strong> ', 'time3')
->add('fastcat_endtime3', '<strong> Heure de fin</strong> ', 'timeend3');
$box = new Fastcat_Metabox('cat' , 'Organisateur', 'competition');
$box->add('fastcat_organisateur','<strong>Nom de l\'organisateur</strong>','name_organisateur')
->add('fastcat_picture', '<strong Image de l\'organisateur</strong>','picture_organisateur')
->add('fastcat_club', '<strong>Numéro de club</strong> ', 'number_club')
->add('fastcat_adresse','<strong> Adresse: </strong> ', 'text')
->add('fastcat_phone', '<strong> Téléphone </strong>', 'phone')
->add('fastcat_mail', '<strong>Email</strong> ', 'mail')
->add('fastcat_siteweb', '<strong>Site internet</strong> ', 'web')
->add('fastcat_description_organisateur', '<strong>Description de la compétition </strong>', 'description');
//CHECKBOX
<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
<?= $name; ?>
</div>
<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">
<input type="checkbox" id="<?= $id; ?>" name="<?= $id; ?>" value="Cat">
<label for="<?= $value; ?>" style="padding-right:1rem;">CAT</label>
<input type="checkbox" id="<?= $id; ?>" name="<?= $id; ?>" value="FastCat">
<label for="<?= $value; ?>">FASTCAT</label>
</div>
//DATE
<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
<?= $name; ?>
</div>
<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">
<input type="text" name="<?= $id; ?>" id="<?= $id; ?>" value="<?= $value; ?>">
</div>
//TIME
<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
<?= $name; ?>
</div>
<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">
<input type="text" name="<?= $id; ?>" id="<?= $id; ?>" value="<?= $value; ?>">
</div>
Can you help me?
Share Improve this question edited Apr 10, 2019 at 12:28 Nicolai Grossherr 18.9k8 gold badges64 silver badges109 bronze badges asked Apr 8, 2019 at 14:15 Jeanne ThibertJeanne Thibert 135 bronze badges 3- hastebin/eyayicalib.xml – Jeanne Thibert Commented Apr 8, 2019 at 14:23
- Code should be part of your question or answer, and not located externally, as it gets lost there, and then the question will be useless for others. – Nicolai Grossherr Commented Apr 10, 2019 at 12:30
- 1 For more information about the guidelines of this site please visit and give the help center a read. – Nicolai Grossherr Commented Apr 10, 2019 at 12:32
2 Answers
Reset to default 0A short overview of your code reveals some issues that seems to cause the problem:
Your function save($post_id)
saves all fields added to $fields
array.
This array if filled using function add($id, $label, $type = 'text', $default='')
But the problematic fields (checkbox, date and time) are not added to fields, so not saved by your function save($post_id)
.
Suggested Solution:
Either add these problematic fields using add()
function if possible.
Or within your function save()
capture each of the problematic fields submitted data and save to db by something like this
$meta = 'name_of_html_input'; //Replace name_of_html_input with actual name given in html
if( isset( $_POST[$meta] ) ){
$value = $_POST[$meta];
if(get_post_meta($post_id, $meta)){
update_post_meta($post_id, $meta, $value);
} else {
if ($value === '') {
delete_post_meta($post_id, $meta);
} else {
add_post_meta($post_id, $meta , $value );
}
}
}
I hope this may help!
You add metabox in the wrong way. You should use add_meta_boxes
action hook.
public function __construct($id , $title, $post_type)
{
$this->id = $id;
$this->title = $title;
$this->post_type = $post_type;
add_action('add_meta_boxes', array(&$this, 'create'));
add_action('save_post', array(&$this, 'save'));
add_action('admin_enqueue_scripts', array(&$this, 'addJS') );
}
public function addJS()
{
$screen = get_current_screen();
if ($screen && $screen->post_type == $this->post_type)
wp_enqueue_script('uploaderjs', get_template_directory_uri() . '/assets/js/uplaoder.js');
}
After the changes you don't need a line Fastcat_Metabox::addJS();
.
And also there is a problem with a custom checkbox field that gets me a value but if I check two values, only one value appears, and no registration in the Wordpress administrator.
<input type="checkbox" id="<?= $id; ?>" name="<?= $id; ?>" value="Cat">
<label for="<?= $value; ?>" style="padding-right:1rem;">CAT</label>
<input type="checkbox" id="<?= $id; ?>" name="<?= $id; ?>" value="FastCat">
<label for="<?= $value; ?>">FASTCAT</label>
It looks like checkboxes have the same name (ID also).
本文标签: Problem fields custom dateTimeand checkbox
版权声明:本文标题:Problem fields custom date, time, and checkbox 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745605507a2665816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论