• Hi, Im trying to create a private notebox on user profiles page (Im also using Userpro plugin) I figured the way Id do this is to add a ‘notes’ row to the database, Then on the profile page, I have the shortcode for the userpro profile field.

    Below that I have want a box which will show if admin has left any messages for that user, Users cant reply, and only the User whose profile its on can see the notes.

    Im new to wordpress so please bare with. But Ive got it so that it will call the notes row for the user from the Database, However only admin can see the notes, how can I also make it so the user can see the notes too?

    <?php
    
    $user = wp_get_current_user();
    $allowed_roles = array('editor', 'administrator'; ?>
    <?php if( array_intersect($allowed_roles, $user->roles ) ) {  ?> 
    
      <?php global $current_user;
          get_currentuserinfo();
    
        ?>
    	<h1><?php
    	  echo 'notes: ' . $current_user->notes . "\n";
    	  ?></h1>
    <?php } ?>

    So thats my code, Pretty simple (I just made a page template).

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Bubblechaz

    (@bubblechaz)

    Ive just spotted the Current_user, Its only bring Current_user notes through. I will amend the code and put it here again.

    Apologies

    Thread Starter Bubblechaz

    (@bubblechaz)

    I cant seem to find away of getting the script to use the user_id of the current profile being viewed to collect the Notes column from the database for that user.

    If anyone has any Ideas, I’d really appreciate it thanks

    Moderator bcworkz

    (@bcworkz)

    I’m sorry, but there’s little we can do to help folks using commercial plugins because we don’t have access to the source code. And you publishing the source would run afoul of copyright and terms of use restrictions.

    The WP profile page has numerous actions which can be hooked to output custom content. Each action typically passes the profile’s user ID or object for your callback to use. Perhaps UserPro has similar hooks? Look for calls to do_action() and apply_filters().

    What is the first backend page a user sees on your site? The WP default is the dashboard. If it is the same for your site, you might consider adding a dashboard widget to display messages or hooking into the At A Glance widget. I would think messages would be more apparent on the first page seen rather than the profile page.

    Thread Starter Bubblechaz

    (@bubblechaz)

    Users dont come to the backend with userpro.

    The user logins and goes to the home page. I could make a page called “Your notes” where user notes would be displayed?

    This I would use
    $current_user_id = get_current_user_id(); function
    and then I would also need the need the call the user id from the link?

    I could make a members page for admin, so all users are listed their and the admin would simply select which users Notes they would like to see. So I would have something like this? (I apologise Im still learning wordpress)

    if( current_user_can('editor') || current_user_can('administrator') ) { 
    
    $fetch_members = mysql_query("SELECT * FROM wp_users ORDER BY name");
    
    while($notes_return = mysql_fetch_array($fetch_members))
    {
     if($notes_return[id]){echo("<li><a href=\"membersnotes.php?see_notes=$notes_return[id]\" class=\"top\">$notes_return[name]</a></li>
    ");
     }}

    so the membersnotes.php would be like:

    if( current_user_can('editor') || current_user_can('administrator') ) {
    if($_REQUEST['see_notes'])
    {
    	$pageid = $_REQUEST['see_notes'];
    }
    
    $see_notes=$_GET['see_notes'];
    
    $num = mysql_num_rows(mysql_query("SELECT id FROM wp_users id = $see_notes"));
    
    $fetch_members = mysql_query("SELECT id FROM wp_users id = $see_notes"");
    while($notes_return = mysql_fetch_array($fetch_notes))
    {
    if ($fetch_notes[id])
    
    echo "<center>\n";
    
     if($notes_return[id] == $see_notes){
    
    echo("
      $notes_return[username] </br>
      $notes_return[notes] </br>
      $notes_return[notesby] </br>
    "); }
    }

    and then seeyournotes.php

    <?php global $current_user;
          get_currentuserinfo();
    
        ?>
    
        <h1><?php
          echo 'notes: ' . $current_user->notes . "\n";
          ?></h1>
    <?php } ?>

    would this work?
    can I make these as page templates?

    Thank you. Again my apologies, Im still learning

    Moderator bcworkz

    (@bcworkz)

    No need to apologize, we’re here to help ??
    My apologies for knowing nothing of userpro, thanks for bearing with me.

    What you plan appears to be workable in a general sense, but I think there is room for improvement. If the first page a user sees is the home page, then that is where notification of a message should appear. It could just be an envelope icon the user clicks on that goes to your message page. It is only displayed to logged in users that have a pending message.

    The messages could actually be in usermeta. Doing so could make coding easier, but messages in a custom DB structure will work as well. I urge you to avoid modifying WP tables though. Create your own table if you want to go this route. You’ll need a field to keep track of what messages the user viewed, and probably a date field as well.

    The admin interface presents several possibilities. What makes the most sense depends on the expected nature of the messages. Are they mainly personal messages to one or a few users, or are they broadcast type messages that go to a large number of users, or perhaps even all?

    I would be inclined to try to leverage the existing user listings to add in an administrative messaging facility. Knowing nothing of userpro, I can’t speak to how that may be achieved. It’s certainly achievable with the default user list tables. Building your own user list is certainly an option, but it’s rather redundant and could confuse new editors.

    If you completely develop your own pages to handle this, you can make it be anything you want. For it to be better integrated into the site, you will need to make use of available hooks. The WP core has plenty of hooks, with userpro I have no idea.

    I would first strive for what would be the best user experience. If the lack of available hooks makes that unmanageable, then maybe you fall back to a less integrated custom solution.

    You’re largely own your own on this, especially the userpro portions. If you do get stuck, we’ll try to help you the best we can, but it may be limited.

    One small parting tip. Do not use roles as arguments to current_user_can(). Only supply capabilities as arguments. Yes, roles do work. This is mainly only for reverse compatibility. It’s intended for only capabilities to be used. Using roles defeats the overall intent of having capabilities separated from roles and limits the options available for managing users.

    Thread Starter Bubblechaz

    (@bubblechaz)

    Thank you.

    I am currently looking into hooks and the WP interface for things like current_user_can.

    I am going to base this working with just the WP structure and not the Userpro.

    Do you have any links for updating and editing the database using wp hooks please? If there is a wp hook for it I think it might be better. I have created a row in the wp_user in the database for the notes, and Notes infomation to be stored.

    Thank you

    Moderator bcworkz

    (@bcworkz)

    It’s not actually a hook, you will be mostly using the $wpdb global object to interface with the database and you custom column. At least I think you mean column, not row. Individual records are rows. An individual field of that record when aggregated for all the records or rows of the table constitutes a column. user_login is a column. user_pass is a column. The terms are often confused, and conceptually the model would have worked either way, but by convention we’ve chosen rows for records and columns for aggregated fields. The distinction can be important when choosing the correct $wpdb method.

    While adding a custom column to the user table will work fine, you could run into future issues when a WP update modifies the table. I recommend you store messages in user_meta and use add_user_meta() and get_user_meta() to handle the DB interface. If you dislike user_meta and really want to use a custom column, you should create a completely new custom table to contain your data. A user_id field in the custom table will be adequate to relate your table to the WP users. This is just friendly advice, it’s your site, you’re free to do what you like with it.

    The main reason to use hooks in your case is to cause your custom output to occur as part of the default output. Another reason is to delay the execution of initiating code until the WP environment is completely loaded and stable. The ‘init’ action is usually used for this.

    You mentioned you created a custom page template, that’s fine. There’s likely going to be other code such as your hook callbacks that cannot reside on a template. Consider carefully where you put this code. One option is the theme’s functions.php file. The danger here is when the theme updates your custom code could get overwritten. To prevent this from happening, you should create a child theme to contain your custom code and templates.

    The other option for where to place code is in your own plugin. If part of your project involves page templates, avoid plugins. Templates in plugins are very awkward. Still, you may eventually find yourself creating a site specific plugin as a container for all the bits of custom code most WP hackers end up having.

    You’ve embarked on a learning adventure. Good luck and have fun ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Private Note box on User profiles.’ is closed to new replies.