The access rules for each user on page in wordpress
The client wanted to limit the access to certain pages based on user account and password.
So some pages can only accessed by some users that gave them permission the site manager.
For that i create two radio input type with “Yes” or “No” protect page options.When i selected
“Yes” option i can chosen from a list who user has access on page or not.If
i selected “No” option all users have access on page.
I’ve done this in the following code:
echo '<form action="" method="post">'; echo '<input type="radio" name="protect" '.($protejata == 'Yes' ? ' checked' : '').' value="Yes"> <samp>Yes</samp> <input type="radio" name="protect" '.($protejata == 'No' ? ' checked' : '').' value="No"><samp>No</samp>'; echo '</form>'; echo '<div>'; foreach($wpdb->get_results('SELECT ID,user_login FROM cga_users') as $sol_users) { echo '<input type="checkbox" '.(in_array($sol_users->ID, $userii) ? ' checked' : '').' name="sol_users[]" id="sol_user_'.$sol_users->ID.'" value="'.$sol_users->ID.'"><label for="sol_user_'.$sol_users->ID.'">'.$sol_users->user_login.'</label>'; } echo '</div>';
I use jquery from hidden list with users when It’s choen “No” protect page option
jQuery(function() { jQuery('input[name="protect"]').change(function() { if(jQuery(this).val() == 'Yes') { jQuery('#xxx').show(); } else { jQuery('#xxx').hide(); } }); });
These settings are saved in data base with update_post_meta() function.
update_post_meta($post_id, 'pagina_protejata', $_POST['protect']); $users = count($_POST['sol_users']) ? $_POST['sol_users'] : array(); update_post_meta($post_id, 'pagina_protejata_users', serialize($users));
In page.php file i tested who user is logged with is_user_logged_in() function.


Tags: 



Leave a Reply