Introducing hidden fields during runtime
-
I want to create associate a custom Post Type (as parent) to every single post( from USP forms).
I am looking for an approach that will pass on the parent post ID when the USP form is submitted.Here are the approaches I am having difficulty with:
1) Using one of the USP hooks that will insert a hidden input field
2) Creating Custom fields that can be introduced as hidden dynamically during render time of every single form.Please let me know what approach to use, and an sample code snippet to guide me?
Thank you
-
prase_request doesnt seem to work. Could you please help identify where is the issue:
This doesnt seem to be working, and I am unable to save metadata upon saving of the USP post.add_action( 'save_post', 'added_new_post',1,3); function added_new_post( $post_id, $post, $update ) { if ( wp_is_post_revision( $post_id) || $update ) return; if (isset($_POST["usp_form_submit"])){ if (get_post($post_id)->post_type == 'post') { $blog_parentID = $_POST['blogparentpost']; $parent = get_post_meta($blog_parentID ,"wilcity_my_posts"); $aparent= unserialize($parent); $aparent[0][] = $_POST['usp-post-id']; update_post_meta($blog_parentID, '"wilcity_my_posts"',serialize($aparent) ); } } }-
This reply was modified 6 years, 8 months ago by
nomairkashif.
Hi @nomairkashif, I can’t provide support for custom code, but just looking at your example, I don’t see anywhere “prase_request” is being used.
Parse_request is query filter, and I tried that too as per your recommendation.
It doesn’t have access to $POST variables
parse_requestcertainly has access to POST variables; the plugin uses it to process all submitted posts (i.e., POST vars).I cant seem to access the $_POST variables.
Am I doing this right? What is missing?
add_action( 'parse_request', 'added_new_post' ); function added_new_post( $query ) { if (isset($_POST["usp_form_submit"])){ if (get_post($post->ID)->post_type == 'post') { $blog_parentID = $_POST['blogparentpost']; $parent = get_post_meta($blog_parentID ,"wilcity_my_posts"); $aparent= unserialize($parent); $aparent[0][] = $_POST['usp-post-id']; update_post_meta($blog_parentID, '"wilcity_my_posts"',serialize($aparent) ); } } var_dump($_POST); return $query ; }You need to set a lower priority for
add_action().This works:
add_action('parse_request', 'added_new_post', 0);USP uses
1for priority, so you need to use0so it fires before the USP function.Thanks . tried that. It is getting called, but no $_POST appears, nor can I access the global $post->ID either
-
This reply was modified 6 years, 8 months ago by
nomairkashif.
I don’t know then, because this works for me when added to my theme functions.php:
function added_new_post() { die(var_dump($_POST)); } add_action('parse_request', 'added_new_post', 0);Also keep in mind that POST variables only exist when the form is submitted. If that hook/function is not providing complete POST variables, there may be something else that is interfering.
-
This reply was modified 6 years, 8 months ago by
Jeff Starr. Reason: typo
The POST submitted is redirected with 302 status, and on the next page, there is GET and $_POST is lost!
you are not testing the exact same scenario im talking about. When a user click ‘submit form’:
1) Is the parse_request hook invoked: YES
2) Then is $_POST available then?: NO!I want to access the $_POST before the redirect, if possible? Is the new blog post even created that early?
In other words, how do I do additional processing of the post, when user submits the form?
-
This reply was modified 6 years, 8 months ago by
nomairkashif.
I have no idea what you’ve got going on, but this works for me:
1) Add this code via theme functions:
function added_new_post() { die(var_dump($_POST)); } add_action('parse_request', 'added_new_post', 0);2) Fill out the form and click the submit button
The result is a dumped array of POST variables. This works every single time I try it.
Note that the form must be filled out (at least one field) or the POST variable may be empty.
Let me try this…
-
This reply was modified 6 years, 8 months ago by
nomairkashif.
looks like its working now. Not sure what happened.
BUT, the i still dont get the post ID of the blog post that was just created.Please help. How do I get the blogpost ID here? I believe that actual blog post has not been created yet.
Correct, the post has not yet been created at
parse_request. You may need to try a different route, depending on what you are trying to do.I’d want to create a session level or other global variable to store this post, and then retrieve it after USP blog post has been created.
If I take this route, can I use save_post hook to get post id of the created blogpost ? What hook would I use otherwise?
Yeah I’m not sure, it may require accessing the post after it is created. Again, please understand that I can only answer questions about existing plugin functionality. Custom code is not something that is supported with the plugin.
-
This reply was modified 6 years, 8 months ago by
The topic ‘Introducing hidden fields during runtime’ is closed to new replies.