Is there a way to add_action hooks into the qa plugin?

I need to trigger some actions in response to certain forum activity. For example, when a use's answer gets accepted, I want to add an entry to the user's meta data. I would prefer not to hack the code such we can't apply future updates.

  • Hakan

    Hi,

    As Q&A uses custom post types, you can mostly use standard WP actions.

    In your case, a post meta update is done for the question of the related answer. You can have a look at votes.php file how plugin saves related post meta (_accepted_answer).

    A sample code to be included inside functions.php of your theme:

    function add_usermeta_for_qa( $meta_id, $object_id, $meta_key, $meta_value ) {
    		// Check if this is the correct post meta
    		if ( $meta_key != '_accepted_answer' )
    			return;
    
    		$answer_id = (int) $_POST['answer_id'];
    		// We may need this for other things
    		$question_id = get_post_field( 'post_parent', $answer_id );
    
    		if ( 'on' == $_POST['accept'] ) {
    			// Find the author of the answer
    			$answer = get_post( $answer_id );
    			// Get his existing accepted answers
    			$old_value = get_user_meta( $answer->post_author, 'accepted_answers', true );
    			if ( $old_value )
    				$new_value = $old_value . ",". $answer_id;
    			else
    				$new_value = $answer_id;
    
    			// Write new value to his user meta
    			update_user_meta( $answer->post_author, 'accepted_answers', $new_value );
    		}
    }
    add_action( 'update_postmeta', 'add_usermeta_for_qa', 10, 4 );

    This sample saves accepted answers separated with a comma.

    I hope this helps.

    Cheers,
    Hakan

  • aecnu

    Greetings StreetofWalls,

    We have not heard back from you as to the status of this issue.

    If you are still having an issue please let us know so that we may try to get you fixed up as soon as possible by choosing to check mark this ticket as unresolved below and posting any new errors or symptoms you are noticing.

    This action will also bring your ticket up front back in plain view again within the ticket system.

    Thank you for being a WPMU Dev Community Member!

    Cheers, Joe

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.