0

Here is the code that creates my metabox on my CPT named clients it displays fine and seems to keep the value input upon clicking update. The problem is with displaying the value saved within the metabox on a page.

I've tried outputting the data both within a loop and out the loop switching between get_the_ID() and $post_id

I've tried debugging with <?php var_dump($link); ?> which displays this on the frontend string(0) ""

To output the meta box value in the loop I'm doing

// Get Meta data
$link = get_post_meta(get_the_ID(), 'link_url', true); ?>
<li><a href="<?php echo $link; ?>"><?php the_post_thumbnail('clients'); ?></a></li>

Meta box Code

/**
     * Adds a box to the main column on the Post and Page edit screens.
     */
    function portfolio_link_add_meta_box() {

    $screens = array( 'clients' );

    foreach ( $screens as $screen ) {

        add_meta_box(
            'portfolio_logo_link',
            __( 'Logo Link', 'logo_link_textdomain' ),
            'logo_link_meta_box_callback',
            $screen
        );
    }
}

add_action( 'add_meta_boxes', 'portfolio_link_add_meta_box' );

function logo_link_meta_box_callback( $post ) {

    // Add an nonce field so we can check for it later.
    wp_nonce_field( 'logo_link_meta_box', 'logo_link_meta_box_nonce' );

    /*
     * Use get_post_meta() to retrieve an existing value
     * from the database and use the value for the form.
     */
    $value = get_post_meta( $post->ID, 'link_url', true );

    echo '<label for="link">';
    _e( 'Add The Logo Link', 'link_logo_textdomain' );
    echo '</label> ';
    echo '<input type="text" id="link" name="link" value="' . $value . '" size="25" />';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function logo_link_save_meta_box_data( $post_id ) {

    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST['logo_link_meta_box_nonce'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['logo_link_meta_box_nonce'], 'logo_link_meta_box' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

    /* OK, it's safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST['link'] ) ) {
        return;
    }

    // Sanitize user input.
    $my_data = sanitize_text_field( $_POST['link'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, 'link_url', $my_data );
}
add_action( 'save_post', 'logo_link_save_meta_box_data' );
6
  • What does your var_dump show? Besides the text/link inside the href of an a tag won't visibly displayed. Commented Aug 28, 2014 at 11:03
  • I've been looking in inspect element after putting the word "test" in the metabox it should appear between the href tags. But as you can see nothing appears.
    – dannyw24
    Commented Aug 28, 2014 at 11:22
  • But the get_post_meta in your callback works? Does get_the_ID return an ID? Commented Aug 28, 2014 at 11:26
  • Here, $link = get_post_meta(get_the_ID(), 'link_url', true); Make sure you are getting proper id and first check is really your value stored in database post_meta table.
    – Shail
    Commented Aug 28, 2014 at 11:38
  • How Can I check I'm getting the correct id?
    – dannyw24
    Commented Aug 28, 2014 at 12:02

1 Answer 1

0

Make sure your custom post type slug is clients in $screens = array( 'clients' );

Because i have just tested your code in local and its working fine.

3
  • Hi @Shail, The problem is with outputting the value saved within the metabox. Updated my question
    – dannyw24
    Commented Aug 28, 2014 at 10:53
  • Sorry, but what is the problem? You can't get value on front side?
    – Shail
    Commented Aug 28, 2014 at 10:58
  • Yes Shail, I cannot get the value to output.
    – dannyw24
    Commented Aug 28, 2014 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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