Title: How to include one-line PHP code in admin_head using a plugin?
Last modified: August 18, 2016

---

# How to include one-line PHP code in admin_head using a plugin?

 *  Resolved [lelion](https://wordpress.org/support/users/lelion/)
 * (@lelion)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/how-to-include-one-line-php-code-in-admin_head-using-a-plugin/)
 * I have some experience with WordPress, and now I have the following question:
 * I have made a minor change to the file admin-header.php located in wp-admin –
   because I run two blogs (to be able to post in two languages – EN and BG, without
   the need of a plugin), I needed an easy switch link between them. I used the 
   following trick:
 * `<a href="/en/<?php echo substr($_SERVER['PHP_SELF'], 4, 200); ?>" title="Switch
   to English">Switch to EN</a>`
 * _(This code will make the switch to the English version while redirecting you
   to the same section you are currently in in the Bulgarian version.)_
 * So far, so good.
 * I simply inserted the code into the admin-header.php file, right after the SIGN
   OUT and MY PROFILE links.
 * Problem is, when upgrading to a new version of wordpress, I have to manually 
   edit the new admin-header.php file, and I was thinking of a better way of doing
   this change.
 * What I have come up with is a “pseudo-plugin”, which simply inserts some code
   in the admin-footer.php file, and then I position it absolutely using CSS _(I
   used as example the Hello Dolly plug-in by Matt, don’t laugh at me, pls:-D)_.
 * So, here it is:
 *     ```
       <?php
   
       // Some simple html code
       function my_test_plugin() {
       echo "<p id='my-test-plugin'>html link goes here
       (<a href='#'>switch to English</a>)
       ";
       }
       // This will insert the paragraph above into
       the admin footer
       add_action('admin_footer', 'my_test_plugin');
   
       // Let's position #my-test-plugin using CSS
       function my_test_css() {
         echo "
         <style type='text/css'>
         #my-test-plugin {
         position: absolute;
         top: 0;
         left: 0;
         margin: 0;
         font-size: 10px;
         color: #f1f1f1;
         font-family: Verdana, Tahoma, Nina;
         }
         </style>
       ";
       }
   
       // This will insert the CSS into the head
       of the admin pages
       add_action('admin_head', 'my_test_css');
   
       ?>
       ```
   
 * Err, that’s all.
 * The simple example above inserts a paragraph with ID “my-test-example” into the
   admin-footer.php, and I style it with CSS so it shows in top left corner of the
   admin pages.
 * So far, so good.
 * If I use this “plugin” I can safely upgrade to a newer version of WordPress without
   worrying that my link will disappear. So I can get rid of the manual editing 
   of admin-header.php (or admin-footer.php). But…
 * What I was unable to make work, is to include one-line PHP code using this method.
 * I can include static html easily, but I can’t insert PHP. And I’d like to use
   my simple one-line PHP script to transfer me always to the corresponding section
   in the other version of my blog. Let’s say I’d like to achieve something like
   this:
 *     ```
       <?php
   
       // Here I'd like to insert the following PHP code
       to the admin-header.php file, and then the PHP
       code to be executed within that file after its
       inclusion:
       function my_test_plugin() {
       echo "
       <p id='my-test-plugin'>
       <a href="/en/<?php echo substr($_SERVER['PHP_SELF'], 4, 200); ?>" title="Switch to English">Switch to EN</a>
   
       ";
       }
       // This will insert the code above into the
       admin footer, and after that it will
       be processed as PHP (I'd like it to be processed
       as PHP but currently it doesn't work of course
       add_action('admin_footer', 'my_test_plugin');
   
       // Let's position #my-test-plugin using CSS
       function my_test_css() {
         echo "
         <style type='text/css'>
         #my-test-plugin {
         position: absolute;
         top: 0;
         left: 0;
         margin: 0;
         font-size: 10px;
         color: #f1f1f1;
         font-family: Verdana, Tahoma, Nina;
         }
         </style>
       ";
       }
   
       // This will insert the CSS into the head of the
       admin pages
       add_action('admin_head', 'my_test_css');
   
       ?>
       ```
   
 * Obviously, I cannot make the PHP code inside the `echo` to work – my PHP knowledge
   is limited, but I know from experience this won’t work:) PHP won’t work inside
   PHP 🙂
 * So, how do I do it?
 * Is there a way to achieve this effect?
 * I’ll be very grateful just to be pointed in the right direction:)
 * I read some documentation on plugins, but didn’t find what I needed. I never 
   wrote a plugin myself, but as far as I know, WordPress provides lots of ‘hooks’
   so code can be inserted in various places within WP files. Like the first example
   I have come up with – it inserts some html in the footer and some css in the `
   head` of the WP admin files. But how to include PHP code and make it be processed
   as PHP after the inclusion, I do not know 🙁 And this would help me here and 
   in other possible situations as well – instead of editing manually some core 
   WP files, maybe I’ll be able to make minor changes using tricks like this one–
   after I make it work, of course:)))
 * Thx for any help in advance!

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Thread Starter [lelion](https://wordpress.org/support/users/lelion/)
 * (@lelion)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/how-to-include-one-line-php-code-in-admin_head-using-a-plugin/#post-544561)
 * Anyone can help? Pleeease? 🙂
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [19 years, 1 month ago](https://wordpress.org/support/topic/how-to-include-one-line-php-code-in-admin_head-using-a-plugin/#post-544573)
 * Well, yeah. That won’t work. Try this instead.
 *     ```
       function my_test_plugin() {
       ?>
       <p id='my-test-plugin'>
       <a href="/en/<?php echo substr($_SERVER['PHP_SELF'], 4, 200); ?>" title="Switch to English">Switch to EN</a>
       <?php
       }
       ```
   
 * You can “turn off” php at any moment with ?> and turn it back on with <?php..
   Anything there when php is turned off is sent as straight output. You were trying
   to turn php on inside of an echo command while it was already in php processing
   mode. Better to disable php for a moment when you have a bunch of text to output,
   instead of echoing it.
 *  Thread Starter [lelion](https://wordpress.org/support/users/lelion/)
 * (@lelion)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/how-to-include-one-line-php-code-in-admin_head-using-a-plugin/#post-544596)
 * Thanks, Otto42!
 * I will try this and will see what happens:)
 *  Thread Starter [lelion](https://wordpress.org/support/users/lelion/)
 * (@lelion)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/how-to-include-one-line-php-code-in-admin_head-using-a-plugin/#post-544597)
 * Woooooooooooooooooooooooooow!
 * Thanks! It worrkkkks! :-)))))))))
 *  Thread Starter [lelion](https://wordpress.org/support/users/lelion/)
 * (@lelion)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/how-to-include-one-line-php-code-in-admin_head-using-a-plugin/#post-544603)
 * Alternatively, I could have used:
 *     ```
       function my_test_plugin() {
       echo '<p id="my-test-plugin"><a href="/en/' .
       substr($_SERVER['PHP_SELF'], 4, 200) .
       '" title="Switch to English">Switch to EN</a>
       ';
       ```
   
 * …but I do not even know, why this didn’t occur to me in the first place:-)
 * Both ways it works, and thanks for the help (hope this thread might be useful
   for someone else as well) :)))

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How to include one-line PHP code in admin_head using a plugin?’ is closed
to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * 5 replies
 * 2 participants
 * Last reply from: [lelion](https://wordpress.org/support/users/lelion/)
 * Last activity: [19 years, 1 month ago](https://wordpress.org/support/topic/how-to-include-one-line-php-code-in-admin_head-using-a-plugin/#post-544603)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
