WordPress.org

Ready to get started?Download WordPress

Forums

Error saving custom meta boxes data (6 posts)

  1. au.petrone
    Member
    Posted 1 year ago #

    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();
    });
  2. Matt Lowe
    Member
    Posted 1 year ago #

    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.

  3. au.petrone
    Member
    Posted 1 year ago #

    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();
    });
  4. Matt Lowe
    Member
    Posted 1 year ago #

    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.

  5. au.petrone
    Member
    Posted 1 year ago #

    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"));
  6. Matt Lowe
    Member
    Posted 1 year ago #

    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

    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'];

Topic Closed

This topic has been closed to new replies.

About this Topic