Allow contributors to upload images but not delete them - wordpress

I enabled image uploads for contributors on my Wordpress site via the following code:
if ( current_user_can('contributor') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}
It works great, but I also need to disallow them to delete the images they uploaded. Is there an easy way to do it? I know there's User Role Editor plugin, but I don't want to install it just for this.

You can use delete_attachment hook or pre_delete_attachment filter for that:
add_action('delete_attachment', 'wp66410923_prevent_delete_attachment', 11, 1);
function wp66410923_prevent_delete_attachment($postID)
{
if (current_user_can('contributor')) {
echo __('You can not delete attachments.');
die;
}
}
add_filter( 'pre_delete_attachment', 'wp66410923_filter_attachment_deletion', 10, 3 );
function wp66410923_filter_attachment_deletion( $delete, $post, $force_delete ){
if (current_user_can('contributor')) {
return false;
}
return true;
}

Related

Override wordpress routing / url rewriting?

I would like to serve different content based on the URL.
I started with a custom page setup via a custom template but I am open to something else.
The main goal is to have one PHP page that can serve different contents programmatically based on the URL.
For example:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc...
I have been looking at add_rewrite_rule, add_rewrite_tag, flush_rewrite_rules and WP_Rewrite API but I am getting confused about which one I should use?
I found an example here but I could not make it work, I get 404s, any idea why?:
/*
Plugin Name: Products Plugin
Plugin URI: http://clivern.com/
Description: Register URL rules for our products
Version: 1.0
Author: Clivern
Author URI: http://clivern.com
License: MIT
*/
function products_plugin_activate() {
products_plugin_rules();
flush_rewrite_rules();
}
function products_plugin_deactivate() {
flush_rewrite_rules();
}
function products_plugin_rules() {
add_rewrite_rule('products/?([^/]*)', 'index.php?pagename=products&product_id=$matches[1]', 'top');
}
function products_plugin_query_vars($vars) {
$vars[] = 'product_id';
return $vars;
}
function products_plugin_display() {
$products_page = get_query_var('pagename');
$product_id = get_query_var('product_id');
if ('products' == $products_page && '' == $product_id):
//show all products
exit;
elseif ('products' == $products_page && '' != $product_id):
//show product page
exit;
endif;
}
//register activation function
register_activation_hook(__FILE__, 'products_plugin_activate');
//register deactivation function
register_deactivation_hook(__FILE__, 'products_plugin_deactivate');
//add rewrite rules in case another plugin flushes rules
add_action('init', 'products_plugin_rules');
//add plugin query vars (product_id) to wordpress
add_filter('query_vars', 'products_plugin_query_vars');
//register plugin custom pages display
add_filter('template_redirect', 'products_plugin_display');
First of all, make sure your pretty permalinks are enabled, in this case the option "Plain" in Settings - Permalinks should be unselected:
Select one of these options to enable pretty permalinks
You can check whether pretty permalinks are enabled in the code like so:
function is_enabled_pretty_permalinks() {
return !empty( get_option( 'permalink_structure' ) );
}
if ( is_enabled_pretty_permalinks() ) {
echo 'Pretty permalinks enabled';
}
Next add a new rewrite rule:
function add_my_rewrite_rule() {
$page_slug = 'products'; // slug of the page you want to be shown to
$param = 'do'; // param name you want to handle on the page
add_rewrite_rule('my-plugin/?([^/]*)', 'index.php?pagename=' . $page_slug . '&' . $param . '=$matches[1]', 'top');
}
add_action('init', 'add_my_rewrite_rule');
Add your parameter to query vars so you will be able to handle it on the page:
function add_my_query_vars($vars) {
$vars[] = 'do'; // param name you want to handle on the page
return $vars;
}
add_filter('query_vars', 'add_my_query_vars');
Then you can access your query var do in the page template or in a shortcode, for example:
function my_plugin_shortcode_handler( $atts ){
$do = get_query_var( 'do' );
if ( $do === 'this' ) {
return 'do this';
} else {
return 'do that';
}
}
add_shortcode( 'myshortcode', 'my_plugin_shortcode_handler' );
Then place the shortcode to the content via Gutenberg.
Check out the links:
https://some-url.com/my-plugin/this/ - outputs "do this"
https://some-url.com/my-plugin/that/ - outputs "do that".
This can be solved by using query params. Like you mentioned you have set up custom page via a custom template. You can read the URL and check for the parameters and based on that you can send data from the PHP template page.
e.g,
function cleanTheInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = htmlentities($data);
return $data;
}
if (isset($_GET['page_url'])) {
$theUrl = cleanTheInput($_GET['page_url']);
}
if($theUrl == 266)){
// data for https://some-url.com/?page_url=266
}
if($theUrl == 366)){
// data for https://some-url.com/?page_url=366
}

Hook to run after removing a user

I have a function where I duplicate a user to all subsite when they registered.
I achieved that by doing this:
function sync_user( $user_id )
{
$list_ids = get_sites();
$current_site = get_current_site();
$info = get_userdata($user_id);
foreach( $list_ids as $list )
{
if ( $list->blog_id != $current_site->id )
{
add_user_to_blog($list->id, $info->ID, 'subscriber');
}
}
// quick fix for: above somehow doesn't add to main site. add to main site here.
add_user_to_blog(1, $info->ID, 'subscriber');
}
Now, I want to "unsyc" the user when I removed the user from the site. I tried to hook it by using 'remove_user_from_blog', but it caused infinite loop.
Where can I hook the following code so that I can remove all those users that I added previously using above code?
function unsync_user( $user_id )
{
$list_ids = get_sites();
foreach( $list_ids as $list )
{
remove_user_from_blog( $user_id, $list->ID );
}
}
edited the title for clarity
AbdulRahman was correct about that. When user click 'remove' from the user list, the action not fire 'delete_user' or 'deleted_user' hook. I tested it.
I think it is tricky. So, here is how to add custom removed_user action. Add these lines below into your plugin.
add_action('remove_user_from_blog', function($user_id, $blog_id) {
// checking current action
// refer: wp-admin/users.php:99
$wp_list_table = _get_list_table( 'WP_Users_List_Table' );
if( $wp_list_table->current_action() != 'doremove' ) {
return; // only proceed for specific user list action
}
$fire_removed_user_hook = null; // closure reference
$fire_removed_user_hook = function() use ($user_id, $blog_id, &$fire_removed_user_hook) {
do_action( 'removed_user', $user_id, $blog_id );
// remove the hook back
remove_action('switch_blog', $fire_removed_user_hook);
};
// restore_current_blog called at the last line in the remove_user_from_blog function
// so action switch_blog fired
add_action('switch_blog', $fire_removed_user_hook);
}, 10, 2);
add_action('removed_user', function($user_id, $blog_id) {
// the user removed from be blog at this point
}, 10, 2);
The hook "deleted_user" runs after a user is deleted ("delete_user" runs before the deletion occurs):
https://codex.wordpress.org/Plugin_API/Action_Reference/deleted_user

TinyMCE - Custom plugin makes visual editor blank in wordpress

I am trying to add TinyMCE custom plugin in wordpress that change the direction of selected text by using <bdo>. I register the plugin in wordpress:
/*Register Custom TinyMCE plugin*/
add_filter('mce_external_plugins', 'my_tinymce_plugins');
function my_tinymce_plugins() {
$plugins_array = array(
'tiny' => 'tiny.js' //Plugin directory is same as theme's funtion.php
);
return $plugins_array;
}
But it hide visual editor completely & also make text editor, un-editable. What is wrong?
I think you're overwriting all the other plugins, rather than just adding yours. Try
function my_tinymce_plugins($plugin_array) {
$plugin_array['tiny'] = 'tiny.js';
return $plugin_array;
}
instead. You may need to prepend get_stylesheet_directory_uri() to tiny.js to ensure the URL is correct.
Edit
Further to your comment, here's some code I used a few years ago to add a button. I can't say for certain best practices haven't changed, but it worked for me:
add_action('init', 'immo_add_column_button');
function immo_add_column_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
return;
}
if ( get_user_option('rich_editing') == 'true') {
add_filter('mce_external_plugins', 'immo_add_column_tinymce_plugin');
add_filter('mce_buttons', 'immo_register_addcolumn_button');
}
}
function immo_register_addcolumn_button($buttons) {
array_push($buttons, "|", "addcol");
return $buttons;
}
function immo_add_column_tinymce_plugin($plugin_array) {
$plugin_array['addcol'] = get_bloginfo('stylesheet_directory').'/js/immo_column_button.js';
return $plugin_array;
}
add_filter( 'tiny_mce_version', 'immo_refresh_mce');
function immo_refresh_mce($ver) {
// Force refresh of TinyMCE cache by updating the version number
$ver += 3;
return $ver;
}

How to show text in a page on Wordpress whithin a plugin

I am developing a plugin for a website.(Which is my first plugin for Wordpress)
The basic functionality is querying the database and in specific pages show the data from the database with a specific style instead of the content from the pages.
So far I managed to show some text in every specific page.
This is my code after some basic configurations:
global $wpdb;
global $wp_query;
add_action( 'wp', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I've read this post, but I couldn't solve my problem.
WordPress replace content of a page (plugin)
I already read the Wordpress documentation but I havent find anything there.
I found myself a solution, using shortcodes.
global $wpdb;
global $wp_query;
add_shortcode( 'sector_page_display', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function medicalPage()
{
return print "Medical";
}
See that instead of add_action I changed to add_shortcode
Then on everypage I will use to show info from the database I add
[sector_page_display]
in the page, so it call my method. You can add variables in there if you want.
You'll want to run that code before WordPress has fully loaded.
Try this
global $wpdb;
global $wp_query;
add_action( 'init', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I changed the add_action to now run the code when WordPress is being initialized.

How to hide media uploads by other users in the Media manager

i am using wordpress multisite and wan to hide medea which others have uploaded. Like if X User of that site have uploaded any media in the wordpress, Y User should not be able to see or access this from there login. Please help
You could try something like this.
/**
* Allow access to own content only
*/
function my_authored_content($query) {
//get current user info to see if they are allowed to access ANY posts and pages
$current_user = wp_get_current_user();
// set current user to $is_user
$is_user = $current_user->user_login;
//if is admin or 'is_user' does not equal #username
if (!current_user_can('manage_options')){
//if in the admin panel
if($query->is_admin) {
global $user_ID;
$query->set('author', $user_ID);
}
return $query;
}
return $query;
}
add_filter('pre_get_posts', 'my_authored_content');
This will only let admin and the author see the content.
You can either add it to the main functions file or turn it into a plugin.
To create it as a plugin:
Create a new file
add the code from here: http://pastebin.com/rfMLM0BU
save it as my-authored-content.php
upload it to your plugins directory.
Hope this helps you! :-)
For me works this:
function mymo_parse_query_useronly( $wp_query ) {
if(isset($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] == 'attachment'){
if ( !current_user_can( 'level_5' ) ) {
$wp_query->set( 'author', get_current_user_id() );
}
}
}
add_filter('parse_query', 'mymo_parse_query_useronly' );
I use this for uploaded profile picture for the user profile in front end

Resources