Solved

Wordpress add custom fields to 'Your Profile'

Posted on 2011-03-25
2
718 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
2 Comments
 
LVL 9

Accepted Solution

by:
erdincgc 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Read about why website design really matters in today's demanding market.
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
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 …

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now