0

I have a problem. I'm trying to add a meta box textarea field to a custom post type (in a plugin) with custom database table. The meta box is displaying in the custom post type, saving and updating, but just not showing the data on a page (front end) and I just can't figure it out. It shows the title and content of the post, but not the entered data. Here is my code for meta box:

    //show metabox in post editing page
    add_action('add_meta_boxes', 'bsp_add_metabox' );

    function bsp_add_metabox() {
   // add_meta_box( $id, $title, $callback, $screen, $context, $priority );
  add_meta_box('bsp_mother_tongue', 'Mother language','bsp_mother_tongue_handler', 'badge','normal');
   }
   //showing the meta box on admin page
  function bsp_mother_tongue_handler($post){

$value = get_post_custom($post->ID);
?>
<!--<label for="bsp_text_meta">Mother language </label><br />-->
<textarea id="bsp_text_meta" name="bsp_text_meta" class="widefat" style="overflow:auto; resize:none" rows="15"><?php if(isset($value['bsp_text_meta'])) {echo $value['bsp_text_meta'][0]; } ?></textarea>

//check if user can edit post
if( !current_user_can( 'edit_posts' ) ) {
    return;  
}

if( isset($_POST['bsp_text_meta'] )) {
    update_post_meta($post_id, 'bsp_text_meta', esc_attr($_POST['bsp_text_meta']));
}
}

badge is the name of a custom post type (slug).

0

If you are not using multiple key & values, I'd try it this way

// This function adds a meta box with a callback function of my_metabox_callback()
function add_my_meta_box() {
     $var1 = 'this';
     $var2 = 'that';
     add_meta_box( 
           'metabox_id',
           'Metabox Title',
           'my_metabox_callback',
           'page',
           'normal',
           'low', 
           array( 'foo' => $var1, 'bar' => $var2)
      );
}

// $post is an object containing the current post (as a $post object)
// $metabox is an array with metabox id, title, callback, and args elements. 
// The args element is an array containing your passed $callback_args variables.

function my_metabox_callback ( $post, $metabox ) {
     echo 'Last Modified: ' . $post->post_modified;        // outputs last time the post was modified
     echo $metabox['args']['foo'];                         // outputs 'this'
     echo $metabox['args']['bar'];                         // outputs 'that'
     echo get_post_meta( $post->ID, 'my_custom_field', true ); // outputs value of custom field
}

Also you can use the_content filter to add anything to single.php

Example code:

function tsi_source_link($content) {
 if ( is_single() ) {
   global $post;
   $source_link = get_post_meta ($post->ID, 'syndication_permalink', true);
   if ($source_link) {
    $content .= '<div id="tsi-source-link" style="text-align:right;"><a href="' . $source_link . '" target="_blank">Read more ...</a></div>';
}
}
return $content;
}
add_action('the_content','tsi_source_link');
  • Hi @Wali, this also does not work. I found a temporary solution to add a code $meta_value = get_post_meta( get_the_ID(), 'bsp_meta-text', true ); if( !empty( $meta_value ) ) {echo $meta_value; } into themes single.php But because it's a plugin it's not a good idea. – dea May 19 '15 at 18:13
  • I am not sure why this won't work or is not a good idea if in Plugin? Can you share please? – Wali Hassan May 19 '15 at 20:25
  • This bit of code is now manually added to the themes single.php. If it's a plugin I can't do that, because I don't know which themw the user has. I somehow need to add the code from a plugin to the theme's single.php but I don't know how to do that. Or there is a better solution for showing the meta box on the front end. @wali – dea May 20 '15 at 5:56
  • If I use the code you provided the metabox is not showing on the page. I think because when I click publish the post the meta box is empty, so that means it doesn't save the metabox whit your approach. – dea May 20 '15 at 6:51
  • The code I gave you was for your reference on how to create metaboxes and get their value. I will edit my post with complete code. – Wali Hassan May 20 '15 at 14:23

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.