blog code

The Ultimate Guide to Extend, Modify or remove the WordPress 3.1 Admin-Bar

Oh, already forgot this article in my drafts:

Since WordPress 3.1, a lot of new cool features were added. One of the most hated loved ones is the admin-bar. In this article I will cover some of the basic hooks to modify it to suit your needs.
Let’s go:

[ad name=”Adsense – text only”]

How to Disable the Admin-Bar

The developers of WordPress made it really easy for you to disable the admin-bar. Simply go to Users > Your Profile and find the section where it says “Show Admin Bar” and tick your choice.
But this is just saved for each users separately. If you are working on a site for a client and you want to get rid of it for each user, then simply open your theme’s function.php and paste the following code:


This will get rid of the admin-bar – if you want to remove the settings in the user profile too, just append this code:

function hideAdminBar(){
    echo "<style type='text/css'>";
    echo "  .show-admin-bar {display:none;}";
    echo "</style>";

Change the CSS (Move to bottom)

By default, the bar shows on top of each page when you are logged in. If you don’t like that, you can hook your own CSS to the admin-bar and move it to the bottom (and do some more crazy stuff with it).
Simply add this code to your functions:

function adminBarCSS() {
        echo "
        <style type='text/css'>
             html { padding-bottom: 28px !important; }
             body { margin-top: -28px; }
             #wpadminbar {  top: auto !important; bottom: 0; }
             #wpadminbar .quicklinks .menupop ul { bottom: 28px; }
add_action('admin_head', 'adminBarCSS');
add_action('wp_head', 'adminBarCSS');

Now it’s up to you to do some more interesting stuff (like moving the bar to the left/right, let it float, or whatever).

Remove Links from the Admin-Bar

If you want to (or need to) remove links in the admin-bar. this small function will help you:

function myAdminBarRender() {
    global $wp_admin_bar;
add_action( 'wp_before_admin_bar_render', 'myAdminBarRender' );

The way this works is just to provide the IDs for each link to the remove_menu() function.
You can find all default links in /wp-includes/admin-bar.php.

Some of the defaults are:

  • my-account / my-account-with-avatar – the first link, to your account
  • my-blogs – the ‘My Sites’ menu (for networked blogs)
  • get-shortlink -provides a Shortlink to that page
  • edit – the edit link
  • new-content – the ‘Add New’ drop down
  • comments – the ‘Comments’ drop down
  • appearance – the ‘Appearance’ drop down
  • updates – the ‘Updates’ drop down

Add Links to the Admin-Bar

Here you have a simple way of putting custom links into the admin-bar.

function myAdminBarRender() {
    global $wp_admin_bar;
        $wp_admin_bar->add_menu( array(
        'parent' => 'new-content',
        'id' => 'new_media',
        'title' => __('Media'),
        'href' => admin_url( 'media-new.php')
    ) );
add_action( 'wp_before_admin_bar_render', 'myAdminBarRender' );

You even can create a custom drop down menu in the admin-bar – just take a look at Michael Martins tutorial to get you started.

That’s it for now – now go and pimp your admin-bar! :)


Get Accurate Script Execution Time in PHP

This small code can be quite helpful if you need to find out what is taking so long to load in your script(s).
It will determine the time taken for your PHP script(s) to execute, accurate to 0.000000000000001 seconds.

[ad name=”Adsense – text only”]

The Code

// put this at the top of your page/script
$exectime = microtime();
$exectime = explode(" ",$exectime);
$exectime = $exectime[1] + $exectime[0];
$starttime = $exectime;

 *   place your script(s) to here

// put this at the bottom of your page/script
$exectime = microtime();
$exectime = explode(" ",$exectime);
$exectime = $exectime[1] + $exectime[0];
$endtime = $exectime;
$totaltime = ($endtime - $starttime);
echo "This page was created in ".$totaltime." seconds";

…and that’s all! Happy debugging!


CSS Print Framework

If you want to improve your layouts for printing, this mini CSS Framework will definitely assist you.

What is a CSS Framework?

“A CSS framework is a library that is meant to allow for easier, more standards-compliant styling of a webpage using the Cascading Style Sheets language. Just like programming and scripting language libraries, CSS frameworks package a number of ready-made options for designing and outlaying a webpage.” – Wikipedia

One of the cool things in this CSS you can find on line 27/28. This part will grab all links of the document and append the URL to the link-names.

[ad name=”Adsense – text only”]

The Source:

body {
width:100% !important;
margin:0 !important;
padding:0 !important;
line-height: 1.4;
letter-spacing:0.2pt; font-family: Garamond,"Times New Roman", serif; color: #000; background: none; font-size: 12pt;

/*Headings */
h1,h2,h3,h4,h5,h6 { font-family: Helvetica, Arial, sans-serif; }

code { font: 10pt Courier, monospace; }
blockquote { margin: 1.3em; padding: 1em;  font-size: 10pt; }
hr { background-color: #ccc; }

/* Images */
img { float: left; margin: 1em 1.5em 1.5em 0; }
a img { border: none; }

/* Links */
a:link, a:visited { background: transparent; font-weight: 700; text-decoration: underline;color:#333; }
a:link[href^="http://"]:after, a[href^="http://"]:visited:after { content: " (" attr(href) ") "; font-size: 90%; }
a[href^="http://"] {color:#000; }

/* Table */
table { margin: 1px; text-align:left; }
th { border-bottom: 1px solid #333;  font-weight: bold; }
td { border-bottom: 1px solid #333; }
th,td { padding: 4px 10px 4px 0; }
tfoot { font-style: italic; }
caption { background: #fff; margin-bottom:2em; text-align:left; }
thead {display: table-header-group;}
tr {page-break-inside: avoid;}

/*hide various parts from the site

#header, #footer, #navigation, #rightSideBar, #leftSideBar


To use this Framework, just attach it to your header and choose “print” as media-type.

<link rel="stylesheet" href="print.css" type="text/css" media="print"/>

via hartija at Google Project Hosting.


Clean Up User-Input & Convert Line-Breaks To BR-tags

A simple PHP function to clean up user input.
It does following:

  • converts & to &amp;
  • converts < to &lt;
  • converts > to &gt;
  • converts to &quot;
  • and converts line-breaks to <br />

[ad name=”Adsense – text only”]

The Function:

function clean($str) {
    $str = preg_replace('/&(?!#[0-9]+;)/s', '&amp;', $str);
    $str = str_replace(array('< ', '>', '"'), array('&lt;', '&gt;', '"'), $str);
    $str = nl2br($str);
    return $str;