How to hide "Add Media" from frontend users? - wordpress

I'm running a multi author's wordpress site, and users can post contents directly from the front end of my site.
My problem is this, file upload is directly allowed on the front end of my wordpress site.
I want a situation whereby, when users click on Add Media button from my site post submission form,
Instead of seeing this wordpress Add Media Library to select and upload media,
I want them to see this instead. Takes them straight to their device file.
Any code i can use to execute this on my wordpress site?

To hide “Add Media” from frontend users, you should limit access to the media library to administrator and editor roles only.
From WP Beginner, You’ll need to add the following code to your WordPress functions.php file or a site-specific plugin.
// Limit media library access
add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments' );
function wpb_show_current_user_attachments( $query ) {
$user_id = get_current_user_id();
if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts
') ) {
$query['author'] = $user_id;
}
return $query;
}
This code uses current_user_can function to check if the user has the capability to activate plugins or edit other user’s posts. If they don’t, then it changes the query used to display media files and limit it to user’s ID.

Related

WordPress: adding or enabling user/customer registration? And making content only visible when logged in?

I currently have a very basic WordPress site with a few pages.
There are no user accounts, only my own admin account.
What I'm looking for:
A possibility for visitors to sign up i.e. register their own customer account. I don't need detailed account information, just an email address and perhaps a name and/or company name.
Restrict certain content to logged in visitors only. Preferably when someone is not logged in I would want the page to show 'this information is only accessible for customers - log in or sign up here'. And if someone is logged in, show the actual content.
Caveat: I'm dealing with a Woocommerce site and one of the things I'd like to restrict to logged in users only is the Shop page. But as I understood this is not a normal page with regular content in Wordpress. I guess its content is generated or controlled through the Woocommerce plugin. So is there a way to pull this off?
I'd assume this is a very common pattern in WordPress and this is probably very simple. But being a total WordPress newbie with zero experience, I wouldn't know how/where to start.
User Registration
Those are default WordPress behaviors. Enabling registration can be done via the admin control panel.
Settings → General → Membership, ✓ Anyone can register
Once this is done, users can registered # http://localhost/www/wordpress/wp-login.php?action=register
Conditional Tags
Conditional Tags can be used in your Template Files to alter the display of content depending on the conditions that the current page matches. They tell WordPress what code to display under specific conditions. Conditional Tags usually work with PHP if/else Conditional Statements.
Source # https://developer.wordpress.org/themes/basics/conditional-tags/
Restricting content to logged in user can be done via the is_user_logged_in() function.
Determines whether the current visitor is a logged in user.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Source # https://developer.wordpress.org/reference/functions/is_user_logged_in/
Use Case Scenario
<?php
if ( is_user_logged_in() ) {
// ... logged-in user content
} else {
// ... non-logged-in user content
};
WooCommerce Conditional Tags
You can use is_shop() to determine if the current page is a shop page.
Returns true when on the product archive page (shop).
Source # https://docs.woocommerce.com/document/conditional-tags/#section-4
Usually the shop page is built around the archive-product.php page template. the following should englobe the content from the archive-product.php page template. You don't have to specify is_shop as we're building the conditional statement on the archive-product.php page template.
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
};
/**
* Basic logged-in restricted `archive-product.php` page template example.
*/
get_header();
if ( is_user_logged_in() ) {
// ... logged-in user content
} else {
// ... non-logged-in user content
};
get_footer();
Template hierarchy
As you're new to WordPress you should try to understand the basics in term of template hierarchy. You should refer to https://developer.wordpress.org/themes/basics/template-hierarchy/. WooCommerce works the same.

How can I set an "mobile deep link" to downloadable file from woocommerce?

I have woocommerce setup up on a wordpress site for downloading PDF files. After a user goes through the checkout process and is ready to download the file, I'd like to check to see if they are on a mobile device or not. If the user is not on a mobile device, the download link would just be normal.
If the user is on a mobile device, I'd like to set the download link to be an "app deep link" using a custom URI scheme (from an existing vendor). For example, instead of the download link referencing http:// or https://, I'd like the download link to be appname://domain.com/folder/filename.pdf. This way, the PDF would automatically open in my app when they clicked on the link. The app vendor already support the custom URI schema, so I am just looking for "how" to set the link dynamically in WordPress/Woocommerce.
How would I go about:
detecting if the user is on a mobile device, and
changing the URI scheme if they are a mobile user?
The concept I am trying to accomplish is described here in the "Native App" example: http://www.mobiloitte.com/blog/deep-linking-101
and here: https://en.wikipedia.org/wiki/Mobile_deep_linking
For your first question, trying using javascript window.navigator.userAgent or with PHP try using $_SERVER['HTTP_USER_AGENT']. This should work independent of Wordpress or other site details. See related questions here and here.
For your second question, deep linking is used for individual mobile apps, right? The answer probably depends on which app you're targeting. If it's an existing app, you might get the best traction contacting the app developer. If you're writing a new app, there is information for android here and for iOS here. If you want a permalink URL that refers to specific content on your Wordpress page (like a specific transaction event and user name) then you can find information about permalinks and query strings with WordPress, for example here.
Firstly, to see if you're on a mobile platform, WP has a function for that: wp_is_mobile (see Codex page).
Then, if you look at the source of the class-wc-download-handler.php file (see here), at line 177 is a download( $file_path, $product_id ) function. This, at line 188 generates the download path using apply_filters( 'woocommerce_file_download_filename', $filename, $product_id ) so, in theory, you should be able to hook into that and use a regex to replace http with your deep link uri.
Hope that helps
The following code runs before any type of download method, checking if it is mobile and replacing http or https in the download URL with appname, and then finally redirecting to the new URL. In this case, the final URL should be like:
appname://domain.com/folder/filename.pdf
Copy the following code into functions.php:
add_action( 'woocommerce_download_file_redirect', 'change_download_link_on_mobile' , 1, 2 );
add_action( 'woocommerce_download_file_xsendfile', 'change_download_link_on_mobile', 1, 2 );
add_action( 'woocommerce_download_file_force', 'change_download_link_on_mobile' , 1, 2 );
function change_download_link_on_mobile( $file_path, $filename = '' ){
if( wp_is_mobile () ){
$file_path = preg_replace( '/http|https/','appname', $file_path ); // Replace http|https with appname
header( 'Location: ' . $file_path );
exit();
}
}

How can I display Product Images to Logged In Users only, with Logged Out Users seeing an alternative image?

I would like to modify my WordPress/WooCommerce website, so that my Product Images have the following conditions:
Logged in site visitors:
I would like logged in visitors, to be able to see all of the WooCommerce Product Images.
Logged out site visitors:
I would like logged out visitors, to see an alternative image. Therefore, hide the Product Image. This alternative image, will be the same for every Product.
Firstly, you are going to need to create a Child Theme, for your WordPress/WooCommerce website.
Once created, simply insert the following code, into the functions.php file, within your Child Theme:
<?php
// Remove Default WooCommerce Product Images/Placeholders.
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
// Create conditional logic function.
function logged_in_images(){
// If site visitor is Logged in, display Default WooCommerce Product Images/Placeholder.
if ( is_user_logged_in() ) {
echo woocommerce_show_product_images();
}
// If site visitor is not logged in, display the following Image instead.
else {
echo '<img src=http://vaporcenter.be/theme/Wholesale/img/placeholders/default.jpg?1504084579>';
};
}
// We are inserting ('Hooking' into) the above function into where the Default WooCommerce Product Image/Placeholder was, before we removed it.
add_action( 'woocommerce_before_single_product_summary', 'logged_in_images', 20 );
?>
Be sure to replace http://vaporcenter.be/theme/Wholesale/img/placeholders/default.jpg?1504084579 with your desired image.
Don't forget, you can remove the comments (// ...) from the code. They are there, to just help explain what is happening.
I have tested the above code and it does work.
Best of luck!
If you receive an error
Step One: Using an FTP client, such as Filezilla, go into your Root Folder (Where you installed WordPress).
Look for a file entitled wp-config.php and drag this to your computer.
Open the file, using Notepad/Notepad++ and look for the line define('WP_DEBUG', false); and change 'false' to 'true'.
Drag the wp-config.php file back from where you got it from.
Refresh the page you are having an issue. You should then see an error message, which will be helpful in addressing how to resolve any issues.
Once you have resolved your issue, and your website is back in working order, head back into your wp-config.php and change 'true' back to 'false'.

hide popup maker plugin link from wordpress

I installed pop up maker and capabilities plugin in wordpress for my website. I want to create one user who is not able to see the link for pop up maker. I tried with adding user with some capability but I am not able to hide pop up maker link.
How can I achieve this? Do I need to use another capability plugin?
You can use the remove_menu_page() function called via the admin_menu action hook. If you just want to remove the link for a single user, check the current user's ID first with wp_get_current_user().
function remove_admin_menu_item_for_user() {
$user = wp_get_current_user();
// Check to see if the current user's ID is the one we want to remove the page for
if ( $user->ID == ??? ){
// Use the $menu_slug for the page you want to remove
remove_menu_page( $menu_slug );
}
}
add_action( 'admin_menu', 'remove_admin_menu_item_for_user' );
If you would prefer to use a plugin Admin Menu Editor should work as well, though you may need the Pro version.

Customizing Text in WordPress's wp-signup.php

I need to change the following default text on the WordPress multisite signup page (wp-signup.php):
"There is no limit to the number of sites you can have, so create to
your heart’s content, but write responsibly!"
...since this WordPress install will be limiting the number of sites a user can create.
Is it possible to override the content of this page without hacking the wp-signup.php file or dealing with .htaccess?
This WordPress install is being hosted by a third party service and it would be best if I could avoid dealing with modifying these files.
I've managed to hit on another solution. It turns out, that since the text on the wp-signup.php page is run through __(), you can alter it using the 'gettext' filter like so:
/**
* Modify default wp-signup.php text
*/
function themename_wp_signup_text( $translated_text, $untranslated_text, $domain ) {
global $pagenow;
if ( is_multisite() && $pagenow === 'wp-signup.php' ) {
switch ( $untranslated_text ) {
case 'Welcome back, %s. By filling out the form below, you can <strong>add another site to your account</strong>. There is no limit to the number of sites you can have, so create to your heart’s content, but write responsibly!' :
$translated_text = __( 'Welcome back, %s. By filling out the form below, you can <strong>add another site to your account</strong>. You can create a maximum of 5 sites. Remember to write responsibly or some junk! Ciao!', 'theme_text_domain' );
break;
}
}
return $translated_text;
}
add_filter( 'gettext', 'themename_wp_signup_text', 20, 3 );
...which is just for a theme, but you could do this in a plugin too.
So the text is technically filterable, but I'd say this is a huge hack though, but it'll do, and shouldn't have any consequences until that particular text changes in WordPress core...
As Dave Kiss said in his comment above, this string isn't filterable so you can't filter the text. The string isn't marked either so you can't use a translation file.
I suggest you open a WordPress Trac ticket requesting the text be marked or made filterable.
You can open a new ticket here: http://core.trac.wordpress.org/

Resources