Link to home
Start Free TrialLog in
Avatar of Mike Tew
Mike TewFlag for Canada

asked on

Wordpress add custom fields to 'Your Profile'

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

ASKER CERTIFIED SOLUTION
Avatar of Erdinç Güngör Çorbacı
Erdinç Güngör Çorbacı
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tew

ASKER

Thanks for the link. Very helpful.