php shortcode function to add other sum other shortcode values - php

im doing some web designing with wordpress at the moment.
I have created a page with a web form where a client can input some information (name, email, invoice number, price, gst, etc).
I used a plugin called contact form 7 to provide the web form, when the user inputs all of their information, the plugin then emails an html formatted invoice to my email address, with the fields occupied by shortcodes which take on the value from the web form.
For example, in the name field of my invoice table, i enter [first-name] and the name the user inputs gets emailed to me in the invoice.
I need to find a way to sum the value of two of the other fields, one of the fields on my invoice form is called total, which should be equal to [gst]+[price] that the user inputs on the web form.
Ive tried to look for a php shortcode function that can take its arguments as the values of other shortcodes, but havent had any luck. I have never really used php before either so wouldnt know how to write one.
I managed to find this, which doesnt seem to work, as when the email comes through all i see is [sumsc][gst] [price][/sumsc]
add_shortcode('sumsc','sumsc_func');
function sumsc_func( $atts, $content = null ) {
$sum=0;
$content=str_replace(array(" ","] [","]["),array(" ","][","]|["),$content);
$codes=explode("|",$content);
foreach($codes as $code){
$sum+=do_shortcode($code);
}
return " <div>".$sum."
</div>";
}
Any help would be greatly appreciated, I also need to the functionality to be able to multiply a shortcode value by a certain number. For example the price needs to be divided by 0.6 to give the quantity which appears on the invoice. Thanks!

Create a hidden field in the form say with a unique id say it fields_sum.
Use jquery on that page and catch form submission event. Get values of those fields, sum it and set the result to fields_sum field. And then use field shortcode where you want.
jQuery script would be something like (Algorithm)
$(document).ready(function(){
$("your_form_id").submit(function(e){
// get value of field one
// get value of field two
// sum them
// set sum to the hidden field
});
});
I hope this works and enough to get a solution like this.

Related

Delete ACF user profile repeater field row from user account (WordPress)

I am using ACF repeater field for user profiles, which shows like this in the user profile page:
That's cool.
But on the front-end I have a form that I want to use to delete a specific row. My custom form simply lists all the rows with radio buttons, and so if the user selected number 3 and submitted the form then I want to delete the third row (in this example, the row with Catherine Davies would be deleted).
The form works fine in that it submits as expected and returns the value the user selected, but my code to delete the row that was selected doesn't seem to work.
The ACF documentation seems a little vague on the subject. Based on a combination of the ACF doc and this StackOverflow post, I expect this code to work but it does not:
$user_ID = get_current_user_id();
$field = 'extra_user_info'; // Name of the repeater field
$row_to_delete = $_POST["row_to_delete"];
delete_sub_row($field, $row_to_delete, 'user_' . $user_id);
Just to be sure, even if I hardcode the $row_to_delete variable to any number (from 1 to 4) it still does not delete any row.
Just to clarify, I wish to delete an entire row.
Note: I realise I could just embed the ACF form on the front-end, but for reasons I won't go into this is not an option, hence using my own custom form.
There could be another issue but in your example at least, your $user_ID variable has capital letters, while the variable passed to delete_sub_row() is lowercase. PHP variables are case-sensitive.

Wordpress Gravity Forms Serialized List

I've got a List field with Gravity Forms to populate some custom fields in a custom post type. The site is a recipe submission site, and I'm looking for users to be able to add ingredients individually for better SEO. My issue is, when I submit the form only the last input field under ingredients is passed to the recipe.
I know I need a serialized list as this custom field pulls an array, but I'm at a complete loss of how to do that. The array should read something like this
a:8:{i:0;s:26:"4oz piece of salmon/person";i:1;s:12:"1 egg/person";i:2;s:37:"1-2 multi-colored bell peppers/person";i:3;s:12:"Greek olives";i:4;s:9:"Olive oil";i:5;s:13:"Salt & Pepper";i:6;s:22:"Basil (fresh or dried)";i:7;s:0:"";}
I don't even know where to begin in putting together a serialized array for one form field, so any nudge in the right direction is greatly appreciated.
Unfortunately Gravity Forms is configured to store these as separate meta records. One option is to customize the Gravity Forms forms_model.php file, create_post function, which unserializes the field contents and loops through each item to create a new post_meta record.
The following code should replace the case for field type list, and will prevent the creation of individual meta records on a predefined array of Gravity Form fields:
case "list" :
$skipped_list_fields = array('<meta name for field to skip unserializing>',
'<meta name for another field to skip unserializing>');
$value = maybe_unserialize($value);
if (in_array($meta_name, $skipped_list_fields)) {
if(!rgblank($value))
add_post_meta($post_id, $meta_name, $value);
} else {
if(is_array($value)){
foreach($value as $item){
if(is_array($item))
$item = implode("|", $item);
if(!rgblank($item))
add_post_meta($post_id, $meta_name, $item);
}
}
}
break;
Jason, someone had a similar situation https://stackoverflow.com/questions/20591802/how-to-save-comma-separated-inputs-in-gravity-forms-to-a-global-array-on-form-su - I think a similar edit in functions.php in your theme is the direction...
With a Custom Field form field configured as a List type and having filled out the form like this, I'm seeing all the ingredients as separate post meta items (but all attached to the same key) like this.
Is this different from what you are seeing or are you trying to achieve something different?
Use the 'List' field type from the 'Advanced Fields' area when creating/editing your form. That data is already serialized and saved as an array in a single field. Just use the gform_after_submission hook to save the GF field as postmeta.

How to tell if a user has updated a profile field in Drupal 7

I am trying to determine how in drupal 7 I can programmatically tell if a user has updated a custom field in their profile within the past 24 hours however looking through the tables I can't seem to find any field with that information does anyone have any ideas?? Essentially I need to pull this information out during a batch job that run onces a night and updates other systems based upon the information in drupal. If someone can think of a better way todo this it would be appreciated. thanks for any help you can provide
ok i dont know if its working or not try the following steps
1- first you should tell drupal....when user edit the profile page do something...(whatever the something is ..insert something to database..retrieve something..etc)
/**
* Implementation of hook_form_alter
*/
function newcontent_form_alter(&$form, &$form_state, $form_id){
if($form_id == 'user_profile_form'){
$form['#submit'][] = '_do_something_when_user_save_profile';
}
}
/**
* do something when user save profile
*/
function _do_something_when_user_save_profile($form,$form_state){
// do something
}
now i think we need to do some queries ... first we need to create 2 tables
http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_schema/7
(this url will help you to create your schema)
the first table will contain the current value of the field u want track so i think this table should have the following fields
(primary key , user id, field name, field value)
the second table will contain the date of the last upated operation the user made
i think the fields will be like the following (primary key,user id , field name, date)
now lets return to the form submit function
/**
* do something when user save profile
*/
function _do_something_when_user_save_profile($form,$form_state){
// now i can check in the first table to see
// is the current submitted value for this field = the value in the table
// if its = the table value then the user didn't change any thing now i dont
// need to update the second table
// if the current submitted value != the value in the table then its mean
// that the user have updated this field ...
// now i should insert new value to the second
// table with the current date time and the current field value
}
i hope u under stand me and sorry for my bad english
maged adel provided reasonable solution, however there is one more way of doing this:
you can use hook_user_update for this purpose. It's better that solution provided above because of 2 reasons:
1) you have both $edit - entered values and $account - user account values, so you can compare and get know what fields being updated (if you don't have 1 specific field to check).
2) just 1 hook to implement.

How to handle $_POST results of autosuggest multi-input on form

I have the following JsFiddle to help with my question:
http://jsfiddle.net/eY2yH/3/
I am ultimately trying to create a simple input field which allows a user to enter multiple 'names' which creates a message to those users on our system. In many ways, this should function EXACTLY as Composing an email in Gmail (enter text for a user name/email, the autosuggests appears, then allow for additional input or submit).
When I run the code given, the results of the $_POST are quite strange.
Depending on which members are selected, the value of the key changes:
array([as-results-0XXX]=>1,2,3,4)
where XXX varies and 1,2,3,4 would represent the value of the members selected. These values are stored in a single element and separated by comma. In the end, I want to run through a loop for each user selected and send the necessary alert:
If 3 members are selected, e.g.
foreach($rows as $row) {
do table insert with particular value
}
My site is built with php and clearly using jquery. I have done a bit of searching and can find nothing regarding multiple inputs on the same autosuggest field.
You can change the way the name of the hidden input is determined by adding the asHtmlID attribute to your .autoSuggest():
$("input.autoSuggest").autoSuggest(data.items, {
selectedItemProp: "name",
searchObjProps: "name",
asHtmlID: "custom_id"
});
The variable posted to your php script will be available through $_POST['as_values_custom_id']

can I get help on DOB validation on magento

I am new in magento I would to validate my DOB fileds on the registratoin page so that valid dates can be inserted by the user, at the moment the input fieds accept anything and a user can input any number of text and it accepts can you guys please help?
Look in your theme's (or default) template/customer/form/widget/dob.phtml file. It creates three strings which describe the three text inputs for day, month and year. You can either add the validate-number name to their class or rewrite them to use <select> elements instead.

Categories