0

I have three files: submit.php (a page), validation.php, script.php

I am trying to use the fields as a front end submission area via the wp_insert_post() function.

However, I don't know exactly where to put the function, as where I try either is before the validation.php file or if I put it in that file it causes an error since it can't pass wordpress functions.

I have tried playing with the success in the $.ajax but since the die() functions all result in it being successful can't trigger a create post.

Does anyone have a better solution?

In the submit.php I have a form:

<div class="" id="response"></div>
<form id="form_ticket_submit" method="post">
    <label for="form_ticket_subject">Subject of issue</label>
    <input id="form_ticket_subject" name="form_ticket_subject" type="text">
    <label for="form_ticket_content">Post</label>
    <textarea id="form_ticket_content" name="form_ticket_content"></textarea>
    <label for="form_ticket_tax_stage">Select Stage</label>
    <select id="form_ticket_tax_stage" name="form_ticket_tax_stage">
        <option value="">Select one</option>
        <?php foreach ($form_ticket_tax_cat_stage as $form_cat_stage) { echo '<option value="' . $form_cat_stage->slug . '">'. $form_cat_stage->name . '</option>'; } ?>
    </select>
    <input hidden="hidden" id="form_ticket_meta_date" name="form_ticket_meta_date" type="text" readonly="readonly" value="<?php echo get_date_from_gmt( date( 'Y-m-d H:i:s' ), 'jS \of F, Y H:i:s' ); ?>">
    <input hidden="hidden" id="form_ticket_meta_user_name" name="form_ticket_meta_user_name" type="text" readonly="readonly" value="<?php echo $form_ticket_current_user_name; ?>">
    <input hidden="hidden" id="form_ticket_meta_user_email" name="form_ticket_meta_user_email" type="text" readonly="readonly" value="<?php echo $form_ticket_current_user_email; ?>">
    <input name="submit" type="submit" value="Submit Ticket" />
</form>

In the script.js I have:

<script>
    $(function(){
        $("#mb_ticket_submit").submit(function(a){
            a.preventDefault();
            $.ajax({
                method: "POST",
                url: "validation.php",
                data: {
                    mb_ticket_subject:          $("#mb_ticket_subject").val(),
                    mb_ticket_content:          $("#mb_ticket_content").val(),
                    mb_ticket_tax_stage:        $("#mb_ticket_tax_stage option:selected").val(),
                    mb_ticket_tax_application:  $("#mb_ticket_tax_application option:selected").val(),
                    mb_ticket_meta_date:        $("#mb_ticket_meta_date").val(),
                    mb_ticket_meta_user_name:   $("#mb_ticket_meta_user_name").val(),
                    mb_ticket_meta_user_email:  $("#mb_ticket_meta_user_email").val(),
                },
                success: function(a){ $("div#response").show().html(a); },
            });
        })
    });
</script>

Then finally the validation.php has the following (the $var are cleaned up, and validated from their $_POST[]:

    if( empty($form_ticket_meta_user_name) )  { die( 'Doesn\'t look like you have logged in properly'; ) }
    if( empty($form_ticket_meta_user_email) ) { die( 'Doesn\'t look like you have logged in properly'; ) }
    if( !empty($form_ticket_meta_user_email) ){ if( !filter_var($form_ticket_meta_user_email, FILTER_VALIDATE_EMAIL) ) { die( 'Doesn\'t look like your email address is formatted properly. Contact your administrator.' ); } }
    if( empty($form_ticket_subject) ) { die( 'No subject' ); }
    if( empty($form_ticket_content) ) { die( 'No content' ); }
    if( empty($form_ticket_tax_stage) ) { die( 'No stage'); }
    if( empty($form_ticket_tax_application) ) { die( 'No app' ); }
    if( empty($form_ticket_tax_priority) ) { die( 'No priority' ); }
    if( empty($form_ticket_tax_location) ) { die( 'No location' ); }

Thanks :)

2 Answers 2

0

You can achieve the goal through many ways. though the way you have selected is not an appropriate one but you can go with it also. just include the file wp-blog-header.php file (placed at the wordpress root) into validation.php above all other code. you can find all the wordpress related functions will work like a charm.

2
  • Please merge your two accounts.
    – cjbj
    Commented Sep 2, 2016 at 14:05
  • How would you suggest I go about this? The above code was one I took from a previous theme I was making, where it would use the $_POST to form an email. So taking that, thats how I build to this. I originally had no external validation.php file, and before the head would get_template_part() which linked to the wp_post_insert(). Would you suggest I move everything back in there? Or how would you see it being improved?
    – markb
    Commented Sep 2, 2016 at 22:09
0

To fix this, I removed the URL from the $.ajax.

Next, I added the validation.php before the <head> section. That depends on how your theme is made (mine was placed before a get_template_part())

Calling it here allows wordpress functions to be called, allowing the wp_insert_post to be called and utilised. Also, if you are making it completely notification forward, you can utilise the wp_mail after the post has been successful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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