![]() |
I have a political website which displays candidates and their responses to a survey. Candidate records are initially stored in a CPT called people, and need to exist from the moment the survey is sent out. At the same time, they may then complete survey results which need to be approved/verified before the results show publicly.
To that end I’ve setup a front end form which fires insert_post to create a new entry in a “temporary” CPT called pending.
What I’m looking to do now is as follows:
- create a new meta box (easy) with a button to “merge”
- this button would need to run a function which copies all the newly collected data (survey response meta field values, featured image) to the original “person” post for the candidate.
- the post ID I need to merge into is also available as a custom field called “sourcepost”
- ideally once this merge process has happened, the “response” post will now move directly to the trash since the responses have been approved and merged into the real record.
While I know this probably relies on update_post to do the heavy lifting, my Ajax skills are not great so I’m unsure what the best way to package all this up and fire the action from a post edit screen (as well as how to post all the changes from the currently view post.
Solution - 1Hello Andrew, Cam you please show me these live in action somewhere? I mean if you can share url etc. Also please share code that you are using so far. Solution - 2I would suggest adding a button to the "pending" cpt, with an AJAX call to run the "merge" logic. Then I would avoid auto-bin, because when the ajax comes back successful you can double check the "person" post and manually bin the "pending" post you just merged. it's safer and it's an easy flow. this good tutorial explains step by step how to make a mini plugin which adds a button connected to an ajax call: https://www.sitepoint.com/how-to-use-ajax-in-wordpress-a-real-world-example/ the tutorial basic structure is basically the same as what you need. with these differences: 1- the tutorial adds the button to the frontend content, you need it in backend. so instead of using these 2 hooks you should add the button to this hook: 2- then you also have to load the scripts to the admin. And you don't need to load any styles for your purpose. So at the beginning, remove the register/enqueue related to the styles and then modify this hook: so that it's like this: and you could also just put the register_scripts() and wp_enqueue_scripts() in the same function enqueue_rml_scripts() so you can tidy up a bit those things 3- also modify the function for printing the button so that it adds the source post id as well as the current post 4- modify the js so that it pulls the ids from the button and pass them to ajax. (and also change the ajax action name to something that makes more sense for you) 5- modify the php action hook entirely. Here is where your plugin will be completely different, but it's basically just one function. first modify the action name and the function name rather than using "read_me_later": then modify the function read_me_later so that it looks something like this: 6- Skip the part related to the widget 7- Please do follow the instruction at the end of the tutorial, regarding how to make the ajax call more secure and check nonce referrer etc... And go back and modify my sample code accordingly. I would suggest to change the naming of functions so that they make sense for you and they don't have the rml_ prefix. I haven't changed them in most of my sample code so that you can easily refer to the tutorial. Then I think the tutorial is missing a bit where it tells how to actually run-up the logic, because I haven't seen anything that fire the run() method. In order to do so, you should add something like this at the bottom of your plugin file, after the main class is closed: (assuming you changed the name of the class from ReadMeLater to MergeSurveyToPerson )
Solution - 3can you please provide code you already have and share few screenshots how it's working right now. It will be great if you can share login details to in personal message to check everything. Solution - 4Hi, Why don't you use the "pending status" on the original people post type when you first create it ? That way, the new entry would be immediately a people post type that may be reviewed and validated or rejected from the back-end by any editor with the right to do so :) |
Wordpress |
Type: | Code Example |
Category: | Coding |
Sub Category: | Code Example |
Uploaded by: | Admin |
Views: | 22 |