Wordpress User Meta retrieving advance data for meta_key

Sunday, July 6, 2014

I am creating a custom profile form for one of my websites based on Wordpress. I am using add more button for a particular field since I do not know how many inputs may the user like to enter for it.


I am inserting the data into wp_usermeta; I have no issues while inserting the data however while retrieving I can’t get the count of the meta_key since the meta key stored are as ts_date_1 ts_date_2 etc


Thanks for your time.


HTML:



<tr><td><input type='text' name='ts_date[]' id='ts_date_1' /></td></tr>


Javascript:



<script>
function addanotherts() {
var root=document.getElementById('timesheettable');
var divs=root.getElementsByTagName('tr');
var clone=divs[divs.length-1].cloneNode(true);
root.appendChild(clone);
var root=document.getElementById('timesheettable');
var divs=root.getElementsByTagName('tr');
var aaa = divs.length-1;
var abc = divs[divs.length-1];

abc1 = abc.getElementsByTagName("input");
abc1[0].setAttribute("id",'ts_date_'+aaa);
abc1[0].value = '';

abc2 = abc.getElementsByTagName("select");
abc2[0].setAttribute("id",'ts_comments_'+aaa);
abc2[0].value = '';

}
</script>


PHP: For inserting into DB



$ts_date = $_POST['ts_date'];
for($ii = 0 ; $ii < count($ts_date) ; $ii++) {
if(!empty ($ts_date[$ii])){
update_user_meta( $current_user->ID, 'ts_date_'.$ii, esc_attr( $ts_date[$ii] ) );
}
}


For Retriving from DB **



  • (note: Note in the following code $ii < 10; 10 is an assumed number but not the actual count. )


**



<?php
for($ii = 0; $ii < 10; $ii++){
?>
<tr>
<td><input type='text' name='ts_date_<?php echo $ii ?>' value="<?php echo esc_attr( get_the_author_meta( 'ts_date_'.$ii, $user->ID ) );?>" /></td>
</tr>
<?php
}
?>






http://ift.tt/1mwQm5w