I've been struggling with this for a few days now. What I want to do, it to limit the Postcode/zip fields to 4 numbers. So, to add a maxlength and a type="number" to those fields.
Things I've tried so far:
(functions.php)
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['postcode']['maxlength'] = 4;
return $fields;
}
(functions.php) a jQuery way
(Unable to post, due some sort of code format error?)
EDIT: As it turned out, i was editing the old files on the server ... after editing the right one, the maxlength change works fine as the previously supplied code. But there is still the issue of changing the input type.
i tried adding $fields['billing']['billing_postcode']['type'] = 'number'; but that seems to REMOVE the input field all together for some reason. Possibly due to some error i guess. Not sure.
You'll need to add the attributes in a further subarray called 'custom_attributes', e.g.:
$fields['billing']['postcode']['custom_attributes']['maxlength'] = 4;
Related
I've found a couple of coding examples of this but nothing has worked. It seems such a simple thing to fix. We have an issue with our checkout page when certain users we're guessing have autocomplete on in their browser when checking out, we get duplicate or different Address 1 and 2 values. Even enabling the google api to help with autocomplete, didn't solve our issue. It corrected it some but, not enough.
This is one version of the solution I found in the interwebs melded with my own but, does not do anything....
/* Disable autofill address */
add_filter( 'woocommerce_form_field', 'change_autofill', 1, 1 );
function change_autofill( $field) {
$field = str_replace('autocomplete="billing_address_1"', 'autocomplete="new-password"', $field);
return $field;
}
I would like the ability to pick and choose which input fields to set the autocomplete attribute to "new-password" as I've read this forces all modern browsers to think it's a password field and does not try to fill them. When using inspector, each input field in the checkout has "autocomplete=off" already. That's why we are after this solution, in this manner...
Was able to get this working fully with this code (I was searching the wrong thing):
/* Disable browser autofill on all Woocommerce form fields */
add_filter( 'woocommerce_form_field', 'change_autofill', 1, 1 );
function change_autofill( $field) {
$field = str_replace('autocomplete="off"', 'autocomplete="new-password"', $field);
return $field;
}
This works because all fields are set with the attribute autocomplete=off it may not work for everyone. Use your inspector tool for each field to see. Whatever autocomplete is set to, is what you want your first start of str_replace searching for.
Cheers!
Just a comment regarding the approved answer, it should be:
$field = str_replace('autocomplete="new-password"', 'autocomplete="off"', $field);
instead of:
$field = str_replace('autocomplete="off"', 'autocomplete="new-password"', $field);
My PHP skills are not good but I learn by code example because I am not familiar with the syntax, but in this case I can not find a code example that works.
I want to set a Product Option Radio Button Field's selected value during a gform_pre_submission add_action based on another field's value. I've tried something like this (there are other functions and variables are defined elsewhere in advance, this is an abstract) but this approach isn't working for me and I am wondering what I am missing:
add_action( 'gform_pre_submission_FORMIDhere', 'select_radio_button' );
function select_radio_button( $form) {
foreach( $form['fields'] as &$field ) {
if( 53 === $field->id ) {
foreach( $field->choices as &$choice ) {
echo($aag_kg_nummorn.' / /'.$choice['value']);
if( $aag_kg_nummorn == $choice['value'] ) {
echo('Bingo!');
$choice['isSelected'] = true; // <- THIS line doesn't work for me
}
}
}
}
return $form;
}
Thank you for helping me in advance.
There are a couple of things I want to point out before answering
You should be modifying $_POST instead of the field, I can see you already figured this out.
gform_pre_submission is an action, not a filter, so returning the form here doesn't do anything.
The Answer
Say you have a product option field that has the ID of 2, and it has 3 options
First Option|1
Second Option|2
Third Option|3
Then the code will look like
add_action( 'gform_pre_submission_FORMIDhere', 'select_radio_button' );
function select_radio_button( $form ) {
$_POST['input_2'] = 'New Option|6';
}
I assume you are using this with a payment form that utilizes one of the gravityforms payment add-ons like stripe or Paypal, some of these add-ons (like PayPal Checkout) sends the total value to the payment gateway using JS before this action is fired, so the total value sent to the payment gateway will be the value of the option the user has already selected, but the total value on the entry will use the value you set in the code.
If you need help on how to override this from the JS side as well so you can send the overridden value to the payment gateway, I can write that in a separate answer.
So, I have two different custom post types. Both of these have a number of multi-select boxes. The first one, I was able to get the data in these boxes to save to the post without issue.
The second one, however, wasn't working. One of the 4 boxes had its data saved, and other form elements were saving, but the three other boxes weren't saving. After staring at the code, comparing the two back and forth, and trying numerous commenting and removing, I eventually, out of desperation, renamed the custom variable names that the data was being saved to. Lo and behold, it worked.
So, my question is -- why? Is there some variable overlap happening that I wasn't expecting, where it wasn't letting me save in the second CPT because the first was using the same variable name? I have noticed (via print_r calls) that my CPTs all contain ALL of the fields I've saved in any of them. Which seems a little odd to me, but I haven't dug into the specifics of how the data is stored, just trying to get the data out.
Originally Working File:
function save_championship(){
global $post;
update_post_meta($post->ID, "federations", $_POST["championship_federation"]);
update_post_meta($post->ID, "weightclasses", $_POST["championship_weightclass"]);
update_post_meta($post->ID, "genders", $_POST["championship_gender"]);
update_post_meta($post->ID, "type", $_POST["championship_type"]);
}
And the new, (now working) second file:
function save_roster(){
global $post;
update_post_meta($post->ID, "team", $_POST["roster_team"]);
update_post_meta($post->ID, "fedfilter", $_POST["roster_federation"]);
update_post_meta($post->ID, "wcfilter", $_POST["roster_weightclass"]);
update_post_meta($post->ID, "genderfilter", $_POST["roster_gender"]);
update_post_meta($post->ID, "alignfilter", $_POST["roster_alignment"]);
$fedtf = $_POST['showfed'] ? true : false;
update_post_meta($post->ID, "showfed", $fedtf);
$wctf = $_POST['showwc'] ? true : false;
update_post_meta($post->ID, "showwc", $wctf);
$gendertf = $_POST['showgender'] ? true : false;
update_post_meta($post->ID, "showgender", $gendertf);
$aligntf = $_POST['showalign'] ? true : false;
update_post_meta($post->ID, "showalign", $aligntf);
}
Originally, the second parameter of the *filter updates were "federations", "weightclasses", "genders", and "alignments". Those did not work. The filters do.
Help me understand?
EDIT: I have recently discovered the wordpress debug mode, and I've gotten some additional information on this.
Apparently, when I save one of my custom posts, it passes through all of my custom post pages (thus giving me a whole bunch of 'undefined index' errors on save). It finishes up with a 'cannot modify header information' for a different custom post type than the one I'm trying to save.
It seems likely that I'm doing something wrong here. Is there something common that I might be doing? I don't really feel like copying every file here would be beneficial, but if this is a common side effect of what I am doing, can anyone point it out?
Thanks.
Figured out what was going on. Apparently, using the hook save_post will cause that function to be called whenever you save ANY post, not just the custom post type you are creating. From what I've determined, there are two ways of fixing this.
First, the way I went, is to add
$post_type = get_post_type($post);
if ( "CPT" != $post_type ) return;
to all of my custom post type save_post functions. I've also read, but not tried, that you can replace the save_post hook with a save_post_cpt hook instead.
I can add and remove fields in the user profile section with:
function add_remove_contactmethods($contactmethods ) {
// Remove AIM
unset($contactmethods['aim']);
//add Phone
$contactmethods['phone'] = 'Phone';
return $contactmethods;
}
add_filter( 'user_contactmethods', 'add_remove_contactmethods' );
When I view this screen in the backend, the "Phone" field comes last, after some other fields like "Email" and "Website". I guess this is because my added field was added after the default Wordpress fields. How do I sort this, for instance alphabetically, so that my "Phone" field comes in alphabetical order instead of after the default fields? How do I sort the output of $contactmethods without messing it up?
try using ksort
function add_remove_contactmethods($contactmethods ) {
// Remove AIM
unset($contactmethods['aim']);
//add Phone
$contactmethods['phone'] = 'Phone';
ksort($contactmethods);
return $contactmethods;
}
add_filter( 'user_contactmethods', 'add_remove_contactmethods' );
re
UPDATE: So I guess the answer to my original question, is to explain why and how "Website" and "Email" are stored, and how the output is controlled in the backend when you view a profile. Maybe it's an ordered action? I guess "Website" and "Email" are just user meta, but how is the output order controlled. I accept that I might have to write a custom script to sort the output, I just don't know where to begin.
Your right about that, all the new contact fields were added into user_meta table. user_email and user_url are in the users table. The problem you are going to have doing this, is that a filter does not exist to modify the information. You can check the main filters here:
http://codex.wordpress.org/Plugin_API/Filter_Reference
and also you can look at core itself. All the admin templates are in wp-admin so you can look at the variable you need to modify in user-edit.php ($profileuser). Im in no way recommending this, but you could modify the template there, it will be overwritten on the next update of course so thats a drawback to it.
There may be a hook somewhere in admin in the load template process, if you could find one, you could relocate the template location to a theme file and recreate it with the changes you want. But all this seems like a lot of work to include just 2 fields to reorder?
Another approach is to use a higher priority the other when adding the fields. For example, Yoast adding 3 contact methods, and if you want your 'phone' appear before those, set the filter to:
add_filter('user_contactmethods', 'my_contactmethods', -5, 1);
Email and Website cant be re-ordered unless deep PHP coding or javascript re-order or advanced CSS.
if you know the key(s) (check the name fields in the source code), you can add other plugin fields by yourself, and choose exact appearance. A field is only added once if they added twice (!) This is how we use:
function entex_author_contactmethods($contactmethods){
$contactmethods['mail'] = __('Public email', 'entex-theme');
$contactmethods['phone'] = __('Phone', 'entex-theme');
$contactmethods['googleplus'] = __('Google+', 'wordpress-seo');
$contactmethods['youtube'] = __('YouTube URL', 'wordpress-seo');
$contactmethods['facebook'] = __('Facebook profile URL', 'wordpress-seo');
$contactmethods['instagram'] = __('Instagram URL', 'wordpress-seo');
$contactmethods['twitter'] = __('Twitter username (without #)', 'wordpress-seo');
$contactmethods['linkedin'] = __('LinkedIn URL', 'wordpress-seo');
$contactmethods['myspace'] = __('MySpace URL', 'wordpress-seo');
$contactmethods['pinterest'] = __('Pinterest URL', 'wordpress-seo');
return $contactmethods;
}
add_filter('user_contactmethods', 'entex_author_contactmethods', -5, 1);
Happy contact-ing!
So I have a post meta field that has two values, true or false. It is defaulted at false, and it'll automatically change to true when an event occurs.
Anyway, is it possible to create another meta field that records the exact time that this change occurs? If so, how would I go about doing this?
Well without modifying wordpress core or overriding the update_field() function in your theme functions.php you cant get in a smooth way the time when a record has been added to the database.
But what can i advice to you:
1) If your application highly depends on seconds:milliseconds when post_meta has been modified, you should use advanced technique: database transactions.
2) If your application are fine with a small delay between writing up 1st meta and the second just add it with add_post_meta() right below the update_field() performed
p.s If you think that overriding the function with functions.php is the way you prefer just ping me a comment, i'll try to help you with by editing the post.
Cheers!
EDIT
Right after your manipulation with allready_aired field performed
$alreadyAired = get_field('already_aired', $episodeID);
if (get_field('already_aired', $episodeID) == "no") {
update_field('field_28', "yes", $episodeID);
}
you can use add_post_meta($post_id, $meta_key, $meta_value, $unique) function in a way like this:
$alreadyAired = get_field('already_aired', $episodeID);
if (get_field('already_aired', $episodeID) == "no") {
update_field('field_28', "yes", $episodeID);
}
add_post_meta($episodeID, 'NEW_TIMESTAMP_FIELD', time(), true);
Then, you can call this NEW_TIMESTAMP_FIELD and use in a way you like (echo formatted date-string, compare 2 dates, etc.
Please note the 4th parameter of add_post_meta function. This one tell you that this field is unique and considering that app_post_meta does not affect "Update" actions, this bundle wont let this field get modified in the second time.