vendredi 12 avril 2019

Checkbox with multiple choice, custom fields problem database record and missing line break in the custom field description

Currently creating a website in Wordpress during an internship, I created a custom message type with a checkbox multiple choice but also a description field with the wordpress editor

I encounter a problem when saving checkboxes in a database, only one enters the database. But also no box is checked in Wordpress

I also created custom fields with the Wordpress editor, but there are no line breaks, so I look for a solution without
etc.

//METABOX 

<?php



class Fastcat_Metabox
{

    private $id;
    private $title;
    private $post_type;
    private $fields = [];

    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
     */


        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;
        }




    public function create(){
        if(function_exists('add_meta_box')) {
            add_meta_box($this->id , $this->title, array(&$this, 'render'), $this->post_type);
        }

    }

    public function save($post_id){


            if(

                (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ||
                (defined('DOING_AJAX') && DOING_AJAX)


            ){
                return false;
            }



            if(!current_user_can('edit_post', $post_id)){
                return false;
            }




          if (!wp_verify_nonce($_POST[$this->id . '_nonce'], $this->id)){
                return false;
          }






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 );

                }
            }
        }
    }      

}

    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). '">';
    }



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 
$box = new Fastcat_Metabox('compet' , '<h2>Information compétition</h2>', 'competition');
$box->add('fastcat_adresse','<strong> Adresse: </strong> ')
    ->add('fastcat_startdate', '<strong>Date </strong> ', 'date' )
    ->add('fastcat_starttime', '<strong>Heure de début </strong> ', 'time' )
    ->add('fastcat_enddate', ' <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' , '<h2>Date Supplémentaire</h2>', 'competition');
$box->add('fastcat_startdate2', '<strong>Date</strong> ', 'date2')
    ->add('fastcat_starttime2', '<strong>Heure de début</strong> ', 'time2')
    ->add('fastcat_enddate2', '<strong> Heure de fin </strong>', 'timeend2')
    ->add('fastcat_startdate3', '<strong>Date</strong> ', 'date3')
    ->add('fastcat_starttime3', '<strong>Heure de début</strong> ', 'time3')
    ->add('fastcat_enddate3', '<strong> Heure de fin</strong> ', 'timeend3');

$box = new Fastcat_Metabox('cat' , '<h2>Information organisateur</h2>', 'competition');
$box->add('fastcat_organisateur','<strong>Nom de l\'organisateur</strong>','name_organisateur')
        ->add('fastcat_picture', '<strong Image de l\'organisateur','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>', 'textarea2');





<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>



<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
       <?= $name; ?>
</div>

<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">

<?= wp_editor($value,$id); ?>

</div>

Currently only one field is registered in the database and currently no box is checked in wordpress

No line breaks in the description field

So I try to record if both boxes are checked both data in database, but also display the checked boxes in wordpress

And to solve the problem of line break in the editor of Wordpress

Thank you in advance for your future response;)




Aucun commentaire:

Enregistrer un commentaire