Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_shortcode( 'points_for_correct_answer', 'render_points_for_correct_answer' );
- function render_points_for_correct_answer( $atts = array(), $content = NULL ) {
- extract( shortcode_atts( array(
- 'answer' => '',
- 'amount' => '',
- 'ok_text' => '',
- 'ok_img' => '',
- 'bad_text' => '',
- 'bad_img' => ''
- ), $atts ) );
- // User must be logged in
- if ( ! is_user_logged_in() || ! function_exists( 'mycred_get_settings' ) ) return;
- // Prep
- $user_id = get_current_user_id();
- $post_id = $GLOBALS['post']->ID;
- // Load myCRED
- $mycred = mycred_get_settings();
- // Answer must be set and so does the amount!
- if ( empty( $answer ) || empty( $amount ) ) return;
- // Check if form has been submitted
- if ( isset( $_POST['token'] ) && wp_verify_nonce( $_POST['token'], 'answering-question' ) ) {
- $given_answer = sanitize_text_field( $_POST['answer'] );
- if ( $given_answer == $answer ) {
- // Data
- $data = array(
- 'ref_type' => 'post',
- 'answer' => $answer
- );
- // Make this unique
- if ( $mycred->has_entry( 'answer_questions', $post_id, $user_id, $data ) )
- return 'You have already received points for this question.';
- // Add points
- $mycred->add_creds(
- 'answer_questions',
- $user_id,
- $amount,
- '%plural% for correct answer on post %link_with_title%',
- $post_id,
- $data
- );
- $output = $ok_text;
- if ( ! empty( $ok_img ) )
- $output .= '<img src="' . $ok_img . '" alt="" />';
- return $output;
- }
- else {
- $output = $bad_text;
- if ( ! empty( $bad_image ) )
- $output .= '<img src="' . $bad_image . '" alt="" />';
- return $output;
- }
- }
- // No form submission
- else {
- return '
- <form action="" method="post">
- <label>Answer</label>
- <input type="text" name="answer" id="answer" value="" />
- <input type="hidden" name="token" value="' . wp_create_nonce( 'answering-question' ) . '" />
- <input type="submit" name="submit" value="Send" />
- </form>';
- }
- }
- endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement