• Resolved sacconi

    (@sacconi)


    I’m used up to now to sanitize text areas and simple text fields, but the following for a checkbox doesnt work:

    update_user_meta( $user_id, '6_checkbox', sanitize_text_field( $_POST['6_checkbox'] ) );

    The page I need help with: [log in to see the link]

Viewing 10 replies - 16 through 25 (of 25 total)
  • Moderator bcworkz

    (@bcworkz)

    In what context does this code occur? If it’s within the same request that includes the user’s checkbox choice, the same race condition is likely at play and whether something is done about the choice or not should depend on empty( $_POST['checkbox_6']) and not the saved author meta.

    OTOH if it’s not within the same request, relying upon the saved author meta value is the correct approach.

    Thread Starter sacconi

    (@sacconi)

    The above code is in a custom plug in, instead the checkbox code is in a file included in functions.php

    Moderator bcworkz

    (@bcworkz)

    I’m sorry, the meaning of “context” is ambiguous. I meant in relation to HTTP requests and code execution, not physical location in files. Although the fact it’s in a custom plugin is useful information.

    It’s possible the context is for both the same request and separate requests. You might need if/else code similar to what we did for checkbox status.

    Thread Starter sacconi

    (@sacconi)

    it’s not clear to me how I should change the code in my custom plug in , in particular the variable $checkbox_status, accordingly to the new check box code; now I have https://pastebin.com/q3d9FX5E

    Moderator bcworkz

    (@bcworkz)

    In what context does this code occur?

    Same request as the form with the check box? Different request? Or could be either one?

    What does a checked box indicate? Show a discounted price?

    How does author discount relate to post discount (function_sconto)?

    Thread Starter sacconi

    (@sacconi)

    The sense of what I am doing is the following:

    The discount concerning all prices of apartments under the same agency (author) is ruled by $discount, when I want the discount of a specific apartment (post) to preveal over the agency discount ($discount) I fill the field ruled by $discount_post. But for a specific period (n 6) I would like, when the checkbox is checked, to mantain the general agency discount, instead letting the post discount to preveal. So, the logic in pseudo code would be: if $price is not empty { if it’s not empty $discount_post and the checkbox is not checked > apply $discount_post , else if it’s not empty $discount > apply $discount

    So when the checkbox is checked I should have $discount applied even in presence of $discount_post

    why I do this? Because an agency of Lignano I cooperate with have a specific “early birds” discount (author) until the end of january but only for some apartments (posts) and with the exception of the hight season period, so i’m trying to cross all these variables

    Moderator bcworkz

    (@bcworkz)

    Apparently the context is for any of the apartment/post listings, separate from whatever page has the checkbox. Thus there shouldn’t any issue with a race condition.

    I understand (I think) the discount priority and how the checkbox alters which discount is applied. How does author meta “6_periodo” factor into all of this? It appears to just be an arbitrary string that’s part of the output? It’s not any kind of expiration date or anything where after the time period the discount is automatically removed? So its value does not affect any of the applied logic?

    I think what you have is correct? If not, under what conditions is it not working as you want?

    Thread Starter sacconi

    (@sacconi)

    Let’s take this specific apartment: https://test.sacconicase.com/alloggio-6-posti-a-lignano-sabbiadoro-a-due-passi-dal-mare-e-dalla-zona-pedonale/#titolo_listino_prezzi

    This apartment has a $discount_post = 20% and the general discount of this agency ($discount) is 5%, the checkbox is checked so theoretically I should see a discount of 5% in the sixth period, but all the periods have the same discount. Maybe it’s unusefull creating a meta name for a chekbox? maybe “checkbox_6” is unusefull, what I need is just the status, checked or unchecked. “6_periodo” is the only period that would be affected by the checkbox status

    Moderator bcworkz

    (@bcworkz)

    Use names that indicate purpose. “use_agency_discount” is more meaningful than “checkbox_6”

    I tested your code on my site and it works the way I understand it should. With the box checked the agency discount is applied. However, I don’t have your meta data on my site, so I replaced all get meta calls with typical hard coded meta values. For example: $discount_post = '20'; If you’re not getting expected results, be sure $post->ID is correct and that each meta value returned is correct, as these are the only things that are different than your code in my testing.

    The $period value does not influence which discount is applied. If you have a checkbox for each period, the $period value needn’t influence which discount to apply. But then you need to be getting a different checkbox meta value for each period. Having individual check boxes for each period is not the only approach. The best approach depends on what sort of user interface you want.

    Thread Starter sacconi

    (@sacconi)

    I found the error

    $checkbox_status = get_the_author_meta($post->ID, 'checkbox_6', true);

    The checkbox is in the edit author page so the place should be

    get_the_author_meta( 'checkbox_6' , $post->post_author );

    it’s an error I did many times, now it works!!

Viewing 10 replies - 16 through 25 (of 25 total)

The topic ‘Sanitizing a checkbox’ is closed to new replies.