Shortcode using WP_User object

677 Views Asked by At

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!

2

There are 2 best solutions below

0
Yogesh Garg On

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

0
oyegigi On

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' );