plugin development - Custom Meta Box returns no HTML

admin2025-04-20  0

I'm trying to write my first plugin and running into an issue with the callback in add_meta_box(). The meta box appears on the custom post type page however there is no content inside.

Here is the relevant code:

class MySkills
{
    function __construct()
    {
        add_action('init', array('MySkills', 'ip_myskills_register_post_type'));
        add_action('load-post.php', array($this, 'init_metabox'));
        add_action('load-post-new.php', array($this, 'init_metabox'));
    }

    public function init_metabox() {
        add_action('add_meta_boxes', array('MySkills', 'skillsmetabox_init'));
        add_action('save_post', array($this, 'save_metabox_details'));
    }

    public function skillsmetabox_init() {
        add_meta_box('myskills_meta', 'Proficiency', 'myskills_html');
    }

    public function myskills_html($post) {
        ?>
        <?php wp_nonce_field( basename(__FILE__ ), 'ipmyskills_nonce' ) ?>
        <label for="ipmyskills_input"><?php _e('Enter the proficiency level for this skill (1 to 100)', 'myskill') ?></label>
        <input type="text" class="widefat" name="ipmysills_input" id="ipmyskills_input" value="<?php echo esc_attr( get_post_meta($post->ID, 'ip_myskills_input', true) ) ?>" />
        <?php
    }


}

I'm trying to write my first plugin and running into an issue with the callback in add_meta_box(). The meta box appears on the custom post type page however there is no content inside.

Here is the relevant code:

class MySkills
{
    function __construct()
    {
        add_action('init', array('MySkills', 'ip_myskills_register_post_type'));
        add_action('load-post.php', array($this, 'init_metabox'));
        add_action('load-post-new.php', array($this, 'init_metabox'));
    }

    public function init_metabox() {
        add_action('add_meta_boxes', array('MySkills', 'skillsmetabox_init'));
        add_action('save_post', array($this, 'save_metabox_details'));
    }

    public function skillsmetabox_init() {
        add_meta_box('myskills_meta', 'Proficiency', 'myskills_html');
    }

    public function myskills_html($post) {
        ?>
        <?php wp_nonce_field( basename(__FILE__ ), 'ipmyskills_nonce' ) ?>
        <label for="ipmyskills_input"><?php _e('Enter the proficiency level for this skill (1 to 100)', 'myskill') ?></label>
        <input type="text" class="widefat" name="ipmysills_input" id="ipmyskills_input" value="<?php echo esc_attr( get_post_meta($post->ID, 'ip_myskills_input', true) ) ?>" />
        <?php
    }


}
Share Improve this question asked Sep 27, 2019 at 19:33 IsaacIsaac 432 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try adding array( $this, 'myskills_html' ) to the third argument.

    public function skillsmetabox_init() {
        add_meta_box( 'myskills_meta', 'Proficiency', array( $this, 'myskills_html' ) );
    }

Since this is class method, it should have the $this as well.

The same way you did with

add_action( 'save_post', array( $this, 'save_metabox_details' ) );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745123449a286285.html

最新回复(0)