0

I have created a dynamic meta field that is generated for each post type. I used the following to create the meta-field:

<?php        
$args=array(
'post_type' => 'Course',
'status' => 'approve'
);
                        $allposts = get_posts( $args );
                        foreach ( $allposts as $post ) { 
                        $course_name = $post->post_name;
                        $post_course_meta = $_POST[$post->post_name];
                        $radio_status = get_user_meta( $user->ID, $course_name, true );
                        ?>                                   
<input type="radio" name="<?php echo $post->post_name; ?>" value="Did not start" <?php if ($radio_status == 'Did not start' OR $radio_status == '' ) { ?>checked="checked"<?php }?> /><label>Did not start"</label><br>
<input type="radio" name="<?php echo $post->post_name; ?>" value="Started" <?php if ($radio_status == 'Started' ) { ?>checked="checked"<?php }?> /><label>Started</label><br>
<input type="radio" name="<?php echo $post->post_name; ?>" value="Finished" <?php if ($radio_status == 'Finished' ) { ?>checked="checked"<?php }?> /><label>Finished</label>

And to save it:

function save_educadme_courses_for_user( $user_id ) {

 if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

    $args=array(
        'post_type' => 'Course',
        'status' => 'approve'
        );
    $allposts = get_posts( $args );

    foreach ( $allposts as $post ) {
    $course_name = $post->post_name;
    $post_course_meta = $_POST[$post->post_name];
    update_user_meta( $user_id, $course_name, $post_course_meta );
    }//end of master loop

}

And to display & save, I am using

add_action( 'show_user_profile', 'educadme_courses_for_user' );
add_action( 'edit_user_profile', 'educadme_courses_for_user' );
add_action( 'personal_options_update', 'save_educadme_courses_for_user' );
add_action( 'edit_user_profile_update', 'save_educadme_courses_for_user' );

Now, everything is working fine if I wanted to update through the profile itself. However, I don't intent to do that.

I want to be able to also edit through the profile edit page, but, mainly, I want to be able to also update the meta field by hooking an update function to actions which are run on different parts of the website. However, when I try to do that, the update I run through the external function doesn't update the value of the radio or at least when I open the profile page, it saves original value I set through the profile.

For instance, I wanted to test this function:

function testtt() {
    if ( is_user_logged_in() ) {
    update_user_meta( $user_id, 'research-introduction', 'Finished' );
    }
}
add_action( 'wp_footer', 'testtt' );

But nothing happened! The value (radio button) that was checked through the profile update page is still selected.

I changed a bit of code and was able to make it work the other way around, to fetch value and update but not have any effect when edited through the profile page. This brought me to the question, can I allow or use updates both ways? If yes, what am I doing wrong please?

Thanks!

  • wp_footer doesn't pass the $user_id variable. That code won't work. If you need to update that field in multiple contexts you will have to modify your code (potentially) for each context. If you want help with that you need to provide details about exactly when and where you need this to work. – s_ha_dum Jan 2 '14 at 0:48
  • @s_ha_dum Sorry I wasn't clear. That function actually didn't work with the current was the save function is using $post. I actually tried the same function with a slightly different save function and it worked fine. – Dr.Hariri Jan 2 '14 at 3:13
  • To answer your question, I plan to use an action that is created with a plugin I am using. With the action, I can access the user id as well as the post->name. Thanks! – Dr.Hariri Jan 2 '14 at 3:14
  • Is the question solved, then? If so, post your answer and accept it. – s_ha_dum Jan 2 '14 at 5:27
  • No it is not solved. As I've said, I can't update with both methods. – Dr.Hariri Jan 2 '14 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.