phpwordpressmetadataupdate-post-meta

Wordpress: Saving metadata inside of a class


I am making a php class to make and manage custom metaboxes. Somehow it won't save the values from the field, it doesn't even save a value if I hardcode the update_post_meta function eg update_post_meta(25, 'test_key', 'foo'). I'm guessing there is something wrong with my add_action but it fires, I think, because when I add a var_dump() or echo in the save function it says "Updating failed. The response is not a valid JSON response"

Here is the basic code

$meta_args = array(
    'post-type' => 'event',
    'id'    => 'nickname',
    'title' => 'Nickname',
    'position' => 'side',
    'meta-fields' => array(
        array(
            'key'   => 'nickname',
            'type'  => 'text',
            'placeholder'   => 'Johana'
        ),
    ),
);

new custom_metabox_simple($meta_args);

and here is the class

class custom_metabox_simple
{
    public array $meta_general;
    public array $meta_fields;
    public function __construct(array $meta_args)
    {
        //Extracting and setting the arguments
        $this->meta_general = $meta_args;
        unset($this->meta_general['meta-fields']);

        if (isset($meta_args['meta-fields'])) {
            $this->meta_fields = $meta_args['meta-fields'];
        }
        

        // Printing the box
        $this->print_metabox();
        
    }
    private function print_metabox(){
        //Add meta box
        add_action('add_meta_boxes', function(){$this->setup_metabox();});

        //Save meta values
        add_action('save_post', function(){$this->save_metavalues();});
    }
    private function setup_metabox(){
        //ID of metabox
        $boxID = $this->meta_general['post-type'].'_'.$this->meta_general['id'];

        //Add meta box
        add_meta_box($boxID, $this->meta_general['title'], function($id){$this->content_metabox($id);}, $this->meta_general['post-type'], $this->meta_general['position']);
    
    }
    private function content_metabox($id){
            //Key for selecting the field and value
            $key = $id->post_type.'_'.$this->meta_fields[0]['key'];

            //gets value
            $value = get_post_meta($id, '_'.$key.'_key', TRUE);

            // Prints the fields
            $printed_field = '<input type="text" id="'.$key.'_field" name="'.$key.'_field" placeholder="'.$this->meta_fields[0]['placeholder'].'" value="'.$value.'">';
            echo $printed_field;
            
    }
    private function save_metavalues(){
        //Key for selecting the field and value
        $key = $this->meta_general['post-type'].'_'.$this->meta_fields[0]['key'];

        //gets value
        $value = $_POST[$key.'_field'];

        update_post_meta(get_the_ID(), '_'.$key.'_key', $value);
    }
}

Thanks for all and any help!

Have a nice day :D


Solution

  • I googled around a couple more times and read the w3schools articles on oop php, which I recommend to anyone who hasn't mastered classes yet. I found this question: how to save custom meta box data. It didn't have the same problem that I had but it had functioning code, so I tried to find the differences.


    This is what fixed it

    I wasn't grabbing the right arguments in my add_meta_box callback it should have been $post insted of $id

    I simplified the key I was using, as it was prone to errors. Now it is plugin-name_post-type_key

    here is the current code:

    class custom_metabox_simple{
        public array $meta_general;
        public array $meta_fields;
        public function __construct(array $meta_args){
            //Extracting and setting the arguments
            $this->meta_general = $meta_args;
            unset($this->meta_general['meta-fields']);
    
            if (isset($meta_args['meta-fields'])) {
                $this->meta_fields = $meta_args['meta-fields'];
            }
            
            // Printing the box
            $this->print_metabox();
            
        }
        public function print_metabox(){
            //Add meta box
            add_action('add_meta_boxes', array($this, 'setup_metabox'));
    
            //Save meta values
            add_action('save_post', array($this, 'save_metavalues'));
        }
        public function setup_metabox(){
            //ID of metabox
            $boxID = $this->meta_general['post-type'].'_'.$this->meta_general['id'];
    
            //Add meta box
            add_meta_box($boxID, $this->meta_general['title'], array($this, 'content_metabox'), $this->meta_general['post-type'], $this->meta_general['position']);
        
        }
        public function content_metabox($post){
                //Key for selecting the field and value
                $key = 'ljm_'.$this->meta_general['post-type'].'_'.$this->meta_fields[0]['key'];
    
                //gets value
                $value = get_post_meta($post->ID, $key, TRUE);
    
                // Prints the fields
                $printed_field = '<input type="text" id="'.$key.'" name="'.$key.'" placeholder="'.$this->meta_fields[0]['placeholder'].'" value="'.$value.'">';
                echo $printed_field;
                
        }
        public function save_metavalues($id){
            //Key for selecting the field and value
            $key = 'ljm_'.$this->meta_general['post-type'].'_'.$this->meta_fields[0]['key'];
    
            //gets value
            $value = $_POST[$key];
    
            // Updates meta value
            update_post_meta($id, $key, $value);
            
        }
    }
    

    There were some other small tweaks I made, but nothing that changes the functionality.