simpliko / wpadverts

WordPress Classifieds Plugin
https://wpadverts.com/
GNU General Public License v2.0
20 stars 11 forks source link

Get and save user IP adress #138

Closed Iainthealer closed 2 years ago

Iainthealer commented 2 years ago

Hello!

I need to somehow save users IP address somewhere in his post in admin panel. I would like to add a new field in users advertisement in administrator panel, where only admins could see it. So no displaying in frontend. I've checked code snippets and cant find anything that would help me.

Could you please give me a hint on how to do it?

gwin commented 2 years ago

Hi, i understand you want to save the IP when a user is posting from the page with the [adverts_add] shortcode if so then you should be able to do that by adding the code below in your theme functions.php file, it will save the _ip_address in the wp_postsmeta table

add_filter( "adverts_add_pre_save", function( $init ) {
    $init["meta"]["_ip_address"] = array(
        "value" => adverts_get_ip(),
        "field" => array( "type" => "adverts_field_hidden" )
    );
    return $init;
});

Now you will also need some code to show the IP address when editing the Ad from the wp-admin / Classifieds panel, the easiest way to do that is to use WP meta boxes API, the code below will create a new meta box on the Ad edition pages with the IP address in the content

add_action( "add_meta_boxes", function() {
    $screens = [ 'advert' ];
    foreach ( $screens as $screen ) {
        add_meta_box(
            'advert_ip_address', 
            'User IP Address',    
            'advert_ip_meta_box_html',
            $screen                  
        );
    }
} );
function advert_ip_meta_box_html( $post ) {
    echo sprintf( "<div>IP: <strong>%s</strong></div>", get_post_meta( $post->ID, "_ip_address", true ) );
}

All the code snippets you can add in your theme functions.php file or create a new blank plugin and it there.

Also, please send the support questions via the contact form at https://wpadverts.com/contact/ or the forum at WP.org the GitHub issues are for bugs and improvements, thanks.