0

I'm trying to create a WP shortcode that will insert a profile form for a Wordpress user.

I want to call the following action: <?php do_action( 'bbpnns_digest_show_profile_form', $user); ?> Where $user is the WP_User object for the user being displayed.

Here's what I have so far but it doesn't work (I get a "Bad User" message):

function custom_shortcode_sc() {
  $current_user = wp_get_current_user (); 
  $user=$current_user->user_login;

  do_action( 'bbpnns_digest_show_profile_form', $user);        
}
add_shortcode( 'custom_shortcode', 'custom_shortcode_sc' );

I think I am close but am missing something with calling the $user properly. Thanks for your help!

oyegigi
  • 11
  • 4
  • 1) I'd a check if user is logged in first. Maybe you don't have a current user. Also could check if $current_user is an object and not false. 2) what issues the 'Bad User' message? That doesn't sound like a WP message. Perhaps whatever it is is expecting the full user object? OR if trying to get $current_user->user_login from 'false' that might cause the message. Add checks & debug code. 3) You haven't shown the code for the action that has been added at the action hook. IE: somewhere else you have code that does "add_action ('bbpnns_digest_show_profile_form', 'some_added_action')" ? – anmari Aug 02 '18 at 03:24
  • @anmari 1) makes sense. Now I just have to figure out how to do that properly! lol 2-3) I'm not sure where the code for the action lives but the author of the plugin I'm using directed me to use that action. I think I do need to call the cull user object since he also directed me that "the $user is the WP_User object for the user being displayed." – oyegigi Aug 02 '18 at 03:35

2 Answers2

1

Thanks to @yogesh for an easy solution that got my code working. Here's the final code for reference:

function custom_shortcode_sc() {
  $current_user = wp_get_current_user (); 
  $user = $current_user->ID;

  do_action( 'bbpnns_digest_show_profile_form', $user);        
}
add_shortcode( 'custom_shortcode', 'custom_shortcode_sc' );
oyegigi
  • 11
  • 4
0

I have tested your Short-code and its working fine on my end..

Please try to pass User ID rather then User login. Because some time user login contains space and special character and function parameter may not accept this.. so try User ID..

$user = $current_user->ID;

If you need more assistance then please let me know..

I will be happy to help you..

Thank You

Yogesh Garg
  • 299
  • 3
  • 10