enkiki
enkiki

Reputation: 399

PHP Snippet - WordPress problem on Edge/Firefox

I have a strange problem...

I have a PHP snippet that i'm using inside Wordpress and the plugin SNIPPETS.

The code is working is fine in Chrome but for whatever reason, nothing is showing up in Edge or Firefox.

I just want to display a different icon based on a custom user field.

Here is my code

 function ajax_13xx()

 { $current_user = wp_get_current_user();

 if ( $current_user->s_managed == Yes  ) { 

  return '<div align="right"><img src="abc.com/onswitch20.jpg"> </div>' ;    

 }

  if ( $current_user->s_managed == No  ) { 

  return '<div align="right"><img src="abc.com/offswitch20.jpg"></div>' ;  

  }
}

add_shortcode('managetoggle', 'ajax_13xx');

I'm using Elementor and the Shortcode widget for [managetoggle]

Any feedback would be appreciated!

Regards, Nathalie

Upvotes: 1

Views: 98

Answers (2)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21671

Use an else block.

if ( $current_user->s_managed == 'Yes'  ) { //make this a string not a constant    
  return '<div align="right"><img src="abc.com/onswitch20.jpg"> </div>' ;        
}else{     
  return '<div align="right"><img src="abc.com/offswitch20.jpg"></div>' ;      
}

Because this is a boolean value, we can simply use else, you want the No in else because we could use 0 or No or Null Generally anything that is falsy. This will also be like a plank time (smallest unit of time possible) faster as only one condition needs to be checked.

Another even better way to write this is like this:

function ajax_13xx()
     return '<div align="right"><img src="abc.com/'.(wp_get_current_user()->s_managed == 'Yes' ?  'on' : 'off').'switch20.jpg"> </div>' ;  
}

Then you keep your code a bit more DRY (don't repeat yourself)

Upvotes: 1

enkiki
enkiki

Reputation: 399

Found solution already...

Added this line...

if ( NULL == $current_user->s_managed ) {

Upvotes: 0

Related Questions