This model only allows you to follow one author at a time. A second author will replace the first. It's more common to be able to follow as many as you want. You might be able to manage with a serialized array serialized array, but a more robust solution would be a table that matched readers to authors.
This model only allows you to follow one author at a time. A second author will replace the first. It's more common to be able to follow as many as you want. You might be able to manage with a serialized array, but a more robust solution would be a table that matched readers to authors.
This model only allows you to follow one author at a time. A second author will replace the first. It's more common to be able to follow as many as you want. You might be able to manage with a serialized array, but a more robust solution would be a table that matched readers to authors.
This model only allows you to follow one author at a time. A second author will replace the first. It's more common to be able to follow as many as you want. You might be able to manage with a serialized arrayserialized array, but a more robust solution would be a table that matched readers to authors.
$following = get_user_meta($user_id, 'following', true);
if ( ! is_array($following) || empty($following) ) {
add_user_meta($user_id , 'following' , array($fauid) , true );
} else if ( ! in_array($fauid, $following) ) {
$following[] = $fauid;
update_user_meta($user_id, 'following', $following);
}
If there's no following array, it creates one. If one already exists and doesn't already have this user ID in it, add to the existing array and update.
This model only allows you to follow one author at a time. A second author will replace the first. It's more common to be able to follow as many as you want. You might be able to manage with a serialized array, but a more robust solution would be a table that matched readers to authors.
This model only allows you to follow one author at a time. A second author will replace the first. It's more common to be able to follow as many as you want. You might be able to manage with a serialized array, but a more robust solution would be a table that matched readers to authors.
$following = get_user_meta($user_id, 'following', true);
if ( ! is_array($following) || empty($following) ) {
add_user_meta($user_id , 'following' , array($fauid) , true );
} else if ( ! in_array($fauid, $following) ) {
$following[] = $fauid;
update_user_meta($user_id, 'following', $following);
}
If there's no following array, it creates one. If one already exists and doesn't already have this user ID in it, add to the existing array and update.
if($_GET['follow']){fun1();}
A more typical way to write this would be
if ( $_GET['follow'] ) {
fun1();
}
As a general rule, do not put statements on the same line as the curly brace that opens the block.
$key = 'following';
$themeta = get_user_meta($user_id , $key, TRUE);
Since you never use $key
again, you don't need it.
$following = get_user_meta($user_id, 'following', true);
If you're only getting the meta data for 'following'
, $following
is probably a better name than $themeta
.
if($themeta != '') {
$user_id= get_current_user_id;
update_user_meta($user_id, 'following', $fauid); }
else {
$user_id= get_current_user_id;
add_user_meta($user_id , 'following' , $fauid , true );
update_user_meta($user_id, 'following', $fauid);
}
Two of the three statements are in both branches of the if
. Therefore, you could pull them out:
$user_id= get_current_user_id;
if($themeta != '') {
} else {
add_user_meta($user_id , 'following' , $fauid , true );
}
update_user_meta($user_id, 'following', $fauid);
Note that the order of the statements will stay the same with the add_user_meta
in the middle.
But if you do that, one of the branches is empty. You might as well remove it.
$user_id = get_current_user_id;
if ( '' == $themeta ) {
add_user_meta($user_id , 'following' , $fauid , true );
}
update_user_meta($user_id, 'following', $fauid);
You also call
$user_id= get_current_user_id;
multiple times. You should be able to do this just once.
The update_user_meta
function will overwrite the value that you just added, so you might be better off with
if ( '' == $themeta ) {
add_user_meta($user_id , 'following' , $fauid , true );
} else {
update_user_meta($user_id, 'following', $fauid);
}
Now you add or update, not both.
This model only allows you to follow one author at a time. A second author will replace the first. It's more common to be able to follow as many as you want. You might be able to manage with a serialized array, but a more robust solution would be a table that matched readers to authors.
It's also worth noting that this is not the normal way to write for WordPress. WordPress usually makes additions by adding plugins, and this doesn't look like a plugin to me. For one thing, it doesn't register any hooks. Nor does it define an install/activate procedure.