Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to see where I went wrong with this code. I was trying to prepend the avatar and user meta above the admin menu. I get header already sent messages as well since the code is just right under the body tag.

function profile_script() {
echo '<script type="text/javascript">
/* <![CDATA[ */
(function($) {
    $("#adminmenu").prepend($menuprofile);
})(jQuery);
/* ]]> */
</script>';
}
add_action( "admin_footer", 'profile_script' );
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The admin_menu hook is not for displaying the menu, it's actually part of the menu construction process to figure out exactly what goes in the menu, and this is where you should be making calls to functions like add_submenu_page. So when this action is fired, nothing has been printed out yet, there's no head/body tags yet.

But even then I don't see how your code works. Your second function attempts to prepend the code via AJAX, but it's calling a PHP variable in the middle of what is Javascript code. For this to work would require the code ot posess some kind of supernatural psychic powers. That JQuery code has no knowledge of PHP variables.

But lets say it did have those supernatural psychic powers, or say a Google Engineer appeared with soem magic tech that let your javascript reach into the memory of the PHP process ( which has already exited once the page had finished loading anyway ), we have a whole new problem.

Your trying to use the variable $menuprofile, yet that variable is not declared anywhere., and if it was declared, what would it contain? It wouldn't contain your menu profile html, that got echo'd out to the frontend, it didn't get put in a variable called $menuprofile.

Remember, everything in code is there for a reason, it's in a particular order for a particular reason, if you don't know why you put 'echo' in there you should read up because you'll probably spend more time flailing about copy pasting random things on the off chance it works.

So lets start with your JS code:

function profile_script() {
    echo '<script type="text/javascript">
    /* <![CDATA[ */
    (function($) {
        var menuprofile = ?; // where do we get this value from?
        $("#adminmenu").prepend(menuprofile);
    })(jQuery);
    /* ]]> */
    </script>';
}
add_action( "admin_footer", 'profile_script' );

We need to get menuprofile from somewhere, we have 2 choices:

  • Echo out the code as a js string and create a html element out of it in JS
  • Echo it out in PHP then grab the element using JQuery and move it to the correct position

I'm going to use the second option as an example:

function profile_script() {
    echo '<div id="example>Hello World</div>";
    echo '<script type="text/javascript">
    /* <![CDATA[ */
    (function($) {
        var menuprofile = $("#example"); // grab that html element from before
        $("#adminmenu").prepend(menuprofile);
    })(jQuery);
    /* ]]> */
    </script>';
}
add_action( "admin_footer", 'profile_script' );

You should also namespace your funcitons or at least prefix them to prevent collisions with other people writing functions who had the same naming ideas. You should also wait until the DOM is ready:

function wpse73532_profile_script() {
    echo '<div id="example>Hello World</div>";
    echo '<script type="text/javascript">
    /* <![CDATA[ */
    jQuery(document).ready(function(){
        var menuprofile = jQuery("#example"); // grab that html element from before
        jQuery("#adminmenu").prepend(menuprofile);
    });
    /* ]]> */
    </script>';
}
add_action( "admin_footer", 'wpse73532_profile_script' );

Instead have you considered modifying the admin bar? Also what will you do when the menu is collapsed? or if the CSS and menu layout changes in a future version of WordPress?

Disclaimer: Copious quantities of sarcasm were used in this answer, but no offence was intended

share|improve this answer
    
well you caught me I don't know php, jquery, and ajax well enough. I get what you are saying about the submenu_page hook instead of admin_menu but the what happens to the profile function? –  user1757516 Nov 21 '12 at 22:22
    
the menu will not collapse due to the current user profile avatar and info being inside the admin menu. –  user1757516 Nov 21 '12 at 22:24
    
That's okay, it's just important to think about things logically and step back, sometimes you read through what you've done and think "yikes why did I write that", and it'll keep happening long after you've figured most things out, I know I do. –  Tom J Nowell Nov 21 '12 at 22:49
    
The profile function should be removed, I dont see any need for it and it just seems like a good way of confusing both of us =] Modify the other function so that the example div contains all the stuff you're wanting. Also the menu may very well collapse, and it will auto-collapse to save space at small screen resolutions, your profile element will still be there it'll just have most of its width hidden, try it out and style accordingly –  Tom J Nowell Nov 21 '12 at 22:50
    
it didn't work: function profile_script() { global $current_user; get_currentuserinfo(); echo '<div id="profileinmenu">'; echo get_avatar( $current_user->ID, 64 ); echo '</div>'; echo '<script type="text/javascript"> /* <![CDATA[ */ jQuery(document).ready(function(){ var menuprofile = jQuery('#profileinmenu'); jQuery("#adminmenu").prepend(menuprofile); }); /* ]]> */ </script>'; } add_action( "admin_footer", 'profile_script' ); –  user1757516 Nov 21 '12 at 23:43
show 12 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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