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

How To Recover Your MySQL root Password

I just needed to recover a lost MySQL root password on a clients server recently and thought I’d share the method I used.

[ad name=”Adsense – text only”]

Actually it’s not a method for recovering but you can change the forgotten password to a new one.
All you need is to follow this 6 easy steps:

Step 1: Stop the mysql service

# /etc/init.d/mysql stop

Expected Output:

Stopping MySQL database server: mysqld.

Step 2: Start the MySQL server without password

# mysqld_safe --skip-grant-tables &

Expected Output:

[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started

Step 3: Connect to your MySQL server using the MySQL client

# mysql -u root

Expected Output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Step 4: Setup a new MySQL password for the root user

mysql> use mysql;
mysql> update user set password=PASSWORD("YOUR-NEW-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit;

Expected Output:

Database changed

Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Bye

Step 5: Stop the MySQL Server

# /etc/init.d/mysql stop

Expected Output:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended

[1]+  Done                    mysqld_safe --skip-grant-tables

Step 6: Start the MySQL server and test it

# /etc/init.d/mysql start
# mysql -u root -p

Expected Output:

Starting service MySQL         done

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is xx
Server version: x.x.xx XXXXX MySQL RPM

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Now you should be all done!
I hope this helps you as much as it did for me.

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.