Skip Navigation

[Resolved] Conditional statement for usermeta not working

This support ticket is created 5 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 30 replies, has 2 voices.

Last updated by mikeH-3 5 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1250417

I just need a way for people to enter in embed code and have the system recognize that field isn't empty. Everything else works. It's simply to allow my clients to paste in embeddable forms from another site. Not crazy about having to switch to a wysigyg field because I don't want them to have to figure out to choose the 'visual' tab vs the 'text' one. I just want it to be real simple. Again, I don't need anything rendered back there. I just need toolset to spot that that code there means the field isn't empty.

#1253031

Okay let's try a custom shortcode and keep the multiline field instead of a WYSIWYG. Add this to your child theme's functions.php file, or create a new code snippet in Toolset > Settings > Custom Code:

function usermeta_has_content_func($atts) {
  $atts = shortcode_atts( array(
      'slug' => 0
  ), $atts );

  $slug = $atts['slug'];
  $rendered = types_render_usermeta( $atts['slug'], array( "user_current" => true, "output" => 'raw' ) );
  return $rendered == '' ? 0 : 1;
}

Then go to Toolset > Settings > Frontend content and register usermeta-has-content in third party shortcode arguments.

Now you can use the shortcode like this in a conditional:

[wpv-conditional if="('[usermeta-has-content slug="online-giving-form-embed-code"]' eq '0')"]There is content in the field[/wpv-conditional]
[wpv-conditional if="('[usermeta-has-content slug="online-giving-form-embed-code"]' eq '1')"]There is no content in the field[/wpv-conditional]
#1253255

Thanks for sticking with this and the code! I did everything you said (added functions code and registered that shortcode in toolset) and nothing shows up when I use both shortcodes in my post, whether that user meta field is filled in or not. Can you help?

#1253337

Can you provide login credentials in the private reply fields here? Let me know where I can find the shortcodes on the front-end of the site.

#1253461

Something about how the code was added to functions.php is not quite right. I placed the shortcode manually before the conditional, and it's just printing out the shortcode on the front-end. That means it's not registered properly. Can you tell me how you added it to functions.php, and are you sure you uploaded it correctly to the site via FTP? Since file editing is disallowed, I can't inspect the file via wp-admin.

#1253471

I added this to the functions.php file:



//toolset code usermeta
function usermeta_has_content_func($atts) {
  $atts = shortcode_atts( array(
      'slug' => 0
  ), $atts );
 
  $slug = $atts['slug'];
  $rendered = types_render_usermeta( $atts['slug'], array( "user_current" => true, "output" => 'raw' ) );
  return $rendered == '' ? 0 : 1;
}

I removed it just now if you want to add it somewhere else?

#1253473

I enabled file editing for you if you want to use it.

#1253483

That's my fault, there was one line missing because of a copy + paste error. Sorry! I'm still not able to edit the theme files directly in wp-admin, though. Can you add the entire code again?

function usermeta_has_content_func($atts) {
  $atts = shortcode_atts( array(
      'slug' => 0
  ), $atts );

  $slug = $atts['slug'];
  $rendered = types_render_usermeta( $atts['slug'], array( "user_current" => true, "output" => 'raw' ) );
  return $rendered == '' ? 0 : 1;
}
add_shortcode( 'usermeta-has-content', 'usermeta_has_content_func');
#1253611

Okay that does show your shortcode now but I still have same problem. When I go to ?church=zoe url I sent you, it shows that field as having no content even though that user meta field is filled in for that username zoe.

And if I go to the other username in there, it shows same message even though that username has no code filled in for that online giving field.

#1253619

Okay I didn't realize you wanted to use the URL parameter that way. I was just checking the field of the current logged-in User. I'll have to work on this a bit more and give you an update tomorrow.

#1253635

Ahh, no it's completely based on the url having the username (its using the views author filter) and then seeing if that username has the giving code in there. Thanks so much for help. Look forward to seeing it working.

#1256975
Screen Shot 2019-06-03 at 10.02.25 AM.png

Okay you'll need to create a View of Users, filtered by User Name, set using a URL parameter "church" (see the attachment). Then in the Loop editor of that View, you can place the conditionals like this:

<wpv-loop>
[wpv-conditional if="([usermeta-has-content slug='user-multiline'] eq '1') " debug="true"]
  Equals not empty
[/wpv-conditional]
[wpv-conditional if="([usermeta-has-content slug='user-multiline'] eq '0') " debug="true"]
  Equals empty
 [/wpv-conditional]
</wpv-loop>
#1257087

I did that but now what? This doesn't do anything. I'm really lost now how this pertains to my original question of how do I show something in my post view if a usermeta field has html code in it? I just need the conditional to work with text areas that have html code in them.

#1257297

I did that but now what? This doesn't do anything.
Create the View of Users like I described. Then place that View wherever you want to use these conditionals.

I'm really lost now how this pertains to my original question of how do I show something in my post view if a usermeta field has html code in it?
You need to access User metadata from a User whose username is available as a URL attribute. We will access that User using a filtered View of Users. In the results of that View, you have access to the User's metadata. You can use the shortcodes I gave you earlier to test whether or not the user meta field is empty. This works with script tags and other HTML.

This is a convoluted solution for two reasons:
1. You're using Javascript incorrectly.
2. You're passing a Username instead of a User ID.

#1257347

I added it in my real site (not the test site you have) so now I did add it to the test site

It still doesn't do anything. I'm at a loss as to what I'm supposed to do. All accounts show the same code in the debug view whther that specific field is empty or not.

I do appreciate your help.. I really do... but I feel like we aren't getting anywhere to a question I asked a week ago that should seemingly be so simple. It's all because toolset conditionals consideres, for example, <iframe=something.html> to not be anything and therefore views that field as empty. If I put real text in, it works fine. So I don't know why we have to do this workaround.

Is there no function I can put in so toolset text fields recognizes html code as the field not being empty?