session_start(); } function insert_question($postdata, $status = 'publish') { if (empty($postdata['content'])) { wp_safe_redirect(home_url('/submit-idea?e=con')); exit; } if (is_user_logged_in()) { if (isset($postdata['post_recaptcha']) && $postdata['post_recaptcha'] == '1') { // check recaptcha first $currentTheme = wp_get_theme(); $themeName = str_replace(' ', '_', strtolower($currentTheme->Name)); $opt = wp_load_alloptions(); $up_opt = unserialize($opt['up_themes_' . $themeName]); if ($postdata["recaptcha_challenge_field"]) { $resp = recaptcha_check_answer($up_opt['recaptcha_private_key'], $_SERVER["REMOTE_ADDR"], $postdata["recaptcha_challenge_field"], $postdata["recaptcha_response_field"]); if (!$resp->is_valid) { $_SESSION['recaptcha_error'] = $resp->error; $_SESSION['post_content'] = $postdata['content']; $_SESSION['post_title'] = $postdata['post_title']; wp_safe_redirect($postdata['redirect_to']); exit; } } } $user = wp_get_current_user(); // status should be draft for contributors/subscribers if (!empty($user->roles) && is_array($user->roles)) { if ((in_array('subscriber', $user->roles) || in_array('contributor', $user->roles)) && !current_user_can('moderate_comments')) { $status = 'pending'; } } if (strlen($postdata['content']) < 50) { $postTitle = substr($postdata['content'], 0, 49); } else { $postTitle = substr($postdata['content'], 0, 49) . "…"; } $post = array('comment_status' => 'open', 'ping_status' => 'open', 'post_author' => $user->data->ID, 'post_content' => $postdata['content'], 'post_status' => $status, 'post_title' => $postTitle, 'post_type' => 'question'); $id = wp_insert_post($post); // Update user's address if given update_user_meta($user->data->ID, 'address', $postdata['op_address']); unset($_SESSION['post_content']); unset($_SESSION['post_title']); unset($_SESSION['tag']); // Need to call do_action("init") for custom post types to work: taxonomies, permalinks etc. // WP_Rewrite may be not initialized yet global $wp_rewrite; if (!is_object($wp_rewrite)) { $wp_rewrite = new WP_Rewrite(); } do_action('init'); if (isset($postdata['question_category'])) { $val = wp_set_object_terms($id, array((int) $postdata['question_category']), 'question_category'); } // WP error - invalid taxonomy. For whatever reason is this.. if (!empty($val) && is_object($val)) { if (!function_exists('questionposttype')) { include_once dirname(__FILE__) . '/question-post-type.php'; } // register the post types again, although do_action('init') should have done that already. questionposttype(); // run wp_set_object_terms again. wp_set_object_terms($id, array((int) $postdata['question_category']), 'question_category'); } $post_tags = explode(',', $postdata['tags']); foreach ($post_tags as $tag) { // append tags with "true" param wp_set_object_terms($id, $tag, 'question_tag', true); } // status='draft' means it's a draft post for payments; return ID if ($id > 0 && $status == 'draft') { return $id; } // pending - for subscribers/contributors, go to thankyou page if ($id > 0 && $status == 'pending') { $paypal = get_option('paypal'); if ($paypal == 'yes') { return $id; } else { wp_redirect(get_bloginfo('url') . "/?p=" . get_option('thankyou_page_id')); exit; } } if (!empty($id) && $id > 0) { wp_redirect(get_bloginfo('url') . "/?p={$id}"); exit; } } else { $_SESSION['post_content'] = $postdata['content']; $_SESSION['post_title'] = $postdata['post_title']; $_SESSION['tag'] = $postdata['tags']; wp_redirect(get_bloginfo('url') . "/?p=" . get_option('signup_page_id')); exit;
/** * Flushes rewrite rules on plugin activation to ensure portfolio posts don't 404 * http://codex.wordpress.org/Function_Reference/flush_rewrite_rules */ function questionposttype_activation() {