0

I basically have a contact form which generates a PDF output using a third-party plugin. The plugin allows for the use of shortcodes to add content to the final PDF.

I want to be able to display the avatar image of the currently logged in user so the PDF is customized for them and not just a default look. The shortcode below works great and if I manually change the user_id it will changes the image perfectly to whom ever that ID belongs to.

However I can't seem to find for the shortcode to dynamically change the user_id number to represent who's actually logged in ... or at least i've not understood clearly the route I need to take from other support posts.

[user_profile_avatar size="original" user_id="1"]

Can someone kindly guide me how to do this and where I need to place code etc. Would be much appreciated. I have some coding knowledge but still learning. I've tired the plugin developers wordpress support page but they haven't answered anyone's request in months.

6
  • 2
    You will need to edit the shortcode function to populate that field for you. You can easily get the current user id using get_current_user_id().
    – Abhik
    Commented May 15, 2022 at 15:16
  • Thanks for the reply but I'm not 100% sure I know what you mean. Where would I normally find the shortcode function to be able to edit and what would the shortcode look like once edited to use the get_current etc ?
    – Tom Kidd
    Commented May 15, 2022 at 15:38
  • It would be in your currently active theme/plugins files. search for the string user_profile_avatar, you will get the function name.
    – Abhik
    Commented May 15, 2022 at 17:45
  • 1
    Ideally you shouldn't be modifying themes or plugins you didn't author. Any changes would just be wiped out by updates. If this is a third party plugin you need to ask the author if there's a supported way to do this. Commented May 16, 2022 at 3:02
  • Thanks everyone, appreciate the feedback. The plugin author does provide this code; "If you want to assign dynamic user id in shortocde without using visual editor <?php //for the logged user, Current user object $user = wp_get_current_user(); echo do_shortcode('[user_profile_avatar user_id="<?php $user->id ?>" size="original" align="aligncenter" link="image" target="_blank"]'. $user->display_name .'[/user_profile_avatar]'); ?> I'm not sure where this goes & how to implement it in the actual shortcode. The author isn't replying to any support tickets
    – Tom Kidd
    Commented May 16, 2022 at 10:11

1 Answer 1

0

One option is to register your own shortcode to act as a wrapper for the plugin's shortcode. In the wrapper callback you would then retrieve the current user's ID and pass it to the actual shortcode.

Simplified example,

add_shortcode( 'my_user_profile_avatar', 'my_user_profile_avatar_callback' );
function my_user_profile_avatar_callback( $atts, $content = null ) {
  // (int) The current user's ID, or 0 if no user is logged in.
  return do_shortcode(
    '[user_profile_avatar size="original" user_id="' . get_current_user_id() . '"]'
  );
}

N.B. not tested, but should work.

This would then be used as [my_user_profile_avatar] wherever shortcodes are accepted.

If you can find out the name of the actual shortcode's callback, then using it directly instead of do_shortcode() inside your shortcode's callback would be more efficient. Something along these lines,

function my_user_profile_avatar_callback( $atts ) {
  // (int) The current user's ID, or 0 if no user is logged in.
  $atts['user_id'] = get_current_user_id();
  // dig into the plugin's source code to find the callback's name
  return original_shortcode_callback_function($atts); // might require more parameters
}

The custom shortcode registration can be done in your (child) theme's functions.php file or with a custom plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.