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

add_filter('show_admin_bar','__return_false');

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

add_action('admin_print_scripts-profile.php','hideAdminBar');
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; }
        </style>
        "
;
}
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;
    $wp_admin_bar->remove_menu('comments');
}
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! :)

Categories
code

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!

Categories
code

The F(ucking) U(ncrackable) E(ncryption) Function in PHP

I think following function doesn’t need any more description but the inline comments.

[ad name=”Adsense – text only”]

The Function:

// f(ucking) u(ncrackable) e(ncryption) function
function fue($hash,$times){
    // Execute the encryptions as many times as the user wants
    for($i=$times;$i>0;$i--) {
        // Encode with base64...
        $hash=base64_encode($hash);
        // and md5...
        $hash=md5($hash);
        // sha1...
        $hash=sha1($hash);
        // sha256... (one more)
        $hash=hash("sha256", $hash);
        // sha512
        $hash=hash("sha512", $hash);
    }
    // Finaly, when done, return the value
    return $hash;
}

This function was first introduced by BlackHatDBL.

Categories
code

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;
}