1
\$\begingroup\$

As a beginner in coding, I have just written this code.

The code should Add a form with a button to each user profile, it named Follow or Unfollow. It updates the user meta named "following" for the user who clicked the button. So we have to use the user IDs as a serialized array. Then we should use a query for the user meta "following" to get a list of all authors the current user has subscribed to and then query for the posts from these authors.

<?php

 if($_GET['follow']){fun1();}

 function fun1()
 {
$fauid = get_user_by( 'slug', get_query_var( 'author_name' ) );
$user_id= get_current_user_id();
$key = 'following';
$themeta = get_user_meta($user_id , $key, TRUE);
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);
}
}

?>

<html>
    <button id="Button" name="Button" onClick='location.href="?follow=1"'>Follow Me <3 <3</button>
</html>

Is the code true or not?

\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! What do you mean by "Is the code true or not"? Have you tested this code? \$\endgroup\$ – Simon Forsberg Jan 10 '15 at 12:25
2
\$\begingroup\$
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.

$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.

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.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much for these important Tips ^_^ I don't know how to program html , php , but i wrote this code after 5 hours of searching in google :( , can you help me please or tell me how to manage with serialized array ^_^ please . \$\endgroup\$ – Sufyan Hassan Jan 11 '15 at 8:30
  • \$\begingroup\$ @SufyanHassan I updated the answer with the serialized array implementation, as I understand it. \$\endgroup\$ – Brythan Jan 11 '15 at 20:01

Your Answer

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

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