mardi 10 mai 2016

WordPress User role based on plugin action

I'm developing a WordPress plugin. One of the feature is Subscribers can add data. I can use "manage_options" within the plugin however, BUT, as a subscriber, anybody can change the general settings etc. So, I have created a custom role and assigned it to the subscriber (manage_schools) but the page shows that "You do not have sufficient permissions to access this page.". Below is the code for the plugin.

// get the the role object
$subscriber = get_role('subscriber');
// add $cap capability to this role object
$subscriber->add_cap('manage_schools');

function sinetiks_schools_show_subs(){

add_menu_page('Show Schools', 'Show Schools', 'read',    'sinetiks_schools_show', 'sinetiks_schools_show');

//this is a submenu
add_submenu_page('sinetiks_schools_list', //parent slug
'Add New School', //page title
'Add New', //menu title
'manage_schools', //capability
'sinetiks_schools_create', //menu slug
sinetiks_schools_create); //function
}

//add_action('Schools', 'sinetiks_schools_show');

function sinetiks_schools_show(){ 
global $wpdb;
$rows = $wpdb->get_results("SELECT id,name from school");
$txt.= "<table class='wp-list-table widefat fixed'>";
$txt.= "<tr><th>ID</th><th>Name</th><th>&nbsp;</th></tr>";
foreach ($rows as $row ){
$txt.= "<tr>";
$txt.= "<td>$row->id</td>";
$txt.= "<td>$row->name</td>";   
$txt.= "</tr>";}
$txt.= "</table>";
echo $txt;
}

Please help in this regard!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire