2

I am trying to integrate both the values from an existing custom taxonomy and custom author profile fields. I would like the values to be used as the options from which an author can select when updating/filling out her profile.

The below works - kind of. (Found in functions.php file)

It saves to the database (users table) but it the "checked" radio does not save. It also does not print properly in the profile section, and looks like this (immediately preceding the radio button) checked="checked" />

... Here is the full code

<tr>
<th><label for="sorority"><?php _e('What sorority are you in?'); ?></label></th>
    <td>
        <?php $sorority = get_the_author_meta( 'sorority', $user->ID ); ?>
        <?php
             $terms = get_terms("sorority");
             $count = count($terms);
             echo '<ul>';
                    foreach ( $terms as $term ) {   
                        echo '<li><input type="radio" value="' . esc_attr( $term->name ) . '" name="sorority" <?php if ($sorority == "' . esc_attr( $term->name ) . '") { ?>checked="checked"<?php }?> />'.$term->name.'</li>';
             }
             echo "</ul>";
            ?>
    </td>
</tr>

Any thoughts? I believe the problem is something with this line:

 <?php if ($sorority == "' . esc_attr( $term->name ) . '") { ?>checked="checked"<?php }?> 
1
  • I assume this is done in meta box, can you also show us save function attached to save_post action hook?
    – Mamaduka
    Sep 20, 2012 at 6:40

2 Answers 2

1

Figured it out, for anyone who needs help with this I'm sure you can use with categories/tags as well (can also use with radio inputs and checked="checked":

<tr>
 <th><label for="sorority"><?php _e('Sorority is...') ?></label></th>
  <td><?php $sorority = get_the_author_meta( 'sorority', $user->ID ); ?>
    <select name="sorority" id="sorority">
    <?php $terms = get_terms('sorority'); foreach ( $terms as $term ) { ?>
    <option name="sorority" value="<?php echo $term->name; ?>" <?php if ( $sorority == $term->name ) {?> selected="selected" <?php }?>><?php echo $term->name; ?></option>
    <?php } ?>
    </select>
 </td> 
</tr>
1
  • 1
    Keep an eye on those little helper functions. selected() for example.
    – kaiser
    Oct 9, 2012 at 16:54
0

Mamaduka,

Thanks for your response. No, its not a post saving thing - this creating extra user profile fields (http~/wp-admin/user-edit.php) with the values that are also "descendants" of a custom taxonomy.

ie. Taxonomy = Ice cream, descendants/children/taxonomies = Chocolate, Vanilla. I want Chocolate and Vanilla to be options that an author can choose from when asked the question found on their profile "What is your favorite flavor icecream?" However, there are HUNDREDS of different flavors of ice cream, and instead of manually creating all of these "potential" values - I would like to use the taxonomy data to dynamically create such options. This means that if I add a new child tax called "strawberry" under "Add New Ice Cream" - the option should also automatically populate in the edit user/profile page.

See the code from my functions.php file below. This spits out the correct data but it is "broken" when it outputs to the screen (looks like this immediately preceding the radio button: checked="checked" />Chi Omega)

Also, the radio button when SELECTED and SAVED will not save as selected, but it does store the correctly selected values into the users table database.

Any help is much appreciated.

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>Additional Information</h3>
<table class="form-table">

<tr>
        <th><label for="sorority"><?php _e('What is your sorority?'); ?></label></th>
        <td>
        <?php $sorority = get_the_author_meta( 'sorority', $user->ID ); ?>
        <?php
             $terms = get_terms("sorority");
             $count = count($terms);
             echo '<ul>';
                    foreach ( $terms as $term ) {   
                        echo '<li><input type="radio" value="' . esc_attr( $term->name ) . '" name="sorority" <?php if ($sorority == "' . esc_attr( $term->name ) . '") { ?>checked="checked"<?php }?> />'.$term->name.'</li>';
             }
             echo "</ul>";
            ?>
</td>           
</tr>

</table>
<?php }   
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;
update_usermeta( $user_id, 'sorority', $_POST['sorority'] );
}
1
  • Welcome to WordPress Stack Exchange. Please move this text to your question. Do not use the answer field for anything but answers.
    – fuxia
    Sep 21, 2012 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.