function video_process_form( $query ) {
if ( $query->is_page( 'submit-video' ) && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
$output = null;
if ( ! function_exists( 'wp_handle_upload' )) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
$file = $_FILES;
// Do some minor form validation to make sure there is content
if ($_POST['title'] != null && $_POST['title'] != 'Video Title') {
$title = $_POST['title'];
} else {
$output .= 'Please enter a video title<br/>';
}
if ($_POST['description'] != null && $_POST['description'] != 'Video Description') {
$description = $_POST['description'];
} else {
$output .= 'Please enter a description<br/>';
}
if ($_POST['video_url'] != null && $_POST['video_url'] != 'Youtube URL') {
$vid_url = $_POST['video_url'];
$video_id = getVideoId($vid_url);
if ($video_id) {
$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $video_id);
if (strpos($headers[0], '200')) {
$video_url = $vid_url;
} else {
$output .= 'This is not an existing youtube video<br/>';
}
} else {
$output .= 'This is not an existing youtube video<br/>';
}
} else {
$output .= 'This is not an existing youtube video<br/>';
}
$tags = $_POST['post_tags'];
// Add the content of the form to $post as an array
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'tags_input' => array($tags),
'post_category' => array(12),
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => fod_videos // Use a custom post type if you want to
);
// If No Errors, Save Post and Redirect //
if ($output == null) {
$pid = wp_insert_post($new_post);
update_post_meta($pid,'video_code',$video_url);
wp_redirect( get_permalink($pid));
// If Errors, Return Errors for Display in Template //
} else {
echo $output;
}
}
do_action('wp_insert_post', 'wp_insert_post');
}
add_action( 'pre_get_posts', 'video_process_form' );