display html data depending on wordpress user id - php

im working in wordpress. I have 3 users in it, and inside my wordpress dashboard login i have a customized html widget with custom data. This widget has 3 divs
If any of these users log in, can see the 3 divs. But what i want to do is display a specific div for a specific user.
Lets say i have:
<div class=“user1”></div>
<div class=“user2”></div>
<div class=“user3”></div>
and i want to “user_id=3” only see the next:
<div class=“user3”></div>
and if user 2 log in, can see the div with class “user2” and hide the others
in wordpress, each user name as for ex: user_id=3
Can anyone who understand php and wordpress help me with this?
Thanks!

You should use PHP to identify the user and then show the appropriate content. This way the page only contains the data the user should see. If you have multiple <div> elements then you have to include them all every time. This creates data leaks and security vulnerabilities.
The Problem
Consider this:
Your page has these div elements like you suggested
<div class="user1">User1 Information</div>
<div class="user2">User2 Information</div>
<div class="user3">User3 Information</div>
When user1 logs in, you only show the div with class='user1'. The problem is, that you still include in the page the information for user2 and user3. That's a bad practice, because a skilled user can easily obtain that information by viewing the page's source code.
The Right Way
You should use the PHP script to identify the user and then print the widget that you want them to see. The code below is a generalized Wordpress script that you will need to adapt to your need.
$user = wp_get_current_user();
$user_id = $user->ID;
switch($user_id) {
case 1: //user1 id
//show user1 widget
break;
case 2: //user2 id
//show user2 widget
break;
case 3: //user3 id
//show user3 widget
break;
}
Here, you only show the widget that the user should see. This is not the only way to accomplish this and your need with dictate your script. You can use if statements or use the switch statement or put all the code in a function to contain it all. The solution is up to you, but I hope this gets you pointed in the right direction.

Related

Linking to a text field

I am trying to get people that are not logged in to log in by linking them to the log in fields in the sidebar.
So far I managed to accomplish this:
<?php comment_form(array(must_log_in => sprintf(__('You must be logged in to comment.')))); ?>
So I am using the Login with AJAX widget, or I could just place the plugin's template tag in there which is <?php login_with_ajax() ?>. Right before the widget I have placed <a name="reg"></a>, so when they click the link to log in they get to where the log in form is, but they are not placed in the username field. Is there a way (I doubt it) where I can place the focus in the username field? That is to have a blinking cursor in there?
"It is not possible" is also an acceptable answer, so I can move onto my next problem.
Assuming something like <input type="text" id="username">, you could use
document.getElementById("username").focus();
in JavaScript.

Sliding Form Content

So I have a 3 page (Page1.php, Page2.php, Page3.php) registration page that is all completed after over 20hrs of coding. But now I'm working on design. My idea is to put all the forms on one page rather than three different pages (Page1.php, Page2.php, Page3.php).
Here's what the front page looks like:
So when the user clicks the register link I want the form to appear in this region:
I have all the code for the forms so it's just a simple matter of copy pasting. But what I need to do is implement some sort of slider for the form. So the user clicks register, the form appears on the right, they fill in the first page, then at the bottom right they click continue, and it SLIDES to the next page. Note, when they click continue i don't want to go to a whole new page, I want to stay on the same page, I just want to display different content.
I know how to do all the onclick (show/hide) stuff in jquery, but I'm wondering if there's a good plugin I can use to provide a much smoother transition and better expereience. Or if there's a better way to do it (Pure CSS or something)
Just set all content to .hide() and .show() the div which has a unique identifier.
So you have:
Page 1
Page 2
Page 3
<div class="tab" data-tab="page1">Content for page 1</div>
<div class="tab" data-tab="page1">Content for page 1</div>
<div class="tab" data-tab="page1">Content for page 1</div>
With the jquery code:
$('a.tab').click(function(){
$('div.tab').hide();
$('div.tab[data-tab=' + $(this).attr('data-tab') + ']').show();
});
Well, that's basically it. You can do the rest, but this should give you a start in the right direction :)
You can make it as complex as you would like. I have some coding who does this with a TabHelper in CakePhp, and with a little bit of javascript I built the same. Because I don't like the jQueryUi tab plugin (hint)
Also, have a look at Php Frameworks, you said you spent 20 hours coding for you registration and login.. Well, I did the same with CakePhp, but now my site is finished and is fully maintainable and sends emails on the right time and has a lot of functionality.

Show different content in sidebar based on user behaviour

I'm planning a site and I was thinking would it be possible to show alternate content in the sidebar based on the behaviour of the visitor? Say the user has clicked on link X on page A and I want the alternate content in the sidebar to be shown on page B, if they clicked on the link. Also, if the visitor fills out a form, would it be possible to associate the tracking cookie information with the submitted form to see what pages the visitor viewed? Would this be easier to implement in a particular cms?
I would appreciate it if someone could at least point me into the right direction. Thanks in advance!
Yes anything is possible & no need for cookies (unless you want to keep a persistent track of the user)
Have a relationship column next to the content in your db, when creating the content assign this value much like a category or tag ect.
Then when user clicks on link A as the page loads store its relationship in the session, then when link B is clicked load content related to the previous set session value.

Restrict Joomla article to one view per user

Does anyone know how I can manipulate joomla to only allow one view per user to certain articles? I am looking to make the article gray out and not allow the user to click on it after it has been viewed for the first time. Any help would be much appreciated.
Thank you,
Dave
This is would probably have to be a plugin. This could only really be reliably done on a page that requires a user to be logged in and you can't just register new accounts. Also someone could just copy the text the first time and then your security is moot. Nevertheless, the plugin would have to track the page ID and the user ID and when the visit the page a row gets added to table. Then you could have a page template where you check that table and if the row exists for that page id and user you do whatever to the content otherwise display it as normal.
I'm not sure why you would want to do this, but it would take a plugin that does several things. First, your content would have to be available to registered users only. Cookies would work, but it would be really easy to clear your cookies to regain access to the content. Next, the plugin would need to record every visit to the page, the record would need to include the userID. Last, the plugin would need to look up the specific user that is trying to access an article and check to see if they have been to the page before.
However, all of that work would not prevent anyone from registering another account to access the content again. Or copying and pasting the content or saving the page locally. Why would you want to limit your users to a single page view?
The only correct way to do is create a plugin "content" that will check the database in the field "hits". If it is greater than X, you can redirect Joomla to block his access:
$mainframe->redirect('index.php', JText::_('AN ERROR HAS OCCURED'));
The advantage of this method is that you have the option to reset the counter in the administration of articles
As for the display of the results list, I advise you to use your html file of your template to make such audit and carried away those who are no longer available.

Best method for generating html content to a single web page

Good day everyone!
I have a single page, this page has no dynamic content.
I have a set of links. What I need is to have content placed on the page based on the link the user clicks.
Example: I have pictures of animals. When the user click a Zebra, the Zebra info is placed on the page.
So the page does not change, meaning the content on the page changes but not the page it self.
The idea is instead of creating different html pages for each animal profile, the profile is generated to the page from another source.
Is is this possible?
What will be the best way to get this accomplish?
Thanks everyone!
IC
I would probably use jquery to do this. Set your data in hidden divs and just move them into the "display" area when the appropriate image is clicked on.

Categories