2

I created my first plugin last week that makes a custom post type. As this worked, I copy pasted the code I already had and modified it to be a different post type. For whatever reason however, the callback I have set for add_meta_box is not being called. Can anyone tell me why this is, where I have looked it over repeatedly with no luck what so ever.

/**
 * Adds a meta box to the post editing screen
/***************************************************************************/
function package_custom_meta()
{
    add_meta_box( 'package_meta', __( 'Package Title', 'package_textdomain' ), 'package_meta_callback', 'package' , 'high' );

    echo "package_custom_meta CALLED";
}

add_action( 'add_meta_boxes', 'package_custom_meta' );

/**
 * Outputs the content of the meta box
/***************************************************************************/
function package_meta_callback( $post )
{
    echo "package_meta_callback CALLED";

    wp_nonce_field( basename( __FILE__ ), 'package_nonce' );
    $package_stored_meta = get_post_meta( $post->ID );
    ?>

    <p>
        <label for="meta-package-512mb" class="package-row-title"><?php _e( '512MB RAM', 'package_textdomain' )?></label>
        <input type="text" name="meta-package-512mb" id="meta-package-512mb" value="<?php if ( isset ( $package_stored_meta['meta-package-512mb'] ) ) echo $package_stored_meta['meta-package-512mb'][0]; ?>" />
    </p>

    <p>
        <label for="meta-package-1gb" class="package-row-title"><?php _e( '1GB RAM', 'package_textdomain' )?></label>
        <input type="text" name="meta-package-1gb" id="meta-package-1gb" value="<?php if ( isset ( $package_stored_meta['meta-package-1gb'] ) ) echo $package_stored_meta['meta-package-1gb'][0]; ?>" />
    </p>

    <?php
}
2
  • I don't see any errors in your arguments or your syntax. Is the echo statement in your first function displayed? – Matt Keys Nov 8 '14 at 1:24
  • Yeah, the echo in the first function is called, the one in the second is not. That is why I am so confused as to what the actual issue is. Here is the full file if it helps to see it pastie.org/private/drc95wfyis41naamt6aaw – Aidan Knight Nov 8 '14 at 2:56
2

Looks like we both missed the error because I didn't see it at first either.

In your call to add_meta_box you skipped the argument for context and went straight to priority. "high" is not an available string for the context argument, and this apparently causes the function to silently fail.

Adding in the string for context fixes it:

add_meta_box( 'package_meta', __( 'Package Title', 'package_textdomain' ), 'package_meta_callback', 'package', 'normal', 'high' );

3
  • That did indeed fix it, thank you so much. It is strange because the first plugin I made for testing also lacked that but worked without issue. As soon as I added that though, this one began working correctly. – Aidan Knight Nov 8 '14 at 13:10
  • Context and Priority are optional, so maybe in your previous plugin you omitted them completely, which would make them go to their defaults and work normally? – Matt Keys Nov 8 '14 at 14:21
  • It is definitely weird because it doesn't but I will just chalk it up to an anomaly lol. add_meta_box( 'prfx_meta', __( 'Coupon Title', 'prfx-textdomain' ), 'prfx_meta_callback', 'coupon' , 'high' ); – Aidan Knight Nov 8 '14 at 16:07
1

For me empty string as screen param worked

add_meta_box(
        'tribe_events_event_details',
        'Post Events Calendar',
        'bw_events_chooser_box',
        '', // screen
        'normal',
        'high'
    );

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.