Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am developing a wp plugin which supports custom post type in that i have create a metabox everything goes well.When i enter the values in my metabox field it will not display in admin panel where as it will store in database(wp_postmeta table ).Can any one suggest me what is going wrong with my code.

//Create Meta box
 function astest_add_metabox(){
 add_meta_box("astestimonial_metaboxes", "AS-Testimonial Client Information", "astestimonial_meta_box", "astestimonial", "side", "low");
 }
add_action('add_meta_boxes','astest_add_metabox');

//html code
function astestimonial_meta_box($post_id){
wp_nonce_field('astest_meta_box','astest_meta_box_nonce');
$clientname = get_post_meta($post->ID,'clientname_value','true');
echo'<label for="client_name_val">';
_e('Name: ', 'astestimonial_textdomain');
echo'</label>';
echo'<input type="text" id="clientname_value" name="clientname_value"   value="'.esc_attr($clientname).'" />';

//save post
function as_testi_save_data($post_id){
global $post;
if(isset($_POST['post_type']) && ($_POST['post_type'] == "astestimonial"))
{
    $cli_name_data = $_POST['clientname_value'];

update_post_meta($post_id, 'clientname_value', $cli_name_data);
 }
}

add_action("save_post", "as_testi_save_data");

Can anyone find what is wrong with this.. Thanks

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You forgot to declare global $post

//Create Meta box
function astest_add_metabox() {
    add_meta_box("astestimonial_metaboxes", "AS-Testimonial Client Information",    "astestimonial_meta_box", "astestimonial", "side", "low");
}
add_action('add_meta_boxes','astest_add_metabox');

//html code
function astestimonial_meta_box() {
    global $post;
    wp_nonce_field('astest_meta_box','astest_meta_box_nonce');
    $clientname = get_post_meta($post->ID,'clientname_value',true);
    echo'<label for="client_name_val">';
    _e('Name: ', 'astestimonial_textdomain');
    echo'</label>';
    echo'<input type="text" id="clientname_value" name="clientname_value"   value="'.esc_attr($clientname).'" />';
}

//save post
function as_testi_save_data($post_id){
    if(isset($_POST['post_type']) && ($_POST['post_type'] == "astestimonial"))  {
            $cli_name_data = $_POST['clientname_value'];
            update_post_meta($post_id, 'clientname_value', $cli_name_data);
    }
}

add_action("save_post", "as_testi_save_data");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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