-1

So, this is the php code that I have:

<?php global $royal_profile;?>
    <div class="profile-img" data-key="profilepicture">
        <?php echo get_avatar( get_current_user_id(), 64 ); ?>
    </div>

Then here is my attempt to turn it into a shortcode (so that I can use in a menu as I realized that I can't simply put php into the menu.

function wpse_143641_profile_shortcode( $atts ) {
  return <<<royal_profile

       <?php global $royal_profile;?>
    <div class="profile-img" data-key="profilepicture">
        <?php echo get_avatar( get_current_user_id(), 64 ); ?>
    </div>

royal_profile;
 }
add_shortcode( 'royal_profile', 'wpse_143641_profile_shortcode' );

When I inspect the element on the frontend, following is the result I got.

enter image description here

What am I doing wrong?

Thanks

  • You are trying to put a shortcode in a menu? – s_ha_dum Jun 5 '15 at 5:13
  • mhmm, I am trying replace the menu item title with the avatar. – steve Kim Jun 5 '15 at 5:16
  • 2
    It was asked before by you. You didn't need a "notice" to know that. – s_ha_dum Jun 5 '15 at 5:24
  • 3
    Be nice. – Rarst Jun 5 '15 at 7:48
  • 3
    Please mind your manners. – Pieter Goosen Jun 5 '15 at 12:42
0

Hi You can use this code.

function wpse_143641_profile_shortcode( $atts ) 
  {
   echo get_avatar( get_current_user_id(), 64 );
  }
add_shortcode( 'royal_profile', 'wpse_143641_profile_shortcode' );

And you should add the short code in your editor

[royal_profile]

or template

do_shortcode('[royal_profile]')
  • Never echo anything from a shortcode, all output should be returned. Echoing output will result in unexpected behavior – Pieter Goosen Jun 5 '15 at 13:11
  • Though this answer was accepted I don't see how it could answer the question-- "...into a shortcode (so that I can use in a menu as I realized that I can't simply put php into the menu." Menus don't, to my knowledge, process shortcodes. There would be additional work to be done. – s_ha_dum Jun 5 '15 at 14:57

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