My Wordpress text field package_id in the CPT gd_place should accept data only from a finite list of known and trusted values which are different depending on user e.g. role_one can only input 1, role_two can only input 2, role_tree only 3.
I first wrote this function, hiding the input field with css
function safe_package( $data ) {
if( 'gd_place' != $data['post_type'] ){
return $data;
//This is for one CPT only
}
$user = wp_get_current_user();
if(!is_admin() && !current_user_can('administrator')){
//Does NOT run in the backend and also takes the admin role into account
if(current_user_can('role_one')){
$data['package_id'] = 1;
return $data;
}
else if (current_user_can('role_two')){
$data['package_id'] = 2;
return $data;
}
else if (current_user_can('role_three')){
$data['package_id'] = 3;
return $data;
}
} else {
//do nothing new
return $data;
}
}
add_filter( 'wp_insert_post_data', 'safe_package' );
But then it might make more sense to just validate the user input (whitelist) and update_post_meta( $post->ID, 'package_id', $safe_package );
As the options are finite per user role maybe just replacing any wrong input would make sense... anyone familiar with validation?
Related
I want to create a shortcode with conditions based on who the current user is
I have the essence of my shortcode...
function my_shortcode_function() {
return 'here is the content of my shortcode!';
}
add_shortcode('coolshortcodename', 'my_shortcode_function');
But I need to add conditions to it...I want to say if user ID is Jonny then show this, if the user is Sally show that, if the user is Jamie something else...
function my_shortcode_function() {
IF JONNY
return reblex_display_block(610);
ELSE IF SALLY
return reblex_display_block(199);
ELSE IF JAMIE
return reblex_display_block(554);
}
add_shortcode('coolshortcodename', 'my_shortcode_function');
function my_shortcode_function() {
$current_user_id = get_current_user_id();
if ($current_user_id == 5) // where user ID of Jonny is 5
return reblex_display_block(610);
else if ($current_user_id == 6) // where user ID of Sally is 6
return reblex_display_block(199);
else if ($current_user_id == 7) // where user ID of Jamie is 7
return reblex_display_block(554);
}
basically you have to check the current user id by get_current_user_id()
here is the code that i have modified. I think it will work for you.
function my_shortcode_function() {
ob_start();
$jonny_id = 1; // You have to collect the specific user id from wp dashboard
$sally_id = 2; // You have to collect the specific user id from wp dashboard
$jamie_id = 3; // You have to collect the specific user id from wp dashboard
if(is_user_logged_in()){
if( $jonny_id == get_current_user_id() ){
return reblex_display_block(610);
}
elseif( $sally_id == get_current_user_id() ){
return reblex_display_block(199);
}
elseif( $jamie_id == get_current_user_id() ){
return reblex_display_block(554);
}
}
return ob_get_clean();
}
add_shortcode('coolshortcodename', 'my_shortcode_function');
I have a validating function to remove invalid terms from the product after saving.
my problem is that after I successfully removed the invalid terms.
they are re-added so when the update is complete.
the invalid terms are still saved to the post.
add_action('save_post_product','validate_product',99,3);
function validate_product($product_id,$post,$update){
$store_tax = 'pa_store';
if(get_field('requires_medical_license',$product_id) === true){
$stores = wc_get_product_term_ids($product_id,$store_tax);
$invalid_stores = [];
foreach($stores as $store){
if(get_field('store_type',$store)!=='pharmacy'){
$invalid_stores[] = $store;
}
}
if(!empty($invalid_stores)){
remove_action('save_post_product','validate_product');
$success = wp_remove_object_terms($product_id,$invalid_stores,$store_tax);
add_action('save_post_product','validate_product',99,3);
$after_stores = wc_get_product_term_ids($product_id,$store_tax);
// $success // true
// var_dump($after_stores) // does not contain any invalid stores
// wp_die() // if I stop the execution here, the terms are successfully removed.
}
}
}
I found workaround that works even better for my intentions.
add_action( 'added_term_relationship','check_validation',1,3);
function check_validation(int $object_id,int $term_id,string $taxonomy){
if($taxonomy==='pa_store'&&get_field('requires_medical_license',$object_id) === true){
if(get_field('store_type','term_'.$term_id)!=='pharmacy'){
wp_remove_object_terms($object_id,$term_id,$taxonomy);
add_action(
'admin_notices',
'admin_notice_product_validation_failed'
);
}
}
}
This basically hooks me right after the term relationship was created.
And lets me validate and remove it post creation.
I wish there was a cleaner way to do it so we dont have to call the DB twice.
but that is all I could find.
I am using Moodle 3.1+. I am trying to add a custom validation into profile edit. The field is a custom user profile field. I have created a function
function validYop($element_name,$element_value) {
$len = strlen($element_value);
// If the string is empty, then return false
if ($len == 0) { return false; }
$first_letter = $element_value{0};
$last_letter = $element_value{$len-1};
if ($first_letter == $last_letter) {
return true;
} else {
return false;
}
}
Then I have registered the rule using below code.
$mform->registerRule('same_firstandlast','function','validYop');
$mform->addRule('profile_field_YearOfPassing','The first and last letters must be the same', 'same_firstandlast');
But the code is not working. Please help
I've got woocommerce registration form with two sections:
- One for private person,
- the other for company.
In company option there is two additional fields. I can switch between private and company by radio buttons and then I see relevant fields.
Problem: When I fill the form (as private user) and make some mistake, form reload and show where is the error (that is ok).
But unfortunately, after reload, it loads the form with all fields (the ones with additional company fields too). So I need to click 2 times between private and company to restore the right behavior.
How can i fix this? I mean after this error reloading, to display the form as initially.
I don't be sure that this is code responsible for this, but let's try:
add_filter('woocommerce_registration_errors', 'rs_registration_form_validation', 10, 3);
function rs_registration_form_validation($reg_errors, $sanitized_user_login, $user_email)
{
global $woocommerce;
$company_fields_required = (!empty($_POST['registration_type']) && 'company' === $_POST['registration_type']);
$shipp_to_different_address = (!empty($_POST['register_ship_to_different_address']) && 1 == $_POST['register_ship_to_different_address']);
$errors = false;
$fields = rs_registration_form_fields();
if ($shipp_to_different_address) {
$fields += rs_registration_form_fields_address();
}
if (!$company_fields_required) {
unset($fields['billing_company']);
unset($fields['billing_nip']);
}
//Validate required
foreach ($fields as $field => $settings) {
if (false === isset($settings['required']) || true === $settings['required']) {
if (empty($_POST[$field])) {
$errors = true;
wc_add_notice('Pole: <strong>'.$settings['label'].'</strong> jest wymagane.', 'error');
}
}
}
if ($errors) {
return new WP_Error('registration-error', 'Proszę poprawić błędy w formularzu');
}
return $reg_errors;
}
add_action('woocommerce_created_customer', 'rs_registration_form_submit');
function rs_registration_form_submit($user_id)
{
$fields = rs_registration_form_fields();
$fields += rs_registration_form_fields_address();
foreach ($fields as $field => $settings) {
if (isset($_POST[$field]) && !empty($_POST[$field])) {
update_user_meta($user_id, $field, $_POST[$field]);
}
}
}
add_filter('register_form', 'rs_registration_form');
function rs_registration_form()
{
$fields = rs_registration_form_fields();
include '_rs_registration_form.php';
}
add_filter('register_form_address', 'rs_registration_form_address');
function rs_registration_form_address()
{
$fields = rs_registration_form_fields_address();
include '_rs_registration_form.php';
}
add_filter('woocommerce_edit_address_slugs', 'rs_fix_address_slugs');
function rs_fix_address_slugs($slugs)
{
$slugs['billing'] = 'billing';
$slugs['shipping'] = 'shipping';
return $slugs;
}
function rs_rejestracja_url()
{
return get_permalink(244);
}
function rs_logowanie_url()
{
return get_permalink(20);
}
function rs_show_checkout_progress_bar($step = '')
{
include '_checkout_progress_bar.php';
}
function rs_order_form_buttons()
{
include '_order_form_buttons.php';
}
add_filter('woocommerce_get_checkout_url', 'rs_get_checout_url');
function rs_get_checout_url($url) {
if (is_user_logged_in()) {
$url .= '#step1';
}
return $url;
}
include 'src/RS_Search.php';
I don't know WooCommerce, but I think the error results because of these lines:
$company_fields_required = (!empty($_POST['registration_type']) && 'company' === $_POST['registration_type']);
and
if (!$company_fields_required) {
unset($fields['billing_company']);
unset($fields['billing_nip']);
}
After you submitted your "private" form and the validation failed, your fields are loaded again. Could it now be, that in your $_POST variable the registration_type is somehow set to 'company'? You can test this if you just print_r your $_POST['registration_type'] at the beginning of the function. If that is not the case, then I'm pretty sure the bug happens in another function, because this makes sense to me so far.
EDIT: After taking another look on your code, I think none of the posted functions is responsible for the misbehaviour. The first function is only responsible to check if some of the posted values are missing and to say "hey, here is an error". There has to be another function which is responsible for the fields which later are displayed in your HTML. I think you need to find this function.
I'm trying to modify the below code snippet / function hook to disable registration if the user is logged in.
<?php
add_filter("gform_disable_registration", "disable_registration", 10, 4);
function disable_registration($is_disabled, $form, $entry, $fulfilled){
//check form id and if not the form being checked status passed in to function
if ($form["id"] != 160)
return $is_disabled;
//check submitted values to decide if registration should be stopped
if ($entry["4"] == "No" && $entry["5"] == "No") {
//disable registration
return true;
}
else{
return false;
}
}
?>
I've tried the following to no avail:
add_filter("gform_disable_registration", "disable_registration", 10, 4);
function disable_registration($is_disabled, $form, $fulfilled){
//check form id and if not the form being checked status passed in to function
if ($form["id"] != 2)
return $is_disabled;
//check user login to decide if registration should be stopped
if( ! is_user_logged_in() ) {
return true;
}
else {
return false;
}
}
Hoping I can get this to work! Thank you.
Here is an article/snippet I wrote to do this... I didn't confirm if this is still the best way to accomplish this, but it certainly is a way that works. :)
http://gravitywiz.com/skip-user-registration-for-logged-in-users/
I believe there's a setting in the Form Settings to allow you require users to be logged in. Is there a reason you can't just use that?