Check if user agreed to terms , set cookie - drupal

I'm a Drupal nub. I would like to check on every page if user (anonymouse) agreed to somekind of terms. I suppose i should write small custom module ?
Where will this condition be written
if(!$_COOKIE('confirm')){
//jQuery show confirmation form
//Set cookie for 1hour
}
maybee in page.tpl.php ? Please, give me some tips ..

If you don't want to store the info for a long time, you should use $_SESSION variable. Then in preprocess page you could check if the user has accepted and set a variable that you can use in your page.tpl.php.

user_save() accepts an array argument which you can put custom data into. This will then be loaded with your $user object and you can use in any template file.

Check out these modules:
http://drupal.org/project/legal
http://drupal.org/project/terms_of_use

The Legal and TOS modules are good if you need a login. If working with anonymous users, however, you'll need to use the rules module with https://www.drupal.org/project/rules_session_vars.

Related

User access to CPT (Custom Post Type) without access to regular posts

I have created a custom post type that for the sake of this question we can call "my_cpt".
I have also created a new role which we can call "my_role".
What I want to do is give "my_role" access to "my_cpt" without adding the "edit_posts" capability because that gives access to other post types which I do not want the role to have access to.
I have tried variations of numerous bits of code I have found but none have worked so I don't really know where to begin. Based on that I don't have any base code to display here.
A plugin like this one should enable you to do this: https://www.role-editor.com/
Ended up being easier than I thought.
I created a couple capabilities... "read_my_cpt" and "edit_my_cpt".
The "my_role" has permissions to both the capabilities.
I then just had to change the capability_type for the CPT to "my_cpt" instead of "post"

How to skip a function in Drupal?

I need to skip mollom verification if the users on a site have more then X points. Does anyone knows if it is possible to right a module that skips mollom_form_alter ? Thanks !
You can't skip the mollom_form_alter call, to do that you would need to alter the mollom module itself, What you can do, howerver, is to alter the form after mollom, undoing whatever you like.
You can't skip a form alter call, however you can create your own form alter that "triggers" mollom based on the number of points that the user has.

Drupal using views with CCK custom fields

I've got a Drupal site which uses a custom field for a certain type of node (person_id) which corresponds to a particular user. I want to create a view so that when logged in, a user can see a list of nodes 'tagged' with their person_id. I've got the view working fine, with a url of my-library/username but replacing username with a different username shows a list of all nodes tagged with that user. What I want to do is stop users changing the URL and seeing other users' tagged nodes. How can I do this? Is there somewhere where I can dictate that the only valid argument for this page is the one that corresponds with the current logged in user's username?
person_id = uid?
In this case, add argument with user:uid, then in Validation options select PHP Code, read comment of this field carefully:
Enter PHP code that returns TRUE or
FALSE. No return is the same as FALSE,
so be SURE to return something if you
do not want to declare the argument
invalid. Do not use . The
argument to validate will be
"$argument" and the view will be
"$view". You may change the argument
by setting "$handler->argument".
Add this code:
global $user;
$account = user_load('name'=>arg(1));
$handler->argument = $user->uid;
return $account->uid == $user->uid;
I'm not sure how you have setup your view, which gives some different options to solve this. A way that should work would be to set the default argument be the logged users id/username and remove the argument from the url.
Alternatively you could create your own filter which requires some work with the views API, but gives more control.

Wordpress session management

I'm putting up a site using Wordpress and I'd like to piggyback on its sessions. But I'm not finding any plugins, or even documentation. Any suggestions or references before I start hacking it?
Note: I'm asking about if and how WP uses standard PHP sessions itself, not how to add PHP sessions e.g. using session_start(). Apparently any state WP maintains is accomplished by other means. So if I want to use PHP sessions I need to add and maintain it myself entirely, using techniques like those in the thread.
Thanks all!
It's a very bad idea to modify WP Core files for the ability to use sessions. The best way I've found is to call the session_start() from init action hook.
function kana_init_session() {
session_start();
}
add_action('init', 'kana_init_session', 1);
You can place it in functions.php file of your theme.
Detailed article can be found here: http://www.kanasolution.com/2011/01/session-variable-in-wordpress/
WordPress doesn't appear to call session_start() because it wants to be stateless
and if register_globals is defined, it automatically destroys your $_SESSION
Consider using WordPress Transient API
Values stored using the Transient API are visible to all users, not just the current user, depending on the unique identifier used to retrieve the transient, you could assign each user a unique identifier essentially causing a transient to behave very much like a session.
Further considerations:
Depending on a users setup with object cache, etc., transients may
not always be stored in the DB (e.g. memcached), using transients for
sessions could mean that the data can get bulky and fill memory
quickly (in the use of memcached).
Also, it seems that WP does not do auto garbage collection for
transients:
https://wordpress.stackexchange.com/questions/6602/are-transients-garbage-collected
For what I need to do, the best answer involves:
To allow the cookie for wordpress to persist across subdomains, install the Root Cookie plugin.
sub1.domain.com has wordpress; sub2.domain.com is another site. From the other site (sub2), I read the cookies to identify who the user is and if the user is logged in.
My cookies are as follows:
[wordpress_909bb230b32f5f0473202684d863b2e0] => mshaffer|1255298821|d0249fced9c323835c5bf7e84ad3ffea
[wordpress_logged_in_909bb230b32f5f0473202684d863b2e0] => mshaffer|1255298821|56e9c19541ecb596a1fa0995da935700
Using PHP, I can loop over the cookies, parse the key=>value pairs. These cookies let me know that [mshaffer] has a cookie stored on wordpress, and also is authenticated as logged_in. The expiry of the cookie is 1255298821.
In sub2, I can query the database of wordpress and grab the user info:
SELECT * FROM `wp_users` WHERE user_login = 'mshaffer' ... grab user_id, user_email from this query
SELECT * FROM `wp_usermeta` WHERE user_id = '$user_id' ... grab lots of other data from wp
With this info, I can add to my sub2 session variable / cookie and do what I want with the data. I can identify if I am logged in, and my username ... which let's me grab lots of different data. I can now use WordPress authentication in my sub2.domain.com and redirect accordingly.
monte
{x:
Wordpress doesn't seem to use any sessions.
The best way to go about it is to use the action hooks it provides.
Have you checked the solution here this may work for here and its on easy way
http://thedigilife.com/wordpress-how-to-set-session-custom-variable-while-login/
Hooking a function with session_start() on wp_loaded seems to work in this case.
Put this code in wp-config.php at first line:
if (!session_id()) {
session_start();
}
Put this code in theme's header.php at first line:
session_start();
Then it will maintain all session variables.
If you wanna use your own session values, Wordpress does support it.
You need to add following lines at the top of wp-config.php
if (!session_id()) {
session_start();
}
Then add following line at the top of header.php
session_start();

change user_profile_form form fields order

When a user login , the user will be redirect to a user profile page, which has a My account field set.
the field set has 2 fields, "Username: ", "Email address:". those 2 fields are generated by drupal.
those 2 field contained in a form which has a id ("user_profile_form") . I want to change the order of those 2 fields.
I have tried to intercept 'user_profile_form' , inside hook_form_alter.
code as follow:
$form['account']['name']['#weight'] = 1;
but that did not success, drupal did not even rendering the 'name' field, so no username: showed on browser.
What you did is absolutely correct, and probably did work. You can change the weight of the fields with the method described above.
The username field is not always rendered. The reason is that a persmission is required: change own username. If that perm is not set, you wont be allowed to alter you username and the field wont be shown.
Info on debugging.
Your info alone is not quite enough to debug. From what you describe, you are doing the right thing, but other modules could be making things a bit tricky for you. The devel module is quite good when it comes to debugging, ti defines two functions I use a lot when debugging:
dpm() pretty prints the variable to the message area using krumo.
dd() Prints / saves a variable to a log file. Useful when you can't view messages on the screen.
I would suggest that you look at the $form variable before and after you alter it.
Things that could make it go wrong:
Did you remember to pass the $form variable by reference using the & notation?
Is another module altering your form after you?
Are you checking for the correct form id, so you alter the correct form?
These are some pointers, before you bring more info, all I can do is guess to what your problem exactly can be. I did something like this a few days ago so I know what you describe shouldn't be a problem.

Resources