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.

No html returned from callback

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
    }


}
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' ) );
  • Thank you, that seemed to fix it. Only weird thing though, when I used $this it threw an error and the page didn't load. Adding the class name via plain text though did fixed it. Any idea why $this is somehow not available in this method? – Isaac Sep 27 '19 at 20:24
  • Never mind, looks like in the constructor I had 'MySkills', 'init_metabox' instead of $this. Changed it there and was able to use $this in the callback method. Thanks again for the answer. – Isaac Sep 27 '19 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.