Title: wp_localize_community_events
Published: June 15, 2017
Last modified: April 28, 2025

---

# wp_localize_community_events()

## In this article

 * [Source](https://developer.wordpress.org/reference/functions/wp_localize_community_events/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_localize_community_events/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_localize_community_events/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_localize_community_events/?output_format=md#wp--skip-link--target)

Localizes community events data that needs to be passed to dashboard.js.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_localize_community_events/?output_format=md#source)󠁿

    ```php
    function wp_localize_community_events() {
    	if ( ! wp_script_is( 'dashboard' ) ) {
    		return;
    	}

    	require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php';

    	$user_id            = get_current_user_id();
    	$saved_location     = get_user_option( 'community-events-location', $user_id );
    	$saved_ip_address   = isset( $saved_location['ip'] ) ? $saved_location['ip'] : false;
    	$current_ip_address = WP_Community_Events::get_unsafe_client_ip();

    	/*
    	 * If the user's location is based on their IP address, then update their
    	 * location when their IP address changes. This allows them to see events
    	 * in their current city when travelling. Otherwise, they would always be
    	 * shown events in the city where they were when they first loaded the
    	 * Dashboard, which could have been months or years ago.
    	 */
    	if ( $saved_ip_address && $current_ip_address && $current_ip_address !== $saved_ip_address ) {
    		$saved_location['ip'] = $current_ip_address;
    		update_user_meta( $user_id, 'community-events-location', $saved_location );
    	}

    	$events_client = new WP_Community_Events( $user_id, $saved_location );

    	wp_localize_script(
    		'dashboard',
    		'communityEventsData',
    		array(
    			'nonce'       => wp_create_nonce( 'community_events' ),
    			'cache'       => $events_client->get_cached_events(),
    			'time_format' => get_option( 'time_format' ),
    		)
    	);
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/script-loader.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/script-loader.php#L2021)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/script-loader.php#L2021-L2056)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_localize_community_events/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Community_Events::__construct()](https://developer.wordpress.org/reference/classes/wp_community_events/__construct/)`wp-admin/includes/class-wp-community-events.php` |

Constructor for [WP_Community_Events](https://developer.wordpress.org/reference/classes/wp_community_events/).

  | 
| [WP_Community_Events::get_unsafe_client_ip()](https://developer.wordpress.org/reference/classes/wp_community_events/get_unsafe_client_ip/)`wp-admin/includes/class-wp-community-events.php` |

Determines the user’s actual IP address and attempts to partially anonymize an IP address by converting it to a network ID.

  | 
| [wp_script_is()](https://developer.wordpress.org/reference/functions/wp_script_is/)`wp-includes/functions.wp-scripts.php` |

Determines whether a script has been added to the queue.

  | 
| [wp_localize_script()](https://developer.wordpress.org/reference/functions/wp_localize_script/)`wp-includes/functions.wp-scripts.php` |

Localizes a script.

  | 
| [update_user_meta()](https://developer.wordpress.org/reference/functions/update_user_meta/)`wp-includes/user.php` |

Updates user meta field based on user ID.

  | 
| [get_user_option()](https://developer.wordpress.org/reference/functions/get_user_option/)`wp-includes/user.php` |

Retrieves user option that can be either per Site or per Network.

  | 
| [wp_create_nonce()](https://developer.wordpress.org/reference/functions/wp_create_nonce/)`wp-includes/pluggable.php` |

Creates a cryptographic token tied to a specific action, user, user session, and window of time.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  | 
| [get_current_user_id()](https://developer.wordpress.org/reference/functions/get_current_user_id/)`wp-includes/user.php` |

Gets the current user’s ID.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/wp_localize_community_events/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_localize_community_events/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_localize_community_events/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.8.0](https://developer.wordpress.org/reference/since/4.8.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_localize_community_events%2F)
before being able to contribute a note or feedback.