I edited a bit of code the other day from the Wordpress Ultimate Members plug-in to change the user profile to pending if the profile is edited by the user.
However, I noted that the code also sets the admin account to pending too. I don’t want this, so I have been trying to use an if/ else statement to query if the user is admin before the script runs.
I know this is straight forward for experts in PHP, but I have tried lots of variations and admin is still being set to pending approval.
Here’s the original code that is setting admin to pending:
// Set profile to under review after edits
add_action('um_user_edit_profile', 'um_post_edit_pending_hook', 10, 2);
function um_post_edit_pending_hook($user_id, $args){
global $ultimatemember;
$ultimatemember->user->pending();
}
Here s the code that I am trying to bypass admin with which won’t work. I won’t add all the variations I have tried.
add_action('um_user_edit_profile', 'um_post_edit_pending_hook', 10, 2);
function um_post_edit_pending_hook($user_id, $args){
if ( is_admin() ) {
return false;
} else {
global $ultimatemember;
$ultimatemember->user->pending();
}
}
Any assistance would gratefully be appreciated.
This Conditional Tag checks if the Dashboard or the administration panel is attempting to be displayed. It should not be used as a means to verify whether the current user has permission to view the Dashboard or the administration panel (try current_user_can() instead):
https://codex.wordpress.org/Function_Reference/current_user_can
Source:
https://codex.wordpress.org/Function_Reference/is_admin
#user45250 has explained this very well. I have added this answer to provide some example code.
Here is the code from my earlier comment.
add_action('um_user_edit_profile', 'um_post_edit_pending_hook', 10, 2);
function um_post_edit_pending_hook($user_id, $args){
if ( current_user_can('administrator') ) {
return false;
} else {
global $ultimatemember;
$ultimatemember->user->pending();
}
}
P.S. here is a related question, gmazzap's answer has more detail.
Related
I want to add my own custom payment error text (we use Account Funds and want to add an extra prompt)
How can I change, the following which shows when there is not enough funds, from:
Sorry, it seems that there are no available payment methods for your
state. Please contact us if you require assistance or wish to make
alternate arrangements.
to an error message with a link like below:
You do not have sufficent funds to process this order, please top up or upgrade. Thank you.
The text seems to be stored in templates/checkout/payment.php
https://github.com/woocommerce/woocommerce/blob/ef05bfccfc01bb2c621ef1293e61f7c57950670f/templates/checkout/payment.php
How can I change this without it getting wiped out by a Woocommerce version upgrade?
In WordPress, filters are functions that can be hooked to an event (called hooks). During the execution when the event is triggered the filter is applied to the data output generated by the event hook. It is important to remember that filters perform their actions on data they receive and then return that data before it is displayed in the browser.
In the file you attached (payment.php) you have
apply_filters( 'woocommerce_no_available_payment_methods_message' ....
So you can use the filter "woocommerce_no_available_payment_methods_message" to change the text
Create a custom function and add it to functions file or a small plugin.
First we hook our own function with woocommerce event
add_filter( 'woocommerce_no_available_payment_methods_message', 'your_custom_function_name_here' );
Now we define what our function would do.
function your_custom_function_name_here( $content ) {
//your changes here
$content = "bla bla";
// Returns the content.
return $content;
}
You just have to add a filter and apply your change in your child-theme's functions.php file:
add_filter( 'woocommerce_no_available_payment_methods_message', function( $no_gateways_message ) {
return 'You do not have sufficent funds to process this order, please top up or upgrade. Thank you.';
});
Let me know if that worked.
add_filter( 'woocommerce_no_available_payment_methods_message', 'change_payment_message', 10, 2);
function change_payment_message( $value, $arg2 ) {
$message = WC()->customer->get_billing_country()?'You do not have sufficent funds to process this order, pleasetop up or upgrade. Thank you':'Please fill in your details above to see available payment methods.';
return $message;
}
I have a wordpress multisite install with 100+ users with blogs. Some users have more than one blog.
Users can pay for their blogs (think of it as a donation scenario), or they can choose to have a free blog. If they have a free blog then there is a variable in wp_Blog-ID_options called is_free_site and it is set to 1. (Blog-ID relates to the users blog)
If the user is paying for their site, is_free_site will either be set to 0 or the variable won't exist in the db at all. See screenshot:
http://www.awesomescreenshot.com/image/1657353/0238f2bf2d49f0b165170be6c64ba3a3
I am trying to write a function called does_user_pay this will look to see if the current logged in user pays for any of their sites and if they do it returns true. This is so I can feed premium content to those who choose to pay
So for example, user A might have 2 sites, one which they pay for, one which they don't - so does_user_pay() should be true
User B might have 1 site which they don't pay for, so does_user_pay() will be false
user C might have 1 site which they pay for, so does_user_pay() will be true.
I am coding this into a custom plugin, here is what I have so far:
function does_user_pay() {
global $current_user;
$user_id = get_current_user_id();
$user_blogs = get_blogs_of_user($user_id);
// Need to write a function here that checks if any of the user blogs are paid for
if(is_user_logged_in() && USER_HAS_PAID_SITE) {
return true;
} else {
return false;
}
}
Any help would be really appreciated
You can do this check in this way (assuming user is logged and his id is stored in $user_id):
$blogs = get_blogs_of_user($user_id); // array with all user blogs
foreach($blogs as $blog){
switch_to_blog($blog->userblog_id); // switch the blog
$is_free_site = get_option('is_free_site', 0); // get the option value (if not exists, so the user paid, we'll get 0)
restore_current_blog(); // it's important to restore after a switch blog
if($is_free_site == 0) return true; // found a paid blog
}
return false; // not found a paid blog
I'm writing a plugin that is supposed to hook on the posts query and add some parameters to it.
It should work like this:
Note: We are in the authors admin panel.
An author click on "Show me all the posts"
In the link there are some parameters
The plugin receive the hook with add_filter( 'parse_query', 'sgcp_posts_filter' );
I edit the query with parameters
Wordpress show me a list of my filtered posts.
Now, with an Administrator Role user it works, but it doesn't with an Author Role user .
It sends me back the extremely useful message:
You do not have sufficient permissions to access this admin page.
Here is my code:
add_filter( 'parse_query', 'sgcp_posts_filter' );
function sgcp_posts_filter( $query ){
global $pagenow;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'nf_sub' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['study_groups']) && $_GET['study_groups'] != '') {
$params=$_GET['study_groups'];
$query->set('meta_query' , array(
array(
'key' => '_field_44',
'value' => $params
)
));
}
}
Note: If I comment the $query->set... inside the if statement I don't get the permissions problems anymore.
Thanks to everyone.
I've found a solution to the problem.
What I did wrong was to use a dynamic url on a menu link, while using Admin Menu Editor plugin (very nice plugin).
The plugin indeed use the url of the menu element to identify it.
Since it was changing every time with the parameters I had to pass to the query, every time it was using default permissions to access that menu element. As you can guess, author role was not authorized by default, thus firing the You don't have enough permissions... alert.
The solution was to move the logic to find the parameters inside the page called by the menu element.
I am not an expert in PHP/WordPress and I am running a WP site with multiple user levels. I have already created a custom post type called "race". I would like to allow the Contributor level able to edit "races" that have been published from any other user, not only by themselves.
Currently, only the Author can do this, while Contributor can only edit the "races" himself has published.
The custom post type was made by CPT UI plugin and I did not code it myself.
I have a user role / capabilities plugin ("User Role Editor" and "Members") and I know how to use them once the capabilities are registered, but I do not know how to set up the capabilities for a custom type post.
I think this entry is related to my problem (the last bit of code) but I do not know how to adapt it in the case that the post type already exists, and in which files the code should be inserted to.
PS. As a second step, I would like for edits to have to be approved by an Author anytime they are made by a contributor. Currently, if a contributor edits his own "race", the changes are published immediately. Approval is only needed for initial publication right now.
Wordpress has a filter, it is used to check your capability called "user_has_cap". We can add "edit_other_posts" and "edit_published_posts" for Contributor through this. I have a code here for you, you can put it on your theme functions.php file or any php file in your plugin, try it:
function allow_user_edit_race_post( $all_caps, $caps, $name, $user ) {
if( is_user_logged_in() && $name[0] == 'edit_post' ) {
global $current_user;
$post = get_post( $name[2] );
if( in_array('contributor', $current_user->roles ) && ! is_wp_error($post) && $post->post_type == 'race' ) {
$all_caps['edit_others_posts'] = true;
$all_caps['edit_published_posts'] = true;
}
}
return $all_caps;
}
add_filter( 'user_has_cap', 'allow_user_edit_race_post', 10, 4 );
Hope this help!
Add this bit of code to your functions file:
function wp_change_cap_type() {
global $wp_post_types;
$wp_post_types['race']->capability_type = 'race';
}
add_action('init', 'wp_change_cap_type');
Then assign the capability 'edit_others_races' and 'edit_races' to the Contributor role and any others that you want to have the capability (Editor, Administrator, etc). Do not assign 'publish_races' to the Contributor role, as only the Author is supposed to do that.
Hi I am pretty new to wordpress,php and all this editing thing. I want to add a new cookie to wordpress upon authentication with name "xxx" and value "(currentusername)". I already read http://wptheming.com/2011/04/set-a-cookie-in-wordpress/ . I add the required code to the functions.php of my code however I don't know how to invoke it such that the currentusername logginned is added to the cookie.
Thanks in advance
Here is the code on the other website which I inserted in my functions.php
function set_newuser_cookie() {
if (!isset($_COOKIE['sitename_newvisitor'])) {
setcookie('sitename_newvisitor', 1, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);
}
}
add_action( 'init', 'set_newuser_cookie');
Bumped into this one - I recommend against adding a new cookie, instead I would hijack (take advantage of) the current cookie and let WP manage it for you. Additionally the hooks available in WP allow very clean and tight code using WP features - try the snippet below - I put in comments and tried to be verbose :
function custom_set_newuser_cookie() {
// re: http://codex.wordpress.org/Function_Reference/get_currentuserinfo
if(!isset($_COOKIE)){ // cookie should be set, make sure
return false;
}
global $current_user; // gain scope
get_currentuserinfo(); // get info on the user
if (!$current_user->user_login){ // validate
return false;
}
setcookie('sitename_newvisitor', $current_user->user_login, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false); // change as needed
}
// http://codex.wordpress.org/Plugin_API/Action_Reference/wp_login
add_action('wp_login', 'custom_set_newuser_cookie'); // will trigger on login w/creation of auth cookie
/**
To print this out
if (isset($_COOKIE['sitename_newvisitor'])) echo 'Hello '.$_COOKIE['sitename_newvisitor'].', how are you?';
*/
And yes, use functions.php for this code. Good luck.