disable delete media wordpress - wordpress

Here's what I've been trying to figure out how to do for hours now.
I want the author role to be able to upload content into the media library from their posts, and to be able to view the entire media library. What I don't want is to let the author role delete any media, even their own.
I've thought about automatically switching authors to a 'media' user upon upload completion, but I was hoping there'd be a cleaner way.
Any ideas?

There's no built-in capability for "delete_media." I think this is encompassed by "delete_posts," since uploads are treated as posts. (Note that authors can delete only their own posts and attachments.)
Adding a role or capability is possible, but you'd have to replace the default media admin screens, where the delete action is controlled by the "delete_posts" cap. You don't want to mess with the core files upload.php etc. So you'd have to restrict access to them, then write your own panels for authors. You probably don't want to do this. :-)
There's a better way, however. If you assign uploads to an administrator when they are saved then authors will not be able to delete them. You can use the add_attachment and edit_attachment action hooks to change the post_author to an administrator id.
Hope that helps.

There are a number of places where WordPress will let users delete images, so trying to hide them all can be challenging (and dangerous, because a new plug-in or version of WordPress could add another). However, you can add the following function to prevent deletions and throw an error (not pretty, but effective). You could enhance this by adding a custom capability for deleting images, if you wanted a finer level of control.
add_action('delete_attachment', 'DontDeleteMedia', 11, 1);
function DontDeleteMedia($postID)
{
if (!current_user_can('manage_options')) {
exit('You cannot delete media.');
}
}

add this code in your functions.php file:
add_action('media_row_actions','users_own_attachments', 2, 1);
function users_own_attachments( $wp_query_obj ) {
if( !current_user_can( 'delete_plugins' ) ){
unset($wp_query_obj['delete']);
return $wp_query_obj;
}
}

Related

Restrict access to comments in WordPress using the plugin Restrict Content Pro

I would like to ask if someone can help me with this question. I can't find anywhere how to do it.
I have a website with Wordpress as a CMS. I use the plugin Restrict Content Pro to restrict access to exclusive content.
I share the post in freemium mode.
And now the question:
How could I restrict access to post comments?
I guess through something in the php code, but I don't know how.
Can anybody help me?
Thank you!
I don't use this plugin myself, but just found a good looking tutorial. Apparently, you restrict access to comments in the same way that you restrict access to anything else: By only outputting it in the theme files when the user has access to it. For example, the TwentyTwentyOne theme has a file "comments.php", which starts like this:
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password,
* return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
$twenty_twenty_one_comment_count = get_comments_number();
?>
You can easily add another check beneath the password check:
/**
* Check if the user has access, don't output the comments if they don't.
*/
if ( !rcp_user_has_active_membership() ) {
return;
}
Of course, instead of returning you can also output something along the lines of "get a membership to see the comments".
The tutorial I found lists different ways to check for a membership, a paid membership and more special checks.
Two more things:
Be sure to check if there are other places where comments are displayed. That very much depends on how the WordPress theme is built.
Don't just change the theme if it comes from an external source, e.g. if it might receive updates in the future. In that case create a child theme and make your changes there instead. You can read more about child themes in the Theme Handbook.

Display the same author name for all users in WordPress?

For a Wordpress organization website, I want all the posts to have by default the same author name (the organization name). How can I achieve this behavior?
There are multiple possibilites.
1) Simplest: You only create one author and share the login within the organization
2) You simply do not display the author on your post - why would you do that? It is probably obvious that your organization is the publisher of these pages anyway.
3) Add the following custom code within your Theme or Plugin:
add_filter('the_author','its_my_company');
function its_my_company() {
return 'Organization Name';
}
https://developer.wordpress.org/reference/hooks/the_author/
Your best best is to modify your theme or child theme to display a specific user or name wherever the template does so. In your single.php file, look for the_author() or get_the_author(). Then use something like get_user_by() to pull a specific user, or else hard code the value (less ideal, but an option).
Your other option is to manually set it each time, which I could exactly define as making anything "default." Even so, the option exists.

How to customize Wordpress Theme before going live

Yesterday I installed a new theme on Wordpress on my self-hosted website. I am aware of the feature that allows you to preview a theme and have used it to select a new Theme that I want to install.
Problem
I do not want to interrupt normal operations of my website, but this new theme requires a lot of customization before it is ready to go. How do I do this?
My Crappy Solution
Is the only way to go about it to run a virtual server on my desktop? This seems tedious, not to mention all the errors I usually get when switching to the "real" server when doing this.
A better way?
I've been searching on SO as well as the WordPress Forum for an answer as to how to do this, but have come up short. I would have thought this is a common question. Maybe I'm using the wrong search terms [themes, customization, before installing]???
Any help is greatly appreciated! Thanks!
Ok, since your question is a pretty good one and probably not a few people are going through the same process when they decide to update their site, I decided to give a try to the get_stylesheet and get_template filter hooks. It turns out that with a very small plugin, you can easily enforce a specific theme(well in this case any logged-in visitor, but you can change this to use any logic you want) according to a specific rule/s.
Here's the code that you need to put in a file in your plugins directory:
<?php
/*
Plugin Name: Switch Theme
Description: Switches the theme for logged-in visitors, while keeping the current theme for everyone else. !!!NOTE!!! Please back-up your database prior using this plugin - I can't guarantee that it will work with any theme, nor that it won't break your site's set-up - USE AT YOUR OWN RISK(I did a quick test and it seemed to be fine, but haven't done extensive testing).
You don't need to switch to the desired theme before that - you want to keep active the theme that you will display to your visitors - the one that you will see will be used programatically.
Before activating the plugin, change the line that says `private $admin_theme = '';` to `private $admin_theme = 'theme-directory-name';` where "theme-directory-name" is obviously the name of the directory in which the desired theme resides in.
*/
class MyThemeSwitcher {
private $admin_theme = '';
function MyThemeSwitcher() {
add_filter( 'stylesheet', array( &$this, 'get_stylesheet' ) );
add_filter( 'template', array( &$this, 'get_template' ) );
}
function get_stylesheet($stylesheet = '') {
if ( is_user_logged_in() && $this->admin_theme ) {
return $this->admin_theme;
}
return $stylesheet;
}
function get_template( $template ) {
if ( is_user_logged_in() && $this->admin_theme ) {
return $this->admin_theme;
}
return $template;
}
}
$theme_switcher = new MyThemeSwitcher();
So - first of all BACKUP YOUR DATABASE! I tested locally with Twenty Eleven being the default theme and a basic framework theme as my custom theme - the theme options and navigation menus were saved properly.
Then all you need to do is to update the file(change the line that says private $admin_theme = ''; to private $admin_theme = 'theme-slug'; where theme-slug is the name of the directory in which the theme you want to use is).
Also - you won't be able to change the Front page and Posts page options, without this affecting the live site, nor will you be able to change the any shared components that both themes use(Site name, Front Page, Posts page, Posts Per Page, etc options, content and so on).
So if you have no clue whether this solution is for you - well, it depends.
If both themes are not relatively complex, then most-likely you should be able to use this hack. If they are though maybe you should do a second installation of your website as others suggested - I think that a second installation in either a sub-domain or a sub-directory would be the best option for you(simply because moving a multisite database is more complex than moving a normal WP database).
I'd setup local apache server with a wordpress installed to customize and test a new theme. When you finished customizing it then you can upload the theme to your live site and activate it. If there are settings that you need to set in the dashboard then you probably will have to adjust them again. That's one way to test/customize a theme before putting it live.
You could create a network (make WordPress multisite with define('WP_ALLOW_MULTISITE', true);, see : http://codex.wordpress.org/Create_A_Network) and then create one sub-site, then turn it "off" with a Maintenance plugin so it is not accessible to users not logged in as admin, export your posts & data from main blog, import them in sub-blog with WordPress default importer, then apply your new theme to this sub-blog and work on it. When everything satisfies you, apply the theme to the main site and deactivate subsite.

Wordpress 3 Multisite with same Medialibrary

i created a Worpress 3 Multisite with 5 Sub-Blogs.
Is it possible to share the same Media-Library in this Blogs?
i changed upload_path in wp_1_options and wp_2_options for example and also in my backend in "Super Admins" Menu but it has no effect.
The files are uploaded to wp_contents/blogs.dir/1-2-3/files and the options have no effect.
any ideas? thanks!
One way around is to hook onto the load events of all media admin files, and switch to the main blog using switch_to_blog(1).
This would mean in any blog admin, the media library will always run as if it were on the main blog.
Note that a couple caveats include;
The media library for all blogs is stored in the main blog database table.
You may run into problems with inserting media into posts outside the main blog admin
You will run into problems with inserting galleries into posts outside the main blog admin
User permissions may be false positives or negatives
My best advice would be to use the code example below, and have a good play around with blog admins, logged in as different users, with different roles, and see what happens.
function use_main_blog_library()
{
switch_to_blog(1);
}
add_action('load-media-new.php', 'use_main_blog_library');
add_action('load-media-upload.php', 'use_main_blog_library');
add_action('load-media.php', 'use_main_blog_library');
add_action('load-upload.php', 'use_main_blog_library');
In my searching on this topic, several posts lead back to this one, so I figured I'd share an idea that might help someone wanting to develop a suitable plugin to solve this...
Use get_site_option() and update_site_option() to store global plugin options.
Add an option via hooks to choose if a media upload should be shared network-wide and let the plugin track which media files and where they're located at.
Again, using hooks, have the shared items show up in each blog's media library and perhaps add an indicator showing the file is a network share.
I have spent several hours playing with the administrator hooks and filters and this could be accomplished through them, although I'm not savvy enough to know how to fully integrate it with all of the media library features.
The Shiba Media Library Plugin could serve as a valuable reference since they have utilized several custom features for the Media Library via hooks and filters.
I really wish I had the spare time to work on this right now because I would take my best shot at it. I hope this helps someone else.
I have found a possible solution, which works for me in WP3.7.1 (I haven't tested it in earlier versions)
Create a filter, which overrides the default upload directories:
add_filter('upload_dir', 'ms_global_upload_dir');
function ms_global_upload_dir($uploads)
{
$ms_dir = '/sites/' . get_current_blog_id();
$uploads['path'] = str_replace($ms_dir, "", $uploads['path']);
$uploads['url'] = str_replace($ms_dir, "", $uploads['url']);
$uploads['basedir'] = str_replace($ms_dir, "", $uploads['basedir']);
$uploads['baseurl'] = str_replace($ms_dir, "", $uploads['baseurl']);
return $uploads;
}
Important: the 'Upload Url Path' settings should be empty in Site Settings or if you need to customize it, check the results by dumping the $uploads array to view possible conflicts.
To check if your version of WP supports this method, locate function wp_upload_dir() in file wp-includes/functions.php and find function call: $uploads = apply_filters( 'upload_dir' ...
If it presents, the solution above should work.
Hope, this helps ...
Additionally, I have spent almost two days to make a solution to replicate/delete the uploaded media in each of the blogs with action hooks 'add_attachment' and 'delete_attachment' by generating the necessary post and postmeta entries in the corresponding database tables. With this, you can add/delete media in any of the blogs, that will be visible in/removed from all blogs media library. If you are interested, I can share it...
Cheers

Hide other domains' menus from node edit form on a Drupal site using domain access

I'm in the process of making some improvements to a live Drupal site that's using the Domain Access module to run a number of microsites. I'm trying to find a way of restricting the menus a user can post content to from the node edit screen. A user on one of the domains should only be able to post content to menus associated with that domain.
Is there a simple way of achieving this? I'm guessing there are some hooks I could use, but so far I have been unable to identify them. I'd prefer not to have to install further modules to achieve this and to be able to add some code to the current site to alter the forms. The site is struggling with the large number of modules we've had to install on it already.
According to the readme for the module, you need to set some specific permissions in user management:
To enable this feature, you should grant the 'edit domain nodes' and
(optionally) the 'delete domain nodes' permission to some roles. Then assign
individual users accounts to specific domains to assign them as Domain Editors.
From my experience many moons ago with the module, you can check the global $user object and figure out what domains the user should have access to. You can then use a form alter to remove any options from the select box that you don't want them seeing. As always with Drupal though, it's better to let someone else write the code - so if the Domain module provides this functionality, use it!
Here is some updated code for Drupal 7:
/**
* Implements hook_form_FORM_ID_alter().
*/
function MYMODULE_form_page_node_form_alter(&$form, &$form_state) {
global $_domain;
if (isset($_domain['domain_id'])) { // only display domain's primary links
$menus[domain_conf_variable_get($_domain['domain_id'], 'menu_main_links_source')] = $_domain['sitename'].' Main menu';
}
if (isset($menus)) {
$options = menu_parent_options($menus, $form['#node']->type);
$form['menu']['link']['parent']['#options'] = $options;
}
}
Eventually found a way of fixing this for the particular project I have been working on: in module_form_alter I've added the following:-
global $_domain;
if (isset($_domain['domain_id'])) { // only display domain's primary links
$menus[domain_conf_variable_get($_domain['domain_id']
,'menu_primary_links_source')] = $_domain['sitename'].' Primary links';
}
if ( isset($menus) ) {
$options = menu_parent_options($menus, $form['menu']['#item']);
$form['menu']['parent']['#options'] = $options;
}
This restricts the menu options to just the current domain's primary links menu which is just what we wanted.
Thanks to Fabian who pointed me in the right direction earlier.

Resources