skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    Found solution already...

    Added this line...

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

  2. 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)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search