Disable visual editor based on role? - wordpress

I want to disable visual editor form 'author' role form my wordpress site.
Author only see text editor when write a post.

You can use the user_can_richedit filter to tell WP to disable rich editor for author only through this code (put this inside your functions.php file):
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
add_filter( 'user_can_richedit' , '__return_false', 50 );
}

I make following function it's work.
function cp_disable_editor()
{
if( current_user_can( 'author' ) )
{
$user_ID = get_current_user_id();
update_user_meta( $user_ID, 'rich_editing', false );
}
}
add_action('admin_init','cp_disable_editor');

Related

how to determine if current user is admin in wordpress gutenberg UI

I have found .canUser() method. But it returns undefined for me.
I'm calling it in UI with:
wp.data.select( 'core' ).canUser('create', 'users')
Where am I wrong?
Here are the docs for reference:
enter link description here
Is there any known plausible way to do it in JS UI plugin code itself? Or how did you do it maybe?
You need to use useSelect in order to achieve this:
wp.data.useSelect( (select) => select( 'core' ).canUser( 'create', 'users' ) );
After Analyzing Gutenberg source for a bit found out I was semi right.
The way to check this is around users (only admin can do that).
Here is how one could do it in Gutenberg UI Block code.
wp.data.select( 'core' ).canUser( 'create', 'users' )
THe only problem it did not work while the breakpoint was set.
You can use current_user_can() to check the user roles.
https://developer.wordpress.org/reference/functions/current_user_can/
function current_user_can( $capability ) {
$current_user = wp_get_current_user();
if ( empty( $current_user ) ) {
return false;
}
$args = array_slice( func_get_args(), 1 );
$args = array_merge( array( $capability ), $args );
return call_user_func_array( array( $current_user, 'has_cap' ), $args );
}
To check if the user is editor or administrator:
<?php if( current_user_can('editor') || current_user_can('administrator') ) { ?>
// Stuff here for administrators or editors
<?php } ?>

Wordpress - Make it so that non-admin users can't see posts with a specific custom post status

What hooks would I use for the functions file in order to make it so that all non-admin users can't see all posts with a specific custom post_status in the wp-admin back-end. BUT it is still able to be queried and looped through the WordPress post loop?
With pre_get_posts you should be able to get started (to hide posts from the admin screen). You may also want to check the post type, etc.
function filter_posts( $wp_query ) {
if ( is_admin() ) {
$user = wp_get_current_user();
$post_status = 'draft';
if ( ! in_array( 'administrator', $user->roles ) ) {
$wp_query->set( 'post_status', $post_status );
}
}
}
add_action( 'pre_get_posts', 'filter_posts', 10 );
To disallow users to edit posts with that specific status, you should do:
function restrict_post_editing(){
global $post;
$post_status = 'draft';
if ( get_post_status( $post ) == $post_status ) {
$user = wp_get_current_user();
if ( ! in_array( 'administrator', $user->roles ) ) {
do_action('admin_page_access_denied');
wp_die( __('You cannot modify or delete this entry.') );
exit;
}
}
}
add_action('edit_post', 'restrict_post_editing', 10, 1);
add_action('wp_trash_post', 'restrict_post_editing', 10, 1);
add_action('before_delete_post', 'restrict_post_editing', 10, 1);

I want to create two new user type in WordPress

In WordPress, I want to create two register user type: 1.teacher and 2. student.. means register as teacher and register as a student.
teacher registration is free and it posts his video, text etc..
student registration is not free but it's 6-month and 12-month subscription when a student is subscribed for this then student show the teacher's all post.
Can you please suggest me in this how to create this type of registration and membership...
thanks in advance
I think you can do it using add_role function.
There are three parameters in add_role function.
add_role( $role, $display_name, $capabilities );
$role: Unique name of the role.
$display_name: The name to be displayed in WordPress Admin Panel.
$capabilities: Privileges that one can access.
Complete list of all capabilities can be found here.
Step 2 : add user roles dropdown in registration form
//1. Add a new form element...
add_action( 'register_form', 'myplugin_register_form' );
function myplugin_register_form() {
global $wp_roles;
echo '<select name="role" class="input">';
foreach ( $wp_roles->roles as $key=>$value ) {
// Exclude default roles such as administrator etc. Add your own
if ( ! in_array( $value['name'], [ 'Administrator', 'Contributor', ] ) {
echo '<option value="'.$key.'">'.$value['name'].'</option>';
}
}
echo '</select>';
}
//2. Add validation.
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( empty( $_POST['role'] ) || ! empty( $_POST['role'] ) && trim( $_POST['role'] ) == '' ) {
$errors->add( 'role_error', __( '<strong>ERROR</strong>: You must include a role.', 'mydomain' ) );
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action( 'user_register', 'myplugin_user_register' );
function myplugin_user_register( $user_id ) {
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
}
Finally How to check if a user is in a specific role ?
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
//The user has the "author" role
}

WordPress SEO plugin only visible to specific user

I am using the Yoast SEO plugin in WordPress and wanted to know if there was a way to make it only visible to one specific user in the db or in the functions.php file? Not a role, an actual user.
I tried an universal solution to simply add "plugin-name" and disable it, but failed.
But, to show WPSEO only to a specific user (ID equals 2), the following works:
add_action( 'plugins_loaded', 'seo_so_25654837' );
function seo_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
remove_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
}
Don't add the code to functions.php, use it as a normal plugin.
The following is also needed to remove the SEO menu from the admin bar:
add_action( 'wp_before_admin_bar_render', 'bar_so_25654837' );
function bar_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
global $wp_admin_bar;
$nodes = $wp_admin_bar->get_nodes();
foreach( $nodes as $node )
{
if( !$node->parent )
{
if( 'wpseo-menu' === $node->id )
$wp_admin_bar->remove_menu( $node->id );
}
}
}
You can hook to pre_current_active_plugins to remove elements from the table before it is displayed. Using get_current_user_id() within the function will let you selectively hide a plugin.
function hide_plugins_by_user( $all_plugins=false ) {
global $wp_list_table;
// if the current user ID is not 1, hide it.
if ( 1 != get_current_user_id() ){
// the active plugins from the table
$plugins = $wp_list_table->items;
// loop through them
foreach ( $plugins as $key => $val ) {
// use the dir + filename of the plugin to hide
if ( $key == 'plugindir/plugin.php' ) {
unset( $wp_list_table->items[$key] );
}
}
}
}
add_action( 'pre_current_active_plugins', 'hide_plugins_by_user' );
This code is working fine (Credits goes to Hislop):
// Returns true if user has specific role
function check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
// Disable WordPress SEO meta box for all roles other than administrator and seo
function wpse_init(){
if( !(check_user_role('seo') || check_user_role('administrator')) ){
// Remove page analysis columns from post lists, also SEO status on post editor
add_filter('wpseo_use_page_analysis', '__return_false');
// Remove Yoast meta boxes
add_action('add_meta_boxes', 'disable_seo_metabox', 100000);
}
}
add_action('init', 'wpse_init');
function disable_seo_metabox(){
remove_meta_box('wpseo_meta', 'post', 'normal');
remove_meta_box('wpseo_meta', 'page', 'normal');
}
Just place it in the functions.php file.
To disable the Yoast for all the users and enable it for just for few or specific, just add the following piece of code to your function.php file.
function remove_wpseo(){
/* if you want to keep it enabled for user with id 2 */
if ( '2' == get_current_user_id() ) {
return;
}
global $wpseo_front;
if(defined($wpseo_front)){
remove_action('wp_head',array($wpseo_front,'head'),1);
}
else {
$wp_thing = WPSEO_Frontend::get_instance();
remove_action('wp_head',array($wp_thing,'head'),1);
}
}
add_action('template_redirect','remove_wpseo');
Reference: https://makersbyte.com/disable-yoast-seo-plugin-specific-page/

making your posts password-protected by default

I originally wanted to be able to password protect a category. At the very least I wanted it to be password-protected, but preferably a username and password login. Since I have been unsuccessful for days at finding a solution I have now resorted to using WordPress's built-in password protection for posts.
The issue I am having is I will be posting via e-mail and in order to have these posts password-protected I need to login to Wordpress and then manually select password-protected and enter a password in the dashboard.
I would like to be able to have all posts that appear in a specific category be password-protected with the same password by default. Eliminating having to log in to Wordpress and manually select password protect.
I know there is a function <?php post_password_required( $post ); ?> that I need to use but I am not sure how to implement it or where.
Based on this WordPress StackExchange answer. Tested only with a regular dashboard. Publishing via email has to be tested, but I suppose the hook gets called in this kind of posting.
add_action( 'save_post', 'wpse51363_save_post' );
function wpse51363_save_post( $post_id ) {
//Check it's not an auto save routine
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
//Check it's not an auto save routine
if ( wp_is_post_revision( $post_id ) )
return;
//Perform permission checks! For example:
if ( !current_user_can( 'edit_post', $post_id ) )
return;
$term_list = wp_get_post_terms(
$post_id,
'category',
array( 'fields' => 'slugs' )
);
if( in_array ( 'the-category-slug', $term_list ) )
{
// Unhook this function so it doesn't loop infinitely
remove_action( 'save_post', 'wpse51363_save_post' );
// Call wp_update_post update, which calls save_post again.
wp_update_post( array(
'ID' => $post_id,
'post_password' => 'default-password' )
);
// Re-hook this function
add_action( 'save_post', 'wpse51363_save_post' );
}
}
add_filter( 'wp_insert_post_data', function( $data, $postarr ){
if ( 'book' == $data['post_type'] && 'auto-draft' == $data['post_status'] ) {
$data['post_password'] = wp_generate_password();
}
return $data;
}, '99', 2 );

Resources