• hi guys i am trying to create my first custom meta box, i am not sure if this is the right place to put this but if its not, please forgive me its my first time posting too.

    Ive been at this for a couple of days now and i cant figure out what i am doing wrong. i will appreciate your help, you can add or remove anything you want from my code to make it work, at this moment i will try anything really.

    what the meta box suppose to do: its a very simple meta box that accepts one input field that the user will have the ability to add, delete or update client names.

    here is the code:

    add_action( 'init', 'create_clients' );
    function create_clients(){
    	register_post_type( 'clients',
    		array(
    			'labels' => array(
    				'name' => 'Clients',
    				'singular_name' => 'Client',
    				'add_new' => 'Add New',
    				'add_new_item' => 'Add New Clients',
    				'edit' => 'Edit',
    				'edit_item' => 'Edit Clients',
    				'new_item' => 'New Client',
    				'view' => 'View',
    				'view_item' => 'View Client',
    				'search_items' => 'Seach Client',
    				'not_found' => 'No Client found',
    				'not_found_in_trash' => 'No Client found in Trash',
    				'parent_item_colon' => '',
    				'menu_name' => 'Clients'
    	         ),
    		   'public' => true,
    		   'menu_position' => 30,
    		   'supports' => array(''),
    		   'taxonomies' => array( '' ),
    		   'menu_icon' => plugins_url('images/icon_16.png', __FILE__ ),
    		   'has_archive' => true,
    		   'show_in_menu' => true
    		)
         );
    
    }
    
    add_action( 'add_meta_boxes', 'client_admin' );
    function client_admin(){
    	add_meta_box(
    		'client_metabox_id',
    		'Client Details',
    		'display_clients_meta_box',
    		'clients',
    		'normal',
    		'high'
    	);
    }
    
    function display_clients_meta_box($clients){
    	//print_r($clients);
    
        $clientName = esc_attr( get_post_meta( $clients->ID, 'display_clients', true ));
    	//echo 'good';
    ?>
    	<?php wp_nonce_field( basename( __FILE__ ), 'clients_post_class_nonce' ); ?>
        <table width="513">
            <tr>
                <td width="90"><?php _e("Client Name:"); ?></td>
                <td width="411" align="left"><input type="text" size="50" name="client_name" value="<?php echo $clientName; ?>" id="clients-post-class" /></td>
            </tr>
        </table>
    
    <?php }
    
    add_action( 'save_post', 'add_clients_fields', 10, 2 );
    function add_clients_fields( $client_id, $clients) {
    
            /* Verify the nonce before proceeding. */
    	if ( !isset( $_POST['clients_post_class_nonce'] ) || !wp_verify_nonce( $_POST['clients_post_class_nonce'], basename( __FILE__ ) ) )
    		return $client_id;
    
    	/* Get the post type object. */
    	$post_type = get_post_type_object( $clients->post_type );
    
    	/* Check if the current user has permission to edit the post. */
    	if ( !current_user_can( $post_type->cap->edit_post, $client_id ) )
    		return $client_id;
    
    	/* Get the posted data and sanitize it for use as an HTML class. */
    	$new_meta_value = ( isset( $_POST['client_name'] ) ? sanitize_html_class( $_POST['client_name'] ) : '' );
    
    	/* Get the meta key. */
    	$meta_key = 'clients_post_class';
    
    	/* Get the meta value of the custom field key. */
    	$meta_value = get_post_meta( $client_id, $meta_key, true );
    
    	/* If a new meta value was added and there was no previous value, add it. */
    	if ( $new_meta_value && '' == $meta_value )
    		add_post_meta( $client_id, $meta_key, $new_meta_value, true );
    
    	/* If the new meta value does not match the old value, update it. */
    	elseif ( $new_meta_value && $new_meta_value != $meta_value )
    		update_post_meta( $client_id, $meta_key, $new_meta_value );
    
    	/* If there is no new meta value but an old value exists, delete it. */
    	elseif ( '' == $new_meta_value && $meta_value )
    		delete_post_meta( $client_id, $meta_key, $meta_value );
    
    }

  • The topic ‘custom metabox problem (PLEASE HELP ME)’ is closed to new replies.