• I am a beginner in coding but i have just wrote this code and i got an error ??

    this code must be placed in author.php file , so it creates a follow button in author’s page

    and when i click on it , it should save the author-id in a logged-in user meta called “following” ..

    <?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>

    but i think this meta have to be an “array variable” in order not to replace the previous author id

    with the new one .

    anyone can help me please ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • It works better for you if you supply all the information that you can.

    just wrote this code and i got an error ??

    What was the error message ?

    Often it is helpful if you provide your website address.
    Also it would be helpful if you explained in more detail what you want to do, where you have placed this code.

    Thread Starter sufyan_hassan

    (@sufyan_hassan)

    The concept of code :

    the code should Add a form with a button to each user profile, it named Follow or Unfollow.

    so , 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.

    i get this error: “Catchable fatal error: Object of class WP_User could not be converted to string”,

    because i used this code to check if it’s working or not :/

    <?php
    $user_id = 1;
    $key = ‘following’;
    $single = true;
    $user_last = get_user_meta( $user_id, $key, $single );
    echo ‘<p>The ‘. $key . ‘ value for user id ‘ . $user_id . ‘ is: ‘.$user_last . ‘</p>’; ?>

    Your problem is in the line:
    echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: '.$user_last . '</p>';

    You are attempting to display $user_last as text, this is what it is complaining about. Try this instead:

    echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ';
     print_r( $user_last );
     echo '</p>';

    This will both get past your error, and show you the fields that are in $user_last

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Is this code true or not ?!’ is closed to new replies.