nakoya surume
asked 5 years ago

Can I see user ip address when they submit new post or comment?
(user ip in backend for admin only)

2 Answers
Allen
answered 5 years ago

Of course you can.All you need is attach the action to function when the button submit answer,comment and question click using ajax. Here is guide to get the user ip: http://www.sitepoint.com/client-ip-jquery/
When user click button, it’ll get user ip , you can use ajax to send it to a php file then save it along with the answer or question.If you need more help i’ll spend time to guide you.Glad!

zens
replied 5 years ago

thank so much, but I think it too difficult for me. ::cry

Allen
answered 5 years ago

@nakoya sureme : hi, i think you can archive this , just follow my instruction then:
First, You have to add this function to dw-question-answer\inc\actions.php

function dwqa_get_client_ip_server() {
    $ipaddress = '';
    if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } else if( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    } else if( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if( isset( $_SERVER['HTTP_FORWARDED'] ) ) {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    } else if( isset( $_SERVER['REMOTE_ADDR'] ) ) {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipaddress = 'UNKNOWN';
    }
    return $ipaddress;
}

This function is use get the ip address of the client.
And after that , there’ll be 3 sections to work at : Submit Question, Submit Answer and Submit Comment

Section 1 : Submit Question (get ip of user post the question )
First , in that file actions.php in function function dwqa_submit_question() at about line 280 ( I modify code a bit so i cant’ point you the correct line ), and then go to about line 417
change the code there to

if ( dwqa_current_user_can( 'post_question' ) ) {
    $new_question = dwqa_insert_question( $postarr );
    $user_ip = dwqa_get_client_ip_server();
    update_post_meta( $new_question, '_user_ip', $user_ip );
} else {….

After that you can open file inc\templates\default\single-question.php
Add some code there after line 7 :

$user_ip = get_post_meta( get_the_ID(), '_user_ip', true );
    if ( isset( $user_ip )  &&  "" != $user_ip  &&  current_user_can('manage_options') ){
        echo  $user_ip;
    }

Section 2 : Submit Answer ( get ip of user that answer question )
First: in file actions.php if function dwqa_add_answer(), go to line about 107 , change the code there to :

if ( dwqa_current_user_can( 'post_answer' ) ) {
    $answer_id = wp_insert_post( $answers, true );
    $user_ip = dwqa_get_client_ip_server();
    update_post_meta( $answer_id, '_user_ip', $user_ip );
} else {…..

And then you open file inc\templates\default\content-answer.php after line 13 add these code :

$user_ip = get_post_meta( $answer_id, '_user_ip', true );
    if ( isset($user_ip) && "" != $user_ip && current_user_can('manage_options')) {
        echo $user_ip;
    }

Section 3 : Comment submit ( get ip of user comment )

First: in file actions.php if function dwqa_comment_action_add (), go to line about 1047 , after the line code : do_action( 'dwqa_add_comment', $comment_id, $client_id );
add some code there :

$user_ip = dwqa_get_client_ip_server();
update_comment_meta( $comment_id, '_user_ip' , $user_ip );

And then you open file inc\templates\default\content-comment.php after line 1 add these code :

<?php 
    $user_ip = get_comment_meta( get_comment_ID(), '_user_ip', true );
    if ( isset( $user_ip ) && "" != $user_ip && current_user_can('manage_options') ){
        echo $user_ip;
    }
?>

Those codes will help you archive the IP of client who ask question , answer and comment in the front-end and right above each section when you login in as admin. All Ips are store in meta key, you can call it anywhere you want even in back-end , it’s up to you.
If you have problem with this , just ask. Hope you can make it work 🙂

zens
replied 5 years ago

OMGGGG. I can do it,
I love you, I love you, I love you.

ps. If I update plugin, I need to do this again?

nobita
replied 5 years ago

Well it’s the code i customize for you, it’s not in the plug in so if you update the plugin, you might lose it. But i think it’s easy to get it back.

Powered by DW Question & Answer Pro