1

I have created form for enquiry. When user fill this form then I am saving it into database as a custom post type. In backend I need to show extra information on custom post edit page. But did not find any hook.

I have tried this code:

[![function add_extra_info_column( $columns ) {
    return array_merge( $columns, 
        array( 'sticky' => __( 'Extra Info', 'your_text_domain' ) ) );
}
add_filter( 'manage_posts_columns' , 'add_extra_info_column' );][1]][1] 

But it adds a new column in custom post.

I need to show extra info when we click on edit page link for every post.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
php_dev
  • 113
  • 1
  • 11
  • You mean you have to add custom field in your custom post type right? – Purvik Dhorajiya Jun 04 '16 at 04:17
  • Before enquiry form user is adding products in cart for enquiry. when he submit enquiry then i am saving cart data in database. Now i need to show that cart data also in admin. – php_dev Jun 04 '16 at 04:23
  • I have created a custom post named enquiry_request.I am saving enquiry form data in enquiry_request post type. Then in admin left sidebar it is showing my custom post type enquiry requests.But not showing cart data.when we click on edit option of custom post types then it goes to edit page of enquiry reqest. where we can see title and other custom fields data. and on edit page view i need to show cart data like in table format after title before content editor. – php_dev Jun 04 '16 at 04:32
  • Try this one, it does exactly what you need : https://wordpress.org/plugins/wc-fields-factory/ – Sark Jun 04 '16 at 07:07
  • @Sark kindly, the purpose on SO is trying to solve problems around code, like asked on the question. Plugins solutions are kind off topic and the real easy way. I really take time first to find solutions around code as it's the use on SO, so please, if you could avoid that, it will be very fair. thanks. – LoicTheAztec Jun 05 '16 at 15:46
  • @LoicTheAztec Why reinvent the wheel when there are solutions for that?. Anyway I got your point thanks :) – Sark Jun 06 '16 at 05:32

1 Answers1

4

This is just an example and you have to customize it for your needs:

First step: Adding Meta container hook to backend (for example here for products post type):

add_action( 'add_meta_boxes', 'extra_info_add_meta_boxes' );
if ( ! function_exists( 'extra_info_add_meta_boxes' ) )
{
    function extra_info_add_meta_boxes()
    {
        global $post;

        add_meta_box( 'extra_info_data', __('Extra Info','your_text_domain'), 'extra_info_data_content', 'product', 'side', 'core' );
    }
}

(replace 'product' by your post type and this meta box could be like here on 'side' or by 'normal' to get it on main column)

Second step: Adding information in this metabox (fields, data, whatever…)

function extra_info_data_content()
{
    global $post;
    // Here you show your data  <=====

}

References:


Third step (optional): Save the data of the custom meta post (needed if you have some fields).

add_action( 'save_post', 'extra_info_save_woocommerce_other_fields', 10, 1 );
if ( ! function_exists( 'extra_info_save_woocommerce_other_fields' ) )
{

    function extra_info_save_woocommerce_other_fields( $post_id )
    {
        // Check if our nonce is set.
        if ( ! isset( $_POST[ 'extra_info_other_meta_field_nonce' ] ) )
        {
            return $post_id;
        }
        $nonce = $_REQUEST[ 'extra_info__other_meta_field_nonce' ];

        //Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce ) )
        {
            return $post_id;
        }

        // 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 $post_id;
        }

        // Check the user's permissions.
        if ( 'page' == $_POST[ 'post_type' ] )
        {
            if ( ! current_user_can( 'edit_page', $post_id ) )
            {
                return $post_id;
            }
        }
        else
        {
            if ( ! current_user_can( 'edit_post', $post_id ) )
            {
                return $post_id;
            }
        }
        /* --- !!! OK, its safe for us to save the data now. !!! --- */

        // Sanitize user input and Update the meta field in the database.
    }
}
Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Yes it is showing content in rightsidebar but what i need to use inplace of "side" to show it inplace of content editor.Because i don't need editior. add_meta_box( 'extra_info_other_fields', __('Extra Info','your_text_domain'), 'extra_info_add_other_fields_for_packaging', 'requestquote', 'side', 'core' ); – php_dev Jun 04 '16 at 04:44