Title: Error saving custom meta boxes data
Last modified: August 21, 2016

---

# Error saving custom meta boxes data

 *  [au.petrone](https://wordpress.org/support/users/aupetrone/)
 * (@aupetrone)
 * [13 years ago](https://wordpress.org/support/topic/error-saving-custom-meta-boxes-data/)
 * Hi, people. I’m starting wordpress plugin’s development and this is my first 
   attempt to make one. I have a problem with saving custom meta boxes data. I can
   see te meta bo in my pages but when i save post i get an internal server error.
   Can someone please tell me why? Thanks
 *     ```
       <?php
       /*
       Plugin Name: nw_Page_Gallery
   
       */
   
       class NW_mb{
   
       	var $post_meta = array("image1","image2");
   
       	var $boxes = array();
   
       	public function __construct(){
   
       	}
   
       	public function add_Box(){
   
       		add_meta_box("nw_meta_box","Sfondo pagina",array($this, 'add_html'),"page");
   
       	}
   
       	public function add_Html($post){
   
       		$post_meta = get_post_meta($post->ID, "nw_meta_boxes");
   
       		print_r($post->ID);
   
       		?>
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image1" name="nw_image1" value="<?php echo $this->post_meta[0] ?>" />
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image2" name="nw_image2" value="<?php echo $this->post_meta[1] ?>" />
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image3" name="nw_image3" value="<?php echo $this->post_meta[2] ?>" />
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image4" name="nw_image4" value="<?php echo $this->post_meta[3] ?>" />
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image5" name="nw_image5" value="<?php echo $this->post_meta[4] ?>" />
   
       		<?
   
       	}
   
       	public function save_Box($id){
   
       		update_post_meta($id, 'nw_meta_boxes', array(
       			$_POST['nw_image1'],
       			$_POST['nw_image2'],
       			$_POST['nw_image3'],
       			$_POST['nw_image4'],
       			$_POST['nw_image5']
       		));
   
       	}
   
       }
   
       add_action("add_meta_boxes", function(){
       	$meta = new NW_mb();
       	$meta->add_Box();
       });
   
       add_action("save_post", function(){
       	$meta->save_Box();
       });
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Matt Lowe](https://wordpress.org/support/users/squelch/)
 * (@squelch)
 * [13 years ago](https://wordpress.org/support/topic/error-saving-custom-meta-boxes-data/#post-3680322)
 *     ```
       add_action("save_post", function(){
       	$meta->save_Box();
       });
       ```
   
 * That doesn’t look right to me. `$meta` doesn’t seem to be being set anywhere,
   so it’ll be null. If you try to call a method on a null you get an error. If 
   you look at your Apache error logs it would probably tell you this, they’re usually
   found in /var/log/apache/error.log on linux machines and that’s the first place
   to start looking when you get an error.
 *  Thread Starter [au.petrone](https://wordpress.org/support/users/aupetrone/)
 * (@aupetrone)
 * [13 years ago](https://wordpress.org/support/topic/error-saving-custom-meta-boxes-data/#post-3680345)
 * Ok, now is correct, i don’t see the error anymore. But it still doesn’t save 
   my data.
 * Here is the new code
 *     ```
       <?php
       /*
       Plugin Name: nw_Page_Gallery
   
       */
   
       class NW_mb{
   
       	var $post_meta = array("image1","image2");
   
       	var $boxes = array();
   
       	public function __construct(){
   
       	}
   
       	public function add_Box(){
   
       		add_meta_box("nw_meta_box","Sfondo pagina",array($this, 'add_html'),"page");
   
       	}
   
       	public function add_Html($post){
   
       		$post_meta = get_post_meta($post->ID, "nw_meta_boxes");
   
       		print_r($post->ID);
   
       		?>
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image1" name="nw_image1" value="<?php echo $this->post_meta[0] ?>" />
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image2" name="nw_image2" value="<?php echo $this->post_meta[1] ?>" />
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image3" name="nw_image3" value="<?php echo $this->post_meta[2] ?>" />
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image4" name="nw_image4" value="<?php echo $this->post_meta[3] ?>" />
   
       		<label>Indirizzo immagine</label>
       		<input type="text" id="nw_image5" name="nw_image5" value="<?php echo $this->post_meta[4] ?>" />
   
       		<?
   
       	}
   
       	public function save_Box($id){
   
       		update_post_meta($id, 'nw_meta_boxes', array(
       			isset($_POST['nw_image1']),
       			isset($_POST['nw_image2']),
       			isset($_POST['nw_image3']),
       			isset($_POST['nw_image4']),
       			isset($_POST['nw_image5'])
       		));
   
       	}
   
       }
   
       add_action("add_meta_boxes", function(){
   
       	$NW_mb_istance = new NW_mb;
       	$NW_mb_istance->add_Box();
       });
   
       add_action("save_post", function(){
       	NW_mb::save_Box();
       });
       ```
   
 *  [Matt Lowe](https://wordpress.org/support/users/squelch/)
 * (@squelch)
 * [13 years ago](https://wordpress.org/support/topic/error-saving-custom-meta-boxes-data/#post-3680348)
 * `public function save_Box($id){`
 * This method requires a parameter of `$id`.
 * `NW_mb::save_Box();`
 * This is not providing a parameter of `$id`.
 * Again, the error log will be displaying a message along these lines.
 *  Thread Starter [au.petrone](https://wordpress.org/support/users/aupetrone/)
 * (@aupetrone)
 * [13 years ago](https://wordpress.org/support/topic/error-saving-custom-meta-boxes-data/#post-3680370)
 * I’m trying to save some data but i can’t. The problem is that the data saved 
   is not that i expect but is an empty array.
 * This is the code…i’m going mad!
 *     ```
       <?php
       /*
       Plugin Name: nw_Page_Gallery
   
       */
   
       class NW_mb{
   
           var $post_meta = array("image1","image2");
           var $id;
   
           public function __construct(){
   
           }
   
           public function add_Box(){
   
               add_meta_box("nw_meta_box","Sfondo pagina",array($this, 'add_html'),"page");
   
           }
   
           public function add_Html($post){
   
               $post_meta = get_post_meta($post->ID, "nw_meta_boxes");
               $this->id = $post->ID;
   
               print_r($post_meta);
   
               ?>
   
               <label>Indirizzo immagine</label>
               <input type="text" id="nw_image1" name="nw_image1" value="<?php echo $post_meta[0] ?>" />
   
               <label>Indirizzo immagine</label>
               <input type="text" id="nw_image2" name="nw_image2" value="<?php echo $post_meta[1] ?>" />
   
               <label>Indirizzo immagine</label>
               <input type="text" id="nw_image3" name="nw_image3" value="<?php echo $post_meta[2] ?>" />
   
               <label>Indirizzo immagine</label>
               <input type="text" id="nw_image4" name="nw_image4" value="<?php echo $post_meta[3] ?>" />
   
               <label>Indirizzo immagine</label>
               <input type="text" id="nw_image5" name="nw_image5" value="<?php echo $post_meta[4] ?>" />
   
               <?
   
           }
   
           public function save_Box(){
   
               $boxes = array(
                   isset($_POST['nw_image1']),
                   isset($_POST['nw_image2']),
                   isset($_POST['nw_image3']),
                   isset($_POST['nw_image4']),
                   isset($_POST['nw_image5'])
               );
   
               update_post_meta($this->id, 'nw_meta_boxes', $boxes);
   
           }
   
       }
   
       add_action("add_meta_boxes", function(){
           $NW_mb_istance = new NW_mb;
   
           $NW_mb_istance->add_Box();
       });
   
       add_action("save_post", array($NW_mb_istance,"save_Box"));
       ```
   
 *  [Matt Lowe](https://wordpress.org/support/users/squelch/)
 * (@squelch)
 * [13 years ago](https://wordpress.org/support/topic/error-saving-custom-meta-boxes-data/#post-3680433)
 * Now it looks to me like you’re asking WordPress to call save_Box on an object
   that you’ve not created.
 * `add_action("save_post", array($NW_mb_istance,"save_Box"));`
 * `$NW_mb_istance` doesn’t appear to be set in the scope at which you’re calling`
   add_action`. Even if `$NWB_mb_istance` _was_ set I don’t think your code would
   do what you want anyway…
 * Judging by the code you’ve written I would expect to see an array of 5 true or
   false values depending on whether each of $_POST[new_images1-5] have been set.
   Are you sure you understand what `isset()` does? [http://php.net/manual/en/function.isset.php](http://php.net/manual/en/function.isset.php)
 * I hate giving people code, it can lead to laziness and it’s all too easy for 
   me to make an assumption about what you want to do, but for the action I’d expect
   to see something like this:
 *     ```
       add_action("save_post", function() {
           $NW_mb_istance = new NW_mb;
           $NW_mb_istance->save_Box();
       });
       ```
   
 * And for `save_Box()` I’d expect to see something like this:
 *     ```
       public function save_Box() {
   
               $boxes = array(
                   $_POST['nw_image1'],
                   $_POST['nw_image2'],
                   $_POST['nw_image3'],
                   $_POST['nw_image4'],
                   $_POST['nw_image5']
               );
   
               update_post_meta($this->id, 'nw_meta_boxes', $boxes);
           }
       ```
   
 * I make no guarantees that this is right, but this is more like what I’d expect
   to be seeing based on what you say you want to do. If the intention was to only
   add those posts to the array if they’re set then you might try something like
   this:
 *     ```
       $boxes = array();
   
       if (isset($_POST['nw_image1'])) $boxes[] = $_POST['nw_image1'];
       if (isset($_POST['nw_image2'])) $boxes[] = $_POST['nw_image2'];
       if (isset($_POST['nw_image3'])) $boxes[] = $_POST['nw_image3'];
       if (isset($_POST['nw_image4'])) $boxes[] = $_POST['nw_image4'];
       if (isset($_POST['nw_image5'])) $boxes[] = $_POST['nw_image5'];
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Error saving custom meta boxes data’ is closed to new replies.

## Tags

 * [development](https://wordpress.org/support/topic-tag/development/)

 * 5 replies
 * 2 participants
 * Last reply from: [Matt Lowe](https://wordpress.org/support/users/squelch/)
 * Last activity: [13 years ago](https://wordpress.org/support/topic/error-saving-custom-meta-boxes-data/#post-3680433)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
