WordPress.org

Ready to get started?Download WordPress

Forums

Displaying saved values in custom metaboxes (9 posts)

  1. rikardo85
    Member
    Posted 1 year ago #

    Hello,

    I have created some custom meta boxes for pages which save to the database with no problems.

    However when I update/publish the page, the saved values are not being displayed.

    Does anyone know how I can display these saved values in their relevent fields when I update / publish the page?

    The code I am using to create these boxes is:

    function featureBox( $post ) {
    
      wp_nonce_field( plugin_basename( __FILE__ ), 'featureBox_noncename' );
    
      $boxTitle = get_post_meta( $_POST['post_ID'], $key = '_boxTitle', $single = true );
      $boxText = get_post_meta( $_POST['post_ID'], $key = '_boxText', $single = true );
      $boxImage = get_post_meta( $_POST['post_ID'], $key = '_boxImage', $single = true );
      $boxLink = get_post_meta( $_POST['post_ID'], $key = '_boxLink', $single = true ); ?>
    
        <label for="boxTitle">Box Title</label>
        <p><input class="widefat" type="text" id="boxTitle" name="boxTitle" value="<?php $boxTitle ?>" size="" /></p>    
    
        <label for="boxImage">Image</label>
        <p><input class="widefat" value="<?php echo esc_url ($boxImage);?>"/></p>
        <p><input id="upload_image_button" type="button" value="Media Library Image" class="button-secondary" /></p>
    
        <label for="boxText">Box text</label>
        <p><input class="widefat" type="text" id="boxText" name="boxText" value="<?php $boxText ?>" size="" /></p>
    
        <label for="boxText">Link Text:</label>
        <p><input class="widefat" type="textarea" name="boxLinkText" value="<?php echo esc_attr($boxLinkText); ?>"/></p>
    
    <?php
    }

    Any help much appreciated

  2. Chris
    Member
    Posted 1 year ago #

    Your $key strings start with an underscore while the HTML name attributes don't. Mabye that's why?

  3. rikardo85
    Member
    Posted 1 year ago #

    That did not work :(

  4. Chris
    Member
    Posted 1 year ago #

    Next round :)
    $post->ID instead of $_POST['post_ID']

    $boxTitle = get_post_meta( $post->ID, 'boxTitle', true );
      $boxText  = get_post_meta( $post->ID, 'boxText', true );
      $boxImage = get_post_meta( $post->ID, 'boxImage', true );
      $boxLink  = get_post_meta( $post->ID, 'boxLink', true );

    and you have to actually echo the value
    value="<?php echo $boxTitle; ?>" instead of just value="<?php $boxTitle ?>"

  5. rikardo85
    Member
    Posted 1 year ago #

    This also didn't work :(

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    Custom field keys that start with an underscore are hidden on the post/page edit screen.
    http://codex.wordpress.org/Function_Reference/add_post_meta#Making_a_.22Hidden.22_Custom_Field

    And as Chris said echo the output:

    <?php
    // using  $post->ID and underscore
    $boxText  = get_post_meta( $post->ID, '_boxText', true );
    ?>
     <label for="boxText">Box text</label>
        <p><input class="widefat" type="text" id="boxText" name="boxText" value="<?php echo $boxText; ?>" size="" /></p>
  7. rikardo85
    Member
    Posted 1 year ago #

    I don't want to hide the values.

    I want them to appear once I update the page

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    I understand, try it with this:

    function featureBox( $object ) {
    
      wp_nonce_field( plugin_basename( __FILE__ ), 'featureBox_noncename' );
    
      $boxTitle = get_post_meta( $object->ID, '_boxTitle', true );
      $boxText  = get_post_meta( $object->ID, '_boxText', true );
      $boxImage = get_post_meta( $object->ID, '_boxImage', true );
      $boxLink  = get_post_meta( $object->ID, '_boxLink', true ); ?>
    
        <label for="boxTitle">Box Title</label>
        <p><input class="widefat" type="text" id="boxTitle" name="boxTitle" value="<?php echo $boxTitle; ?>" size="" /></p>    
    
        <label for="boxImage">Image</label>
        <p><input class="widefat" value="<?php echo esc_url ($boxImage); ?>"/></p>
        <p><input id="upload_image_button" type="button" value="Media Library Image" class="button-secondary" /></p>
    
        <label for="boxText">Box text</label>
        <p><input class="widefat" type="text" id="boxText" name="boxText" value="<?php echo $boxText; ?>" size="" /></p>
    
        <label for="boxText">Link Text:</label>
        <p><input class="widefat" type="textarea" name="boxLinkText" value="<?php echo esc_attr($boxLink); ?>"/></p>
    
    <?php
    }
  9. rikardo85
    Member
    Posted 1 year ago #

    That's done it!

    Thanks you, its much appreciated.

    I can upload an image ok but it appears in the main text editor box.
    Do you have any ideas how I can add and image to the image field in a custom meta box?

Topic Closed

This topic has been closed to new replies.

About this Topic