Solved

Wordpress add custom fields to 'Your Profile'

Posted on 2011-03-25
2
742 Views
Last Modified: 2012-05-11
I'm using the Custom Metadata Manager plugin - http://wordpress.org/extend/plugins/custom-metadata/

I have it working with adding additional fields to the user pages, however this is not carrying over to the profile page.

You'll note if there in the x_add_metadata_group and x_add_metadata_feild the second var is set at user - which sets the user post_type / field group to users page(s). However, when logged in this is not visible on 'Your Profile'.

If there is a way to register the fields to Your Profile with just php, I'm up for that too.



add_action( 'admin_head', 'init_my_custom_fields' , 10 , 1 );

function init_my_custom_fields() {
	
	if( function_exists( 'x_add_metadata_group' ) && function_exists( 'x_add_metadata_field' ) ) {
		x_add_metadata_group( 'x_metaBox1', 'user', $args = array(
			'label' => 'Contact Numbers'
		) );
		
		function fieldCustomHidden1_display( $field_slug, $field, $value ) {
			if( ! $value ) $value = 'This is a secret hidden value! Don\'t tell anyone!';
			?>
			<hr />
			<p>This is a hidden field rendered with a custom callback. The value is "<?php echo $value; ?>".</p>
			<input type="hidden" name="<?php echo $field_slug; ?>" value="<?php echo $value; ?>" />
			<hr />
			<?php
		}
		
	
		
		function fieldCustomList1_display( $field_slug, $field, $object_type, $object_id, $value ) {
			$value = (array)$value;
			$field_class = sprintf( 'field-%s', $field_slug );
			$count = 0;
			?>
			<p>This is an example field rendered with a custom display_callback. All done with about 40 lines of code!</p>
			
			<?php if( empty( $value ) ) $value = array(); ?>
				<?php foreach( $value as $v ) : ?>
					<div class="f1_my-list-item">
						<input type="text" name="<?php echo $field_slug; ?>[]" value="<?php echo esc_attr( $v ); ?>" />
						<?php if( $count > 0 ) : ?>
							<a href="#" class="f1_btn-del-list-item hide-if-no-js" style="color:red;">Delete</a>
						<?php endif; ?>
					</div>
					<?php $count++; ?>
				<?php endforeach; ?>
			<p><a href="#" class="f1_btn-add-list-item hide-if-no-js">+ Add New</a></p>
			
			<script>
			;(function($) {
				$('.f1_btn-add-list-item').click(function(e) {
					e.preventDefault();
					var $last = $('.f1_my-list-item:last');
					var $clone = $last.clone();
					
					$clone
						.insertAfter($last)
						.find(':input')
							.val('')
						;
				});
				$('.f1_btn-del-list-item').live('click', function(e) {
					e.preventDefault();
					$(this).parent().remove();
				});
			})(jQuery);
			</script>
			<?php
		}
		
		
		
		function fieldCustomList2_display( $field_slug, $field, $object_type, $object_id, $value ) {
			$value = (array) $value;
			$field_class = sprintf( 'field-%s', $field_slug );
			$count = 0;
			?>
			<p>This is an example field rendered with a custom display_callback (renders multiple fields and js) and a custom sanitize_callback (aggregates the submitted data into a single array).</p>
			
			<?php if( empty( $value ) ) array_push( $value, array() ); ?>
				
			<?php foreach( $value as $v ) : ?>
				<?php
				$text = isset( $v['text'] ) ? $v['text'] : '';
				$url = isset( $v['url'] ) ? $v['url'] : '';
				?>
				<div class="f2_my-list-item">
					<label>Text</label>
					<input type="text" name="<?php echo $field_slug; ?>_text[]" value="<?php echo esc_attr( $text ); ?>" />
					
					<label>URL</label>
					<input type="text" name="<?php echo $field_slug; ?>_url[]" value="<?php echo esc_attr( $url ); ?>" />
					
					<?php if( $count > 0 ) : ?>
						<a href="#" class="f2_btn-del-list-item hide-if-no-js" style="color:red;">Delete</a>
					<?php endif; ?>
					<?php $count++; ?>
				</div>
			<?php endforeach; ?>
			
			<p><a href="#" class="f2_btn-add-list-item hide-if-no-js">+ Add New</a></p>
			
			<script>
			;(function($) {
				$('.f2_btn-add-list-item').click(function(e) {
					e.preventDefault();
					var $last = $('.f2_my-list-item:last');
					var $clone = $last.clone();
					
					$clone
						.insertAfter($last)
						.find(':input')
							.val('')
						;
				});
				$('.f2_btn-del-list-item').live('click', function(e) {
					e.preventDefault();
					$(this).parent().remove();
				});
			})(jQuery);
			</script>
			<?php
		}
		
		function fieldCustomList2_sanitize( $field_slug, $field, $object_type, $object_id, $value ) {
			$values = array();
			$text_key = $field_slug . '_text';
			$url_key = $field_slug . '_url';
			
			if( isset( $_POST[$text_key] ) ) {
				$count = 0;
				foreach( (array) $_POST[$text_key] as $text ) {
					$url = isset( $_POST[$url_key][$count] ) ? $_POST[$url_key][$count] : '';
					if( $text || $url ) {
						array_push( $values, array(
							'text' => $text
							, 'url' => $url
						) );
					}
					$count++;
				}
			}
			
			return $values;
		}
		
				
		function blog_links_column_callback( $field_slug, $field, $object_type, $object_id, $value ) {
			switch( $value ) {
				case 'google':
					$url = 'http://google.com';
					break;
				case 'bing':
					$url = 'http://bing.com';
					break;
				default:
					$url = '';
					break;
			}
			if( $url )
				return sprintf( '<a href="%s" target="_blank">Go to search</a>', $url );
			return __( 'Search Engine not selected' );
		}
		x_add_metadata_field('Phone', 'user', array(
			'group' => 'x_metaBox1'
			, 'description' => '###-###-####'
			, 'label' => 'Phone'
			, 'display_column' => true
		));
		x_add_metadata_field('Pager', 'user', array(
			'group' => 'x_metaBox1'
			, 'description' => '###-###-####'
			, 'label' => 'Pager'
			, 'display_column' => true
		));
		x_add_metadata_field('Fax', 'user', array(
			'group' => 'x_metaBox1'
			, 'description' => '###-###-####'
			, 'label' => 'Fax'
			, 'display_column' => true
			
		));

		
				
	}
}

Open in new window

0
Comment
Question by:m2ew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
Erdinç Güngör Çorbacı earned 250 total points
ID: 35237022
Long process to write down here so this link will be better to learn the things that are needed to make this addition.

http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

not complicated i think but if you need to ask about , i will be around. :)
0
 

Author Closing Comment

by:m2ew
ID: 35241714
Thanks for the link. Very helpful.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question