Wordpress:在类中保存元数据

问题描述 投票:0回答:1

我正在制作一个 php 类来制作和管理自定义元框。不知何故,它不会保存字段中的值,如果我对

update_post_meta
函数进行硬编码,例如
update_post_meta(25, 'test_key', 'foo')
,它甚至不会保存值。我猜我的
add_action
有问题,但我认为它会触发,因为当我在保存函数中添加
var_dump()
echo
时,它会显示“更新失败。响应不是有效的 JSON 响应” ”

这是基本代码

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

这是课程

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

感谢大家的帮助!

祝你有美好的一天:D

php wordpress metadata update-post-meta
1个回答
0
投票

我又用 google 搜索了几次,并阅读了 w3schools 关于 oop php 的文章,我向尚未掌握

classes
的人推荐这些文章。我发现这个问题:如何保存自定义元框数据。它没有遇到与我相同的问题,但它有功能代码,所以我尝试找出差异。


这就是修复它的方法

我没有在

add_meta_box
回调中获取正确的参数,它应该是
$post
而不是
$id

我简化了我使用的密钥,因为它很容易出错。现在是

plugin-name_post-type_key

这是当前代码:

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

我还做了一些其他的小调整,但没有改变功能。

© www.soinside.com 2019 - 2024. All rights reserved.