Q&A: Add link to 'thank you' page after user submits question

Hello,

We would like to encourage users who ask questions using the Q&A plugin to share their questions with their peers. We only have time to answer one question per day, and so far we're getting about 10 / day.

We determine which questions to answer based on vote count.

To make it easier for users to share their question, we'd like to include either a shortcode or a function in the page template for the Q&A 'thank you' page which includes the link of the user's question, so that they can simply grab it and share, vs. needing to reload the questions home page.

Does this functionality exist in the plugin? If not, can you help me extend the functionality to accomplish this?

Thanks,
Paul

  • Jack Kitterhing

    Hi there Paul,

    Hope you're well today, sorry about the delay.

    I've been checking into this and have found some info that might help, there's a filter called qa_get_question_link

    Then there's also a function of get_question_link on line 357 of template-tags.php.

    That should work for the questions links, but the thank you page is proving a bit more tricky here, the redirect to the thank you page happens on line 278 of edit.php

    Perhaps that would work for you? Or it looks for the slug thank-you, you could overwrite the thank-you page with a custom one? :slight_smile:

    Thank you!

    Kind Regards
    Jack.

  • Cheryl

    Hey Jack,

    This is great, thanks! I'll see what I can do with these, and will drop the result here so you can add it to the documentation if you like.

    I noticed two pretty big issues that relate to the 'thank you' page:

    1. The user is never taken to the thank you page. The question posting process actually goes:
    - Type in their question, hit 'login / register to submit'
    - Taken to login form
    - As soon as they either create an account or login, they are taken directly to their posted question, skipping the 'thank you' page entirely.
    1a. Actually, just noticed that the user is only taken to the 'thank you' page if their question is saved as pending. We currently publish all questions immediately, but really want users to see the thank you page first. Is there a way to alter this functionality?
    2. Users need neither to complete the registration process nor be logged in to submit a question. If they hit 'register / login to submit' on the 'ask a question' page, and then simply use the back button on their browser to return to the previous page, their question posts with no author.

    Thanks,
    Paul

  • Cheryl

    Hey Jack,

    I've just solved #1 above (taking the user to the thank you page even if their question is published), by creating a new class in a file external to the plugin (to avoid destroying upgradability) and edited line 126 of the native functionality. Here's my function:

    class QA_Edit_Mod extends QA_Edit {
    		function _insert_post( $post_id, $post, $defaults ) {
    		if ( !$post_id ) {
    			global $wpdb, $qa_email_notification_content, $qa_email_notification_subject, $current_site;
    
    			if (!isset($current_site)) {
    				$_url = get_bloginfo('url');
    				$_domain_parts = split('/', $_url);
    				$current_site = (object) array('domain' => $_domain_parts[2]);
    			}
    
    			// Assign an author, if selected so flooding check can work
    			if ( !is_user_logged_in() && 'assign' == $this->g_settings["method"] )
    				$post['post_author'] = $this->g_settings["assigned_to"];
    
    			// Check for flooding
    			$most_recent = $wpdb->get_var( $wpdb->prepare( "
    				SELECT MAX(post_date)
    				FROM $wpdb->posts
    				WHERE post_status = 'publish'
    				AND post_type IN ('question', 'answer')
    				AND post_author = %d
    			", get_current_user_id() ) );
    
    			$diff = current_time( 'timestamp' ) - strtotime( $most_recent );
    			if ( !current_user_can('manage_options') && $diff < QA_FLOOD_SECONDS )
    				wp_die( __( 'You are posting too fast. Slow down.', QA_TEXTDOMAIN ) );
    
    			$v = get_role( 'visitor' );
    			if ( $v && is_object( $v ) && $v->has_cap( 'immediately_publish_questions' ) )
    				$visitor_can_publish = true;
    			else
    				$visitor_can_publish = false;
    
    			// Find if post will be saved as pending. New in V1.2
    			$post = array_merge( $post, $defaults );
    			if ( is_user_logged_in() && current_user_can( 'immediately_publish_questions' ) )
    					$post['post_status'] = 'publish';
    			else if ( is_user_logged_in() )
    				$post['post_status'] = 'pending';
    			else if ( $visitor_can_publish )
    				$post['post_status'] = 'publish';
    			else
    				$post['post_status'] = 'pending';
    
    			// Create new post
    			$post = apply_filters( 'qa_before_insert_post', $post );
    			$post_id = wp_insert_post( $post, true );
    
    			// Add tags
    			$question_tag = apply_filters( 'qa_before_add_tag', @$_POST['question_tags'], $post );
    			wp_set_post_terms( $post_id, $question_tag, 'question_tag' );
    
    			// Add category
    			$question_category = apply_filters( 'qa_before_add_category', array( (int) @$_POST['question_cat'] ), $post );
    			// Assign a default category if selected so
    			if ( ( empty( $question_category ) || -1 == $question_category[0] )
    				&& isset( $this->g_settings["default_category"] ) && $this->g_settings["default_category"] )
    				$question_category = array( $this->g_settings["default_category"] );
    			wp_set_post_terms( $post_id, $question_category, 'question_category' );
    
    			$post = get_post( $post_id, ARRAY_A );
    
    			// Notification
    			if (isset($defaults['post_type']) && $defaults['post_type'] == 'question') {
    				$notification_subscriptions = $wpdb->get_results( "SELECT user_id
    					FROM {$wpdb->usermeta}
    					WHERE meta_key = 'qa_notification'
    					AND meta_value = 1");
    				// Add custom users to be notified
    				$notification_subscriptions = apply_filters( 'qa_notified_users', $notification_subscriptions, $post );
    
    				$message_content = get_option('qa_email_notification_content', $qa_email_notification_content);
    				$message_content = str_replace( "SITE_NAME", get_option( 'blogname' ), $message_content );
    				$message_content = str_replace( "SITE_URL", 'http://' . $current_site->domain . '', $message_content );
    
    				$message_content = str_replace( "QUESTION_TITLE", $post['post_title'], $message_content );
    				$message_content = str_replace( "QUESTION_DESCRIPTION", strip_tags($post['post_content']), $message_content );
    				$message_content = str_replace( "QUESTION_LINK", get_permalink($post_id), $message_content );
    				// Modify email message
    				$message_content = apply_filters( 'qa_message_content', $message_content, $post );
    
    				$message_content = str_replace( "\'", "'", $message_content );
    
    				$subject_content = get_option('qa_email_notification_subject', $qa_email_notification_subject);
    				$subject_content = str_replace( "SITE_NAME", get_option( 'blogname' ), $subject_content );
    				// Modify message subject
    				$subject_content = apply_filters( 'qa_message_subject', $subject_content, $post );
    
    				$admin_email = get_site_option('admin_email');
    				if ( !$admin_email ){
    					$admin_email = 'admin@' . $current_site->domain;
    				}
    
    				$message_headers = "MIME-Version: 1.0\n" . "From: " . get_option( 'blogname' ) .  " <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
    				// Modify message headers
    				$message_headers = apply_filters( 'qa_message_headers', $message_headers, $post );
    
    				foreach ( $notification_subscriptions as $uid ) {
    					$user_data = get_userdata($uid->user_id);
    					if ( $user_data )
    						wp_mail( $user_data->user_email, $subject_content, str_replace( "TO_USER", $user_data->display_name, $message_content ), $message_headers);
    				}
    			}
    
    			// Anon posting
    			if ( !is_user_logged_in() ) {
    
    				$login_url = site_url( 'wp-login.php', 'login' );
    				if ( get_option( 'users_can_register' ) )
    					$login_url .= '?action=register';
    
    				$login_url = apply_filters( 'qa_login_url', $login_url );
    
    				// Enable claiming question if it is not assigned to an author
    				if ( 'assign' != $this->g_settings["method"] ) {
    					$key = md5( current_time('timestamp') . $post_id );
    					add_post_meta( $post_id, '_claim', $key );
    					setcookie( '_qa_claim', $key, 0, '/' );
    					wp_redirect( $login_url );
    					die;
    				}
    				// We're sending the user to the thank you page regardless of draft status
    				if ( 'publish' == $post['post_status'] ) {
    					wp_redirect( get_permalink( $this->g_settings["thank_you"] ) );
    					die;
    				}
    				// Send to thank you page
    				else {
    					wp_redirect( get_permalink( $this->g_settings["thank_you"] ) );
    					die;
    				}
    			}
    			else if ( 'publish' != $post['post_status'] ) {
    				wp_redirect( get_permalink( $this->g_settings["thank_you"] ) );
    				die;
    			}
    		} else {
    			if ( !current_user_can( 'edit_post', $post_id ) )
    				die( "Cheatin' uh?" );
    
    			// Update post
    			$post['ID'] = $post_id;
    			$post = apply_filters( 'qa_before_update_post', $post );
    			$post_id = wp_update_post( $post, true );
    
    			// Add tags
    			$question_tag = apply_filters( 'qa_before_add_tag', @$_POST['question_tags'], $post );
    			wp_set_post_terms( $post_id, $question_tag, 'question_tag' );
    
    			// Add category
    			$question_category = apply_filters( 'qa_before_add_category', array( (int) @$_POST['question_cat'] ), $post );
    			wp_set_post_terms( $post_id, $question_category, 'question_category' );
    		}
    
    		if ( is_wp_error( $post_id ) ) {
    			wp_die( $post_id->get_error_message() );
    		}
    
    		return $post_id;
    	}
    }

    My guess is that #2 has to do with the structure of the if statements in edit.php.

  • Cheryl

    Ok, so I've noticed that on the last line of edit.php a new instance of QA_Edit is being called:

    $_qa_edit = new QA_Edit();

    Is there a way I can do something like this:

    class QA_Edit_Mod extends QA_Edit {
    		function registration_redirect( $redirect_to ) {
    		$post_id = $this->_get_post_to_claim();
    
    		if ( !$post_id )
    			return;
    
    		// Check if post is published
    		$post = get_post( $post_id );
    		if ( 'publish' == $post->post_status )
    			//$url = qa_get_url( 'single', $post_id );
    			$url = get_permalink( $this->g_settings['thank_you'] ) ;
    		else if ( !$url = get_permalink( $this->g_settings['thank_you'] ) )
    			$url = site_url();
    
    		return $url;
    	}
    }
    
    $_qa_edit = new QA_Edit_Mod();

    This obviously completely omits all of the rest of the functions from QA_Edit, but I really just want to modify the behavior of that one function to redirect users to the thank you page (see if case). If I completely recreate the class, then I lose a lot of upgradability of the plugin (if your team edits the QA_Edit class.

    Any ideas?

    Thanks,
    Paul

  • Arnold

    You can extend the class as you indicate but your would need to prevent the "new" call in the plugin. Otherwise you just create two different instances.

    It would be better to add a filter at the end of the current registration_redirect that gives you a hook to change the url however you like.

    So I would add a line

    $url = apply_filters('qa_register_redirect', $url, $post_id );

    just before the return $url at the bottom of the routine. It passes you the $url and the $post_id so you can figure out how you want to change it. I've added it to the next version so you won't get overwritten if you add it to your copy.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.