Solved

Wordpress add custom fields to 'Your Profile'

Posted on 2011-03-25
2
733 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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