WordPress.org

Ready to get started?Download WordPress

Forums

How to add a registered user "signature" to each comment (2 posts)

  1. TWD
    Member
    Posted 2 years ago #

    I'd like each logged in user of my site (i.e. those with an account) to be able to create a "signature" tag, similar to that used on forums, to be displayed on any comments they leave.

    Is there a plugin or other way to do this?

  2. TWD
    Member
    Posted 2 years ago #

    Not a plugin, but this tutorial seems easy enough to implement
    to get the same result. For anyone interested.

    You just need to add some code to the functions.php file that creates custom fields in the user profile and then retrieve those meta details into your theme.

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

    Open your theme’s functions.php file and drop this PHP code in:

    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    
    function my_show_extra_profile_fields( $user ) { ?>
    
    	<h3>Extra profile information</h3>
    
    	<table class="form-table">
    
    		<tr>
    			<th><label for="twitter">Twitter</label></th>
    
    			<td>
    				<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
    				<span class="description">Please enter your Twitter username.</span>
    			</td>
    		</tr>
    
    	</table>
    <?php }

    Also in the functions.php file to save the custom fields:

    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    
    	if ( !current_user_can( 'edit_user', $user_id ) )
    		return false;
    
    	/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
    	update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
    }

    and finally this to your functions.php file to display the meta-data

    function my_author_box() { ?>
    	<div class="author-profile vcard">
    		<?php echo get_avatar( get_the_author_meta( 'user_email' ), '96' ); ?>
    
    		<h4 class="author-name fn n">Article written by <?php the_author_posts_link(); ?></h4>
    
    		<p class="author-description author-bio">
    			<?php the_author_meta( 'description' ); ?>
    		</p>
    
    		<?php if ( get_the_author_meta( 'twitter' ) ) { ?>
    			<p class="twitter clear">
    				<a href="http://twitter.com/<?php the_author_meta( 'twitter' ); ?>" title="Follow <?php the_author_meta( 'display_name' ); ?> on Twitter">Follow <?php the_author_meta( 'display_name' ); ?> on Twitter</a>
    			</p>
    		<?php } // End check for twitter ?>
    	</div><?php
    }

    Now in the single.php file you can add this after the post has been displayed:

    <?php my_author_box(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags