Title: Cannot redeclare function
Last modified: May 7, 2024

---

# Cannot redeclare function

 *  Resolved [PeterC66](https://wordpress.org/support/users/peterc66/)
 * (@peterc66)
 * [2 years ago](https://wordpress.org/support/topic/cannot-redeclare-function-5/)
 * Thanks for the excellent plugin.
 * I am trying to use some code from WPBeginner. It defines the same function in
   two different ways depending on whether a cookie is set or not. So logically 
   the function cannot get declared twice. In essence the code is:
 *     ```wp-block-code
       function wpb_cookies_tutorial2() {
       	if(isset($_COOKIE['wpb_visit_time'])) {
       		function visitor_greeting() {}   
       	} else {
       		function visitor_greeting() {}   
       	}   
       }
       ```
   
 * When I try to activate this I get:
   Snippet automatically deactivated due to an
   error on line 6: Cannot redeclare function visitor_greeting.
 * If I try:
 *     ```wp-block-code
       function wpb_cookies_tutorial2() {
       	if(isset($_COOKIE['wpb_visit_time'])) {
       	if (!function_exists('visitor_greeting')){
       		function visitor_greeting() {}  }
       	} else {
       	if (!function_exists('visitor_greeting')){
       		function visitor_greeting() {}  }
       	}   
       }
       ```
   
 * then Code Snippets accepts the code.
 * Is that the best way to make it acceptable to Code Snippets?
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcannot-redeclare-function-5%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [2 years ago](https://wordpress.org/support/topic/cannot-redeclare-function-5/#post-17777431)
 * Hi [@peterc66](https://wordpress.org/support/users/peterc66/),
 * The code you provided seems quite strange in its construction. In these situations,
   as in general with snippets, I’d recommend favouring anonymous functions over
   named functions.
 * Here’s a rewritten version of that code:
 *     ```wp-block-code
       add_action(
       	'init',
       	function () {
       		$visit_time = gmdate( 'F j, Y g:i a' );
   
       		if ( isset( $_COOKIE['wpb_visit_time'] ) ) {
       			$last_visit = sanitize_text_field( wp_unslash( $_COOKIE['wpb_visit_time'] ) );
   
       			add_shortcode(
       				'greet_me',
       				function () use ( $last_visit ) {
       					$string = 'You last visited our website ' . esc_html( $last_visit ) . '. Check out what\'s new';
       					unset( $_COOKIE['wpb_visit_time'] );
       					return $string;
       				}
       			);
   
       		} else {
       			add_shortcode(
       				'greet_me',
       				function () {
       					return 'New here? Check out these resources…';
       				}
       			);
       		}
   
       		setcookie( 'wpb_visit_time', $visit_time, time() + 31556926 );
       	}
       );
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Cannot redeclare function’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [2 years ago](https://wordpress.org/support/topic/cannot-redeclare-function-5/#post-17777431)
 * Status: resolved