WordPress.org

Forums

[resolved] How to include one-line PHP code in admin_head using a plugin? (6 posts)

  1. lelion
    Member
    Posted 8 years ago #

    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!

  2. lelion
    Member
    Posted 8 years ago #

    Anyone can help? Pleeease? :-)

  3. 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.

  4. lelion
    Member
    Posted 8 years ago #

    Thanks, Otto42!

    I will try this and will see what happens:)

  5. lelion
    Member
    Posted 8 years ago #

    Woooooooooooooooooooooooooow!

    Thanks! It worrkkkks! :-)))))))))

  6. lelion
    Member
    Posted 8 years ago #

    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) :)))

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags