So I have a form that has makes the action jump to another php page.

There my php code tries to upload a post. I get the following error:

Fatal error: Call to undefined function wp_insert_post()

PHP Code:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "upload_ugc") {

if($_POST["editindication"] == 'newpost'){
$post_array = array(
    'post_title'    => $_POST["post_title"],
    'post_content'  => '',
$post_id = wp_insert_post( $post_array, true );
  • So what is actually the question? IMO the error says it all.... Apr 3, 2016 at 11:54
  • Why can't i use the function you think?
    – mesqueeb
    Apr 3, 2016 at 11:55
  • because you haven't included the file in which it is defined. To make things easier, please edit the question and explain what is it that you are actually trying to do. I guess it is AJAX, but better to hear it from you Apr 3, 2016 at 11:56
  • Oh i see, i'll try and search which file that is! If you know, feel free to post it as an answer!
    – mesqueeb
    Apr 3, 2016 at 11:57
  • hmm, I have updated my previous comment, please read it Apr 3, 2016 at 11:58

1 Answer 1


If you'd like to use WordPress functions inside your naked PHP file, you'll have to load the WordPress environment:

require_once '/path/to/wp-load.php';

That said it's best practice to NOT handle things via separate PHP files like that outside of WordPress, but use a plugin instead and fire your code on an appropriate action. You can use a page template or a shortcode to render your form, and then catch the form input on the init or template_redirect hook from within a plugin:

add_action( 'init', function() {
    if ( empty( $_POST['action'] ) || $_POST['action'] != 'upload_ugc' )

    // Your wp_insert_post statement
    wp_redirect( home_url( '/upload-success/' ) );

Also, when working with user input, don't forget about security, especially for logged in users. In your case you'll need a wp_nonce_field at least, and probably run your post title and content through wp_kses.

  • So basically if I put what I want to do into a "plugin" I wouldn't need to require_once '/path/to/wp-load.php'; Is that what you are saying?
    – mesqueeb
    Apr 4, 2016 at 1:54
  • Kovshenin, I tried adding the php that makes a post from my form as an plugin. But I'm not sure where to guide the "action" of the form now. I asked it here, if you have some spare time, it would be great if you could have a look! →wordpress.stackexchange.com/questions/222562/…
    – mesqueeb
    Apr 4, 2016 at 4:34
  • If you're handling the form on init then pretty much any URL will do, for example home_url( '/my-form/' );
    – kovshenin
    Apr 4, 2016 at 8:02
  • What do you mean by "handling the form on init" ?
    – mesqueeb
    Apr 4, 2016 at 9:59
  • I mean add_action( 'init', function() { // form handler });
    – kovshenin
    Apr 4, 2016 at 10:00

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.