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' )
return;
// Your wp_insert_post statement
wp_redirect( home_url( '/upload-success/' ) );
die();
});
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.