Wordpress User Role allow acces only to Contact Form 7
Asked Answered
H

4

7

I have a custom user role, called form_editor. I want this user to only be able to edit contact form 7.

THis is what I have so far

$form_editor_role = add_role(
    'form_editor',
    __( 'Form Editor' ),
    array(
        'read'         => true,  // true allows this capability
        'edit_posts'   => true,
        'delete_posts' => false, // Use false to explicitly deny
    )
);

$role = get_role( 'form_editor' );
if(!$role->has_cap('cfdb7_access')){
    $role->add_cap( 'cfdb7_access' );
}

Right not it doesn't have access to posts. It has view access to contact forms, but no edit permissions.

Hellish answered 24/10, 2018 at 10:23 Comment(0)
R
1

try adding publish_pages and add remove_role('form_editor'); to refresh current role

remove_role('form_editor');
add_role('form_editor', __('Form Editor'), array(
    'read' => true, // true allows this capability
    'edit_posts' => true,
    'delete_posts' => false, // Use false to explicitly deny
    'publish_pages' => true
));

$role = get_role('form_editor');
if (!$role->has_cap('cfdb7_access')) {
    role->add_cap('cfdb7_access');
}
Rao answered 19/11, 2018 at 13:31 Comment(0)
C
0

Take a look at this link. The Contact Form 7 uses the built-in user capabilities

wpcf7_edit_contact_form => publish_pages
wpcf7_edit_contact_forms => publish_pages
wpcf7_read_contact_forms => edit_posts
wpcf7_delete_contact_form => publish_pages
wpcf7_manage_integration => manage_options

for editing permission you should give publish_pages capability to your new role like this:

$role = get_role( 'form_editor' );
$role->add_cap( 'publish_pages' );
Convertite answered 21/11, 2018 at 21:23 Comment(1)
This still holds true as of CF7 v5.2.1, publish_pages cap is required for wpcf7_edit_contact_forms to work.Coarsegrained
W
0

The CF7 plugin was written at the initial stages of WordPress, prior to the framework maturing its dashboard integration core code and as such, the plugin author created a number of admin pages that extend admin classes in order to integrate the form editor pages. The WordPress core code evolved to the point where a standard mechanism exists today for plugin integration in the dashboard so as to leverage a lot of the functionality that is already built into the admin interface, while the CF7 plugin code has maintained its legacy code to the point that many existing core functionality does not apply to the CF7 plugin. For example adding custom columns to the form table list.

The CF7 forms are stored as a custom post type wpcf7_contact_form, however, the table list and editor pages are both custom admin pages (as opposed to the edit.php and post.php respectively). Getting to WP core standard functionality to work on the CF7 plugin will always be a challenge. It is this very reason which led me to develop a plugin extension to bring back the CF7 plugin into the WP core standard. I wrote the Smart Grid-Layout design Extension in order to be able to create responsive grid-layout forms by creating a new form editor that integrates a UI designer. As a result, the table list and form editor pages are now WP core pages, which leverage the full functionality of the framework.

Using this extension, fine-tuning user role access can be achieved using the WordPress capabilities functionality by either defining a new role or adding additional capabilities to an existing role. The CF7 capabilities that allow you to control access are,

 'edit_posts' => 'wpcf7_edit_contact_forms'; //controls access to form table
 'edit_others_posts' => 'wpcf7_edit_others_contact_forms'; //controls access to forms created by other users.
 'edit_published_posts'  => 'wpcf7_edit_published_contact_forms'; //ability to edit published forms.
 'delete_posts'  => 'wpcf7_delete_contact_forms'; //delete forms.
 'delete_published_posts'  => 'wpcf7_delete_published_contact_forms'; //delete published forms.
 'delete_others_posts'  => 'wpcf7_delete_others_contact_forms'; //delete forms created by other users.
 'publish_posts'  => 'wpcf7_publish_contact_forms'; //publish forms, else status are set as pending.

so for example, creating a new role for a form editor,

add_action('init', 'create_cf7editor_role');
function create_cf7editor_role(){
  add_role('cf7_editor', 'Form Editor',
    array('wpcf7_edit_contact_forms'=>1,
    'wpcf7_edit_others_contact_forms'=>1,
    'wpcf7_edit_published_contact_forms'=>1,
    'wpcf7_read_contact_forms'=>1,
    'wpcf7_publish_contact_forms'=>1,
    'wpcf7_delete_contact_forms'=>1,
    'wpcf7_delete_published_contact_forms'=>1,
    'wpcf7_delete_others_contact_forms'=>1)
    );
}
Winy answered 15/11, 2019 at 9:30 Comment(0)
A
0

Just see this and had apply a trick . I have disable contact form option for all user expect form_editor. will it be good.

function remove_menu_pages() {

    //global $user_ID;

    
    
     if( is_user_logged_in() ) {
 $user = wp_get_current_user();
 $roles = ( array ) $user->roles;
if($roles[0]!='form_editor')
{
    remove_menu_page('wpcf7');
}
}
    
}
add_action( 'admin_init', 'remove_menu_pages' );
Allemande answered 16/6, 2021 at 16:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.