in Design
10 Very Useful WordPress Hacks
Writen by Bogdan+ / Comments Off on 10 Very Useful WordPress Hacks
WordPress is a great platform but it’s not perfect, so that’s why many users want to optimize it a little. A friend of mine sent me a document with a bunch of wordpress scripts, so I wanted to share some of them with you. Please note that I’m not the one who wrote them; these scripts are so popular that I couldn’t find the authors for them. So, let’s thanks the unknown coders who make our life easier with their work. :)
How to open links in new window by default
First, open quicktags.js in the wp-admin folder.
Find:
[codesyntax lang=”php”]
if (!edCheckOpenTags(i)) {
var URL = prompt('Enter the URL' ,defaultValue);
if (URL) {
edButtons[i].tagStart = '<a href="' + URL + '">';
edInsertTag(myField, i);
}
}
[/codesyntax]
Replace with:
[codesyntax lang=”php”]
if (!edCheckOpenTags(i)) {
var URL = prompt('Enter the URL' ,defaultValue);
if (URL) {
edButtons[i].tagStart = '<a href="' + URL + '"';
if (URL!='http://')
{
var defaultTarget = prompt('Enter the Target' ,'_blank');
if (defaultTarget) edButtons[i].tagStart += ' target="' + defaultTarget + '"';
} edButtons[i].tagStart +='>';
edInsertTag(myField, i);
}
}
[/codesyntax]
How to prevent search bots from indexing search results
Add this script in the <head> section of your header.php file.
[codesyntax lang=”php”]
<?php if(is_search()) { ?>
<meta name="robots" content="noindex, nofollow" />
<?php }?>
[/codesyntax]
How to add “edit this” button on posts
All you have to do is to put this script where the_content() is, pobably in the single.php or page.php file. This link will only be visible if the user is administrator of author.
[codesyntax lang=”php”]
<?php edit_post_link(__('Edit This')); ?>
[/codesyntax]
How to add a control panel link visible only for the admins, and also and edit button
Put it inside the loop and your blog will have “Login” and “Edit” for Admins only
[codesyntax lang=”php”]
// Begin WordPress loop
<?php
get_header();
if (have_posts()) : while (have_posts()) : the_post();
?>
// Admin only code
<?php if (current_user_can("manage_options")) : ?>
<a href="<?php echo bloginfo("siteurl") ?>/wp-admin/">Admin</a>
<?php edit_post_link(‘Edit’, ”, ”); ?>
<?php endif; ?>
// code here, get contents
...
...
// End WordPress loop
<?php endwhile; else: ?>
Sorry, no pages matched your criteria.
<?php endif; get_footer(); ?>
[/codesyntax]
Create a custom database error page
Create and add the script above in “db-error.php” directly inside your /wp-content/ folder and WordPress will automatically use that when there is a database connection problem.
[codesyntax lang=”php”]
<?php // custom WordPress database error page
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 600'); // 1 hour = 3600 seconds
// If you wish to email yourself upon an error
// mail("your@email.com", "Database Error", "There is a problem with the database!", "From: Db Error Watching");
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Database Error</title>
<style>
body { padding: 20px; background: red; color: white; font-size: 60px; }
</style>
</head>
<body>
You got problems.
</body>
</html>
[/codesyntax]
How to embed a page inside another page
This script can be used in the regular page loop. All you have to do is to replace **ID** with the ID of the page that you want to embed.
[codesyntax lang=”php”]
<?php $recent = new WP_Query("page_id=**ID**"); while($recent->have_posts()) : $recent->the_post();?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile; ?>
[/codesyntax]
How to add the author bio at the end of the post
This script will display the user name and bio from User settings area in the Admin Panel.
[codesyntax lang=”html4strict”]
<div class="author-box"> <div class="author-pic"><?php echo get_avatar( get_the_author_email(), '80' ); ?></div> <div class="author-name"><?php the_author_meta( "display_name" ); ?></div> <div class="author-bio"><?php the_author_meta( "user_description" ); ?></div> </div>
[/codesyntax]
How to create a Facebook “Like” button
Add this script into single.php template underneath where it outputs the content of the post.
[codesyntax lang=”php”]
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo rawurlencode(get_permalink()); ?>&layout=standard&show-faces=true&width=450&action=like&font=arial&colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>
[/codesyntax]
Move WordPress Admin Bar to the Bottom
Add this CSS to your CSS file, add all the code to your functions.php file in order to make it work.
[codesyntax lang=”php”]
function fb_move_admin_bar() {
echo '
<style type="text/css">
body {
margin-top: -28px;
padding-bottom: 28px;
}
body.admin-bar #wphead {
padding-top: 0;
}
body.admin-bar #footer {
padding-bottom: 28px;
}
#wpadminbar {
top: auto !important;
bottom: 0;
}
#wpadminbar .quicklinks .menupop ul {
bottom: 28px;
}
</style>';
}
// on backend area
add_action( 'admin_head', 'fb_move_admin_bar' );
// on frontend area
add_action( 'wp_head', 'fb_move_admin_bar' );
[/codesyntax]
Reset Admin Password Through Database
You’ll need to be able to run SQL on that database, like for example, through phpMyAdmin.
[codesyntax lang=”php”]
UPDATE `wp_users` SET `user_pass` = MD5( 'new_password_here' ) WHERE `wp_users`.`user_login` = "admin_username";
[/codesyntax]
Please not that you use these hack at your own risk.

