AI Verified

Name

Display External Server IP

About

This WordPress code snippet enhances the admin dashboard by adding a custom item to the WordPress admin bar labeled "Check External IP". This feature allows administrators to fetch and display the external IP address of the server hosting the WordPress site with a simple click. It's designed to provide convenience for administrators, especially useful in scenarios where knowing the external IP address is necessary for troubleshooting, configuration, or security purposes.

Language

PHP

Rating

Voted: 1 by 1 user(s)

How to Setup Snippet

Viewing the External IP: Once implemented, administrators will see a "Check External IP" item in the WordPress admin bar. Clicking this item will trigger an alert dialogue that displays the current external IP address of the server. Security and Privacy: This feature is secure and respects privacy by limiting access to administrators and not displaying the IP address until explicitly requested. The code snippet utilizes api.ipify.org, a simple and straightforward external service, to fetch the external IP address of the server where the WordPress site is hosted. This is a free API and you should visit their website for more information.

Codevault

Nestwise

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

29/03/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Cross Site Scripting
Found on line : 51
Code : echo(esc_html($data->ip)
Vulnerable line : 51
Code : $data = json_decode($body));

Found 1 vulnerabilities

Display External Server IP

 
                    
1function add_external_ip_check_to_admin_bar($wp_admin_bar) {
2 // Check if the current user has the 'manage_options' capability
3 if (!current_user_can('manage_options')) {
4 return; // Exit if the user is not an admin
5 }
6 
7 $args = array(
8 'id' => 'external-ip-check',
9 'title' => 'Check External IP', // The title of your admin bar button
10 'href' => '#',
11 'meta' => array(
12 'class' => 'external-ip-check',
13 'title' => 'Click to fetch the server\'s external IP address'
14 )
15 );
16 $wp_admin_bar->add_node($args);
17 
18 // Inline JavaScript for handling the AJAX request
19 $script = <<<SCRIPT
20 <script type="text/javascript">
21 jQuery(document).ready(function($) {
22 $('.external-ip-check').on('click', function(e) {
23 e.preventDefault();
24 $.ajax({
25 url: ajaxurl,
26 data: {
27 'action': 'get_external_ip'
28 },
29 type: 'POST',
30 success: function(response) {
31 alert("External IP: " + response); // Display the IP through an alert or consider a more elegant way
32 }
33 });
34 });
35 });
36 </script>
37SCRIPT;
38 
39 echo $script;
40}
41 
42add_action('admin_bar_menu', 'add_external_ip_check_to_admin_bar', 100);
43 
44// AJAX handler for fetching the external IP
45function handle_get_external_ip() {
46 $response = wp_remote_get('https://api.ipify.org?format=json');
47 if (is_wp_error($response)) {
48 echo 'Error fetching IP';
49 } else {
50 $body = wp_remote_retrieve_body($response);
51 $data = json_decode($body);
52 echo esc_html($data->ip);
53 }
54 wp_die(); // make sure to end script execution
55}
56 
57add_action('wp_ajax_get_external_ip', 'handle_get_external_ip');

1

Related Snippets

Please see some snippets below related to this snippet..

WordPress Admin

AI Verified

0

post duplication

Added: 1 year ago

Last Updated: 1 year ago

Function for post duplication. Dups appear as drafts. User is redirected to the edit screen

WordPress Admin

AI Verified

0

Redirect user role on specific page

Added: 1 year ago

Last Updated: 1 year ago

This code will redirect a specific page based on the user role NOT being met.

WordPress Admin

AI Verified

0

Display snippet line numbers

Added: 1 year ago

Last Updated: 1 year ago

Based on work by platypus424 and germankiwi, this snippet will allow you to see code on a particular line number from all of your snippets, allowing you to track down exactly where an error might be o...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

5

Display Estimated Read Time For Blog Posts

Added: 6 months ago

Last Updated: 5 months ago

Display the estimated read time of a post with a shortcode. This code snippet produces a shortcode to display the estimated reading time for a post. It strips HTML tags, counts the words, and divi...

WordPress Admin

AI Verified

1

Display External Server IP

Added: 4 months ago

Last Updated: 3 months ago

This WordPress code snippet enhances the admin dashboard by adding a custom item to the WordPress admin bar labeled "Check External IP". This feature allows administrators to fetch and display the ext...

General

AI Verified

0

What's My IP? Display the current users IP address with shortcode

Added: 4 months ago

Last Updated: 4 months ago

Display the users current IP address with shortcode [my-ip]