Home › Toolset Professional Support › [Resolved] View list with checkbox for select rows and update selected rows submit
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Hong_Kong (GMT+08:00)
Tagged: Toolset Forms, Views plugin
This topic contains 26 replies, has 3 voices.
Last updated by Luo Yang 5 years, 3 months ago.
Assisted by: Luo Yang.
I am trying to work out how i can have a list view with a checkbox on each row so a user can select which row need to updated using a cred form and would do a submit all selected and update a single field for each row and trigger an email notification for each select row updated.
Can this be done ?
How would i go about it ?
Thanks
Tony
Well, it can be done, but it cannot be done without any custom logic.
I'd approach this as follows:
1. Create a Post Form that lets you edit posts of that type. Insert it to a page and tell it to edit a specific (dummy) or even a post as passed by URL parameter.
Note, when inserting the form you are asked what post to edit, and there choose any post. Later, edit the ShortCode and pass the wpv-search-term of your choice to the post id Forms ShortCode attribute.
2. Create a list in a view that offers the checkboxes to choose the items - you can use this DOC as a starter: https://toolset.com/learn/create-an-ecommerce-wordpress-site/product-comparison/
Make sure the submit here leads to the page with the form as in #1 - and pass for example all ID's of the to be edited posts in a URL parameter, also pass one post to edit (the first in the checked rows) as the parameter you use for wpv-search-term when populating the Edit Form on the page.
3. Create a Custom Code, that when the form is submitted, updates all posts with ID as passed by the URL parameter as in #2 above.
This code should then hook to the Forms API cred_save_data() and update all posts with the given ID and the new data you pass (let's say a custom field).
For this you'll need to consult the API DOCs here:
https://toolset.com/documentation/programmer-reference/cred-api/
https://codex.wordpress.org/Function_Reference/update_post_meta
I can help only partially with this, but since I think it should be possible with WordPress and Toolset + some Custom Code, I can assist you along this process.
I'd suggest starting at #1 and ask me here in case you get stuck.
I also will enable a private reply in case you have a staging site you are working in I could access, then I can eventually (if required) set some examples for you.
OK , i will read the information you have sent me and come back to you, my staging site is not running currently , but i will look at that option as well
I have the application already running on our live site.. Just a couple of our testers ask if could select rows and bulk update.
We have not gone live yet on that app yet. but would be nice to add that feature in.
Let me know if you would like to connect and assist.
Thanks
Tony
I can surely assist with how-to information and debugging of things that do not work, inclusive set up some simple examples, however, I cannot prepare this at full for you - for that, you would need to for example hire a contractor https://toolset.com/contractors/
It's a project requiring some work both in structure and styling.
I can assist along the process, any doubt and issue can be consulted with us, however, we cannot set it up for you at full.
If you think that I should have backend access to assist you better, you can add so in the next reply.
From there we would have to go thru the steps I mentioned and each time you have questions or get stuck you are of course welcome to ask information from us.
Note, on live sites I prefer usually to not do any changes at all, so you would usually have to include some permissions when you request our help (like, "you are free to create new dummy views but please do not delete any" 🙂 )
Thanks!
It seems to be an invalid password.
"Error: Invalid Password."
Can you review it?
Also, can you add details as of where and how you implemented what, so I can find the relevant pages and codes/displays quickly and suggest improvements if required?
Thanks!
OK, so we have our list with the posts you want to pass to be edited here hidden link
That content is created here hidden link
In that view, I see you handle Delegate Group. That is a Repeatable Field Group. Note that those are posts, they can only be edited with a Post Form that applies to that type, and only one RFG a time each Form. Luckily Post Forms take API calls so we can still do what I suggested.
But in that View you also say "Select items from the Delegate Group group that are related to the Post with ID set by the URL parameter wpv-relationship-filter.
eg. hidden link"
That means, the hidden link will return ALL those posts and to have those posts filtered, you would logically need to add a Custom Front End Filter as well or delete this query.
Then, in the Views loop you correctly generate the Form which wraps all the loop items.
You pass the action [wpv-bloginfo show='url']/delegate-update-results/, which will lead the form's submit to hidden link passing ?post_ids[] param.
That Param is what we need to know in our custom code - it tells us what posts exactly we should update with the entered value.
Note, something that I am not sure you know is - with this method logically you will be entering the SAME data for SEVERAL posts (means, you cannot enter the data for MANY posts in ONE form. You can, however, pass ONE data sample to MANY posts).
So, this means on the results page hidden link we need to see a Form.
The Form lets us enter data in the parts of the post we want to bulk edit because that is what this is: bulk editing many posts.
But right now on hidden link is a View.
At this point I need to confirm with you:
- do you want to edit MANY posts adding ONE SAME value to all of them?
- do you want to edit MANY posts, each on their own, in one list?
If first, then you need to remove that View and insert a Form that edits the RFG Tye (Repeatable Post Field Post Type).
But in order for me to help further, you need to reactivate the password, as during the time I was logged in, it logged me out and now it's invalid again.
It's not possible for me to see the backend in this case.
Yes that's me
42.112.158.146, Ho Chi Minh City or Nha Trang, it depends a lot on where the local ISP decides to put my IP (no comment, the cities are not exactly "close").
I have difficulties loading your site, it is slow. Is that only here, or also there?
I think there is some code in the background throwing errors or completing very expensive tasks, as it feels very slow here in the admin and front end.
Other sites load fine, hence I can't yet confirm it is due to location, but maybe over there the site works fast as expected, hence being a local issue...
Often I got also a WPOD (white page of death) speaking for hidden PHP errors.
Ok then, according to your last statement you want to edit many posts with one dataset.
You will have to remove the View from hidden link in this case.
You will instead insert a Toolset Form to edit the Post Type "Repeating Field Group" of your choice.
You will probably need to create it first as I could not see a Post form to edit the RFG yet (in your website)
The form, when inserted to the page, should edit a given post.
Later you will edit that ShortCode to listen to the Search parameter you added (post_ids)
Please let me know once the Form to edit those posts is complete.
Insert it to a Page using a TEXT editor, not the Blocks or Widgets of page builders - it is important that we can edit the Forms ShortCode.
Once done, please let me know so I can show how to edit that ShortCode.
Hi Beda
Yes we have an importer called all import, which runs every hour on the hour, which imports the new planner events and also updates any changes and deletes.
It checks about 1000 records, I am still trying to determine if I need that much data processing every hour, as most of the changes are for events in the future.
That's a by the way.
I will set up as you instructed and come back to you,
Tony
Thanks
For this task, at least theoretically, it doesn't matter how the content is created initially.
Only how we edit matters (which is what you are about to set up now)
I'm here 1,5 more hours, then back tomorrow - just in case you wonder where've gone if I do not reply immediately 🙂
hidden link
I have removed the view from that page and replaced it with the shortcode for the form
Also I have made sure it's text editor
[cred_form form='update-selected-delegate']
Tony
Ok, now we see the Form produces an error (Form Type and post type do not match).
This is because if not passing a precise Post ID to edit the Form will try to edit the Current post and since it's a page, it throws the error.
Hence, in the Form ShortCode now you need to pass the Post to edit.
This is done with the GUI when inserting the ShortCode you choose "Another post" and in there simply select any of the posts offered, and then replace the Post ID it generates in the ShortCode with [wpv-search-term param="post_ids"].
This part ensures, that the Form Edits the one post passed in the URL parameters.
Note, I needed to edit the code a bit in the view - to make it work with the current setup.
Note also - yes, we pass MANY ID's to the post_ids parameter but the Form will accept only one, and since [wpv-search-term param="post_ids"] will return the last instance only, it will use that ID as the one to edit.
But it does not matter, because we will intercept this action and with Custom Code, propagate it to all Posts with ID's as set in the URL parameter.
The full shortcode is:
[cred_form form='update-selected-delegate' post='[wpv-search-term param="post_ids"]']
The View Code to generate the URL parameter was slightly edited:
<input type="checkbox" name="post_ids" value="[wpv-post-id]">
Now, when you head to hidden link, choose a few options, it will lead to something like hidden link
The Form will appear with the LAST of these Posts (RFG with ID 42813) prefilled in the form, ready to edit.
Now, we edit this and press submit, it will edit ONLY that one post, so we add a Custom Code, to propagate these changes to all the posts of all Post ID argument.
The code uses WordPress Logic (update_post_meta() in this example, on one single line field), and hooks to cred_save_data().
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/update_post_meta
We also create a Custom ShortCode, which we put in the Form (in an HTML block), it is used to save keep the URL parameters we passed to the page with the form.
So step by step, to complete this you have to:
1. Register a Custom ShortCode that gets the Query params:
function url_query(){ $return = '<input id="post_ids" name="post_ids" value="'.$_SERVER['QUERY_STRING'].'">'; return $return; //gives us something like post_ids=9&post_ids=11&post_ids=6 } add_shortcode('url-query','url_query');
Please study the code and functions used, it's important to understand them
2. Now display the new ShortCode [url-query] in the Form, in an HTML block, anywhere you want - you can wrap it in a DIV that makes it invisible with CSS For example)
This makes sure, the post_ids we pass in the URL, are IN the Form, as we this later for updating the posts.
3. Now hook your code, whichh gets the URL parameter values from the new ShortCode above, and updates all posts with that ID, with the value passed in the Form (field or whatever it is that will be edited).
This is an example that needs to be adapted to your case:
add_action('cred_save_data', 'my_save_data_action',10,2); function my_save_data_action($post_id, $form_data) { // if a specific form if ($form_data['id']==7)//Change to the Form ID you use here (it's 42847) { //get data to update with - you will need to change this to your custom field or fields, if you have many. $data_to_update_with = $_POST['wpcf-testing-single']; //get data from post_ids parameters in URL - no change should ne needed here unless the attribute changes (it is post_ids here) $url_params = $_POST['post_ids']; //remove & - we need this to later easily create an array of all post_ids passed //note, it would be wise to check here if you actually HAVE some post_ids or not, but this is a refinement to be done later $url_params = str_replace('&', '',$url_params); //split into array - the post_ids are many, so we need to ahve them each on it's own $url_params = explode('post_ids=',$url_params); //At this point we have an array of Post ID's we want to edit, as passed in post_ids url arguments, in an array //now we can update all the posts in that array! foreach ($url_params as $post => $id) { //update only if Post ID is not 0 if ($id != 0){ //update the field you need to update, in this case a single line Types field, with the data you got from the Form, see first lines of the code above update_post_meta($id, 'wpcf-testing-single', $data_to_update_with) } } } }
4. Now that you have the code in place, you can try to check a few Posts in your view, iht "edit" so it calls the bulk edit list, in where you should then see the Post Form and you can make an edit to the field passed in the code.
Then you submit the form and it should update all fields of all posts as passed in the View originally.
Note, the code above considers only ONE field. You would need to extend the code if it should address other fields too.
Please let me know if you need help at any point of this.
Hi Beda
I am getting a
Call to undefined function dd_action() on my mod of the code
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
dd_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==42932)//Change to the Form ID you use here (it's 42847)
{
//get data to update with - you will need to change this to your custom field or fields, if you have many.
$data_to_update_with = $_POST['wpcf-email-request'];
//get data from post_ids parameters in URL - no change should ne needed here unless the attribute changes (it is post_ids here)
$url_params = $_POST['post_ids'];
//remove & - we need this to later easily create an array of all post_ids passed
//note, it would be wise to check here if you actually HAVE some post_ids or not, but this is a refinement to be done later
$url_params = str_replace('&', '',$url_params);
//split into array - the post_ids are many, so we need to ahve them each on it's own
$url_params = explode('post_ids=',$url_params);
//At this point we have an array of Post ID's we want to edit, as passed in post_ids url arguments, in an array
//now we can update all the posts in that array!
foreach ($url_params as $post => $id) {
//update only if Post ID is not 0
if ($id != 0){
//update the field you need to update, in this case a single line Types field, with the data you got from the Form, see first lines of the code above
update_post_meta($id, 'wpcf-email-request', $data_to_update_with);
}
}
}
}
You had marked the ticket as resolved, but I reopened it as I see there are still some issues you mention.
You are receiving a Error because of a copy paste typo you have in the code due to what I posted here:
https://toolset.com/forums/topic/view-list-with-checkbox-for-select-rows-and-update-selected-rows-submit/#post-1298573
It should say add_action and not dd_action.
I have edited my snippet, however please acknowledge that the code cannot be used as a copy paste, but must be understood and modified to your site, otherwise it will lead to complications (eventually worse than the current reported)
Thank you and I extend again my apologies for the bad typo above in the code (fixed now).
We also provide a list of contractors (https://toolset.com/contractors/), which you can consult as well if you require custom coding assistance.
Thank you for understanding!
Hi Beda
I can select the delegates that i want to update the email request field.
It displays the Delegate Update Results form, which as you can see show me the post_ids it has passed.
I tick the email request and hit update it only updates the first one .
I think i am missing the last bit
Thanks
Tony
Sorry, I do not follow - this ticket is quite aged and I handle several issue a day, so often I forget what was done on other long/larger issues when time passes.
Can you please explain your issue again, because it is several months since the start of this problem, and meanwhile we processed to further complex situations I see.
I understand everything works now, but you seem to want to select many items.
I guess it's in my custom code, but I can not even find the page to start with on your site anymore (I guess development went ahead meanwhile 🙂 )
Could you recapitulate where I find what so I can help with this?
It would at this moment maybe be of help having a local copy as well, if you can provide me with one:
https://toolset.com/faq/provide-supporters-copy-site/
I can then check what's wrong in the code (I suspect I miss to loop over each of the instances but as said, I first need to wrap my head around this again)
Thank you for understanding.