0

I've added a custom page template where I pull out a list of available jobs from the database.

The page displaying the jobs is called jobs.

I'm using the Ninja forms plugin to add a form on a page called apply.php, which has fields for a job application.

Now for each job title offered on the jobs page, I want it to be a link that the user can click on to apply, and be redirected to apply.php, where I can grab the job id and save in a hidden field on apply.php.

This is so that when I save the applicant's submission, I can get it saved together with the job they applied to.

I've added the link alright - as follows:

?page_id=18&jid=2

where 18 is the id of the apply page, the page that I've got the ninja form on.

But how can I grab the jid from the query string and use it in the ninja form fields?

closed as off-topic by birgire, Pieter Goosen, fuxia Aug 16 '14 at 0:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2

It looks like you can use the ninja_forms_display_init action to populate a field.

To get the job ID from the URL you mentioned above you can use the $_GET array.

Adapting the code from that documentation page, something like this should achieve what you're looking for:

function wpse_158000_populate_field($form_id) {
    global $ninja_forms_loading;

    $job_id_field = 3; //put the id for your hidden field here

    //Change the value that a field is pre-populated with.
    $ninja_forms_loading->update_field_value($job_id_field, $_GET['jid']);
}

add_action('ninja_forms_display_init', 'wpse_158000_populate_field');

Their documentation lists a number of other actions you can tie into.

If you're into using cleaner URLs you might want to hook into the rewrite_rules_array action and then you can have a URL more like http://www.example.com/apply/2 where 2 is the ID of the job they are applying for and apply is the slug of your apply page.

You could also push this farther and use the slug for that job instead of the ID for a nicer URL and then get the ID for that job via the slug and add that ID to your hidden field's value.

  • 1
    I have a question, what are these scary numbers about in the function names? – user961627 Aug 15 '14 at 16:35
  • When I submitted the form, I got this error on the next page: Fatal error: Call to a member function update_field_value() on a non-object in C:\xampp\htdocs\xampp\wordpress\wp-content\plugins\custom-query-vars.php on line 38 --> this is referring to the last line in the function wpse_158000_poulate_field Any idea why? – user961627 Aug 15 '14 at 16:46
  • @eric-allen This was the solution! Thank you. Man, the docs suck... couldn't find this information for the life of me, but I am admittedly a novice when it comes to WordPress. Cheers! – Volte Jul 22 '15 at 16:47
1

@Eric Allen's answer worked perfectly.

However, I'd gotten in touch with the Ninjaforms folks too, and their answer worked as well, so I'm posting it here too.

They referred me to this: http://docs.ninjaforms.com/customer/en/portal/articles/1981032-filters-ninja_forms_field?b_id=9172

So adapting it to my problem, the solution was this:

function filter_fetch_jid( $data, $field_id ){

    if( $field_id == 27 ){
        $job_id = $job_id = $_REQUEST['jid'];
        $data['default_value'] = $job_id ;
    }
    return $data;
}

add_filter( 'ninja_forms_field', 'filter_fetch_jid',  10, 27);
  • Kept getting a non-object fatal error until tried this solution. With AFC, a little tweak fixed everything. function filter_fetch_jid( $data, $field_id ){ if( $field_id == 21 ){ global $post; $agent_email = get_field('email_address', $post->ID); $data['default_value'] = $agent_email ; } return $data; } add_filter( 'ninja_forms_field', 'filter_fetch_jid', 10, 27); ?> – Faye Nov 27 '15 at 18:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.