Showing posts with label functions.php. Show all posts
Showing posts with label functions.php. Show all posts

Sunday 6 October 2013

How To Change WordPress Author URL Slug


In WordPress, an author's profile is by default accessible by using the yoursite.com/author/name. But it's really easy to change the default author url slug/base. All be just need is a simple snippet.

Kevin Chard found a great code to change the default yoursite.com/author/name to yoursite.com/profile/name. Just add following snippet to your functions.php file:

add_action('init', 'cng_author_base');
function cng_author_base() {
    global $wp_rewrite;
    $author_slug = 'profile'; // change slug name
    $wp_rewrite->author_base = $author_slug;
}

Replace profile on line 4 by any slug you want.

Saturday 5 October 2013

Add Short URL Below Posts In WordPress Without Plugins


Previously I shared a post on this blog about adding a Short Link Widget Below Posts In Blogger, which is a great tutorial for Blogger admins. But I never shared anything like that for WordPress blogs. There are a lot of plugins to add this short link widget below your posts, but we're going to do this without any crappy plugin.

Plus, instead of going with one short link service, you can choose between two short links services. Both services are well known, and easy to use. These two short links services are tr.im and TinyURL.

Add Short URL Below Posts In WordPress:

Add one of the following two codes to your functions.php file:

For tr.im:

function getTrimUrl($url) { $tinyurl = file_get_contents("http://api.tr.im/api/trim_simple?url=".$url); return $tinyurl; }

For TinyURL:

function getTinyUrl($url) { $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url); return $tinyurl; }

After adding one of the above codes to your functions.php file, add following code in your single.php file, within the loop:

<?php $turl = getTinyUrl(get_permalink($post->ID)); echo 'Tiny Url for this post: <a href="'.$turl.'">'.$turl.'</a>' ?>

Above code is for a hyperlink style short link. You can also add a text box style widget with the following code:

<?php $turl = getTinyUrl(get_permalink($post->ID)); echo 'Tiny Url for this post: <input readonly="true" type="text" value="'.$turl.'"/>' ?>

Save your files, and That's IT!

Thursday 3 October 2013

How To Disable HTML In WordPress Comments



I really hate spammers. They make me sick with their spam comments with all the bold text and hyperlinks. I know that I'm not the only one with this crappy problem, since spammers are everywhere these days. Some of them should write a book about "The life of a Spammer."

You can easily disable HTML in WordPress Comments.So if someone uses the strong code, it will not bold the text etc. It's really important for a WordPresser to disable HTML comments to prevent spammers from commenting with HTML formatted comments.

Just add following code in your functions.php file:

// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {

// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);

// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", '&apos;', $incoming_comment['comment_content'] );

return( $incoming_comment );
}

// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {

// Put the single quotes back in
$comment_to_display = str_replace( '&apos;', "'", $comment_to_display );

return $comment_to_display;


That's IT!

Tuesday 1 October 2013

Hide Dashboard Login Errors In WordPress

In this tutorial, I'm going to show how you can easily hide login errors in WordPress. It's a great way to protect from WordPress from hackers. Whenever you login with the correct username but with the wrong password, a message appears saying "Error: Incorrect Password." WordPress has now given a clue to hacker that the username entered is in the system, and that they simply need to crack its password.

Similarly, an "Error: Invalid username" also appears when you enter an unavailable username. It's better for you to prevent hacking by hiding this error message. In order to keep this from happening, you need to add this code to your functions.php file:

add_filter('login_errors', create_function('$a', "return null;"));

This filter code will remove error message from the login page. The error box will still appear, but without any text.

Monday 30 September 2013

How To Create A Custom Dashboard Widgets In WordPress


Earlier this hour I was surfing thru WordPress' official codex website and I found something very interesting to write about. Okay! I know you guys are so exited about this great trick.

The Dashboard Widgets API (added in WP 2.7) makes it very simple to add new widgets to the administration dashboard. It only takes a few minutes to create a simple dashboard widget for your blogger. It can be a great way to make your plugin and themes even more useful.

How To Create A Custom Dashboard Widgets:

 This code would go in one of your plugin's files, or in your theme's functions.php:

/**
 * Add a widget to the dashboard.
 *
 * This function is hooked into the 'wp_dashboard_setup' action below.
 */
function example_add_dashboard_widgets() {

    wp_add_dashboard_widget(
                 'example_dashboard_widget',         // Widget slug.
                 'Name of your widget',          // Title.
                 'example_dashboard_widget_function' // Display function.
        );   
}
add_action( 'wp_dashboard_setup', 'example_add_dashboard_widgets' );

/**
 * Create the function to output the contents of our Dashboard Widget.
 */
function example_dashboard_widget_function() {

    // Display whatever it is you want to show.
    echo "<p>Welcome to BWidgets! Need help? Contact the developer <a href="mailto:yourusername@gmail.com">here</a>.</p>";

That's it! Red part in the above code is the title and content area of our widget. Don't forget to leave a comment if you need any help with this tutorial.

Friday 27 September 2013

How To Add Extra Fields To WordPress User Profile Page


Yesterday I post an article about removing some extra fields (AIM, Yahoo IM, and Jabber) from WordPress' user profile page area. It's time to dig even more deeper into this trick. It's time for us to learn about adding some extra fields to user profile page.

The code below will show you how to add additional Twitter and Facebook fields, but you can use it to add any other field that you like. Add following php code to your theme's functions.php file (from theme editor):

function my_new_contactmethods( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = 'Twitter username (withour @';
//add Facebook
$contactmethods['facebook'] = 'Facebook';
return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);


That's it.

Thursday 26 September 2013

How To Remove User Contact Info From WordPress Profile Page


After writing bunch of articles about WordPress' wp-config.php file, it's time for me to write some articles about the powerful functions.php file. And I'll start from the start. In this post, I'm sharing a very simple and useful trick for WordPressers (WordPressers :p).

Most of us never ever used three fields on the WordPress profile page and so we want to remove those fields. They are the fields for AIM, Yahoo IM, and Jabber. It's time to get rid of these fields:

Add following php code to your theme's functions.php file (from theme editor):

add_filter('user_contactmethods','hide_profile_fields',10,1);
function hide_profile_fields( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);
return $contactmethods;
}


That's it..!!

Thursday 19 September 2013

3 Ways To Disable WordPress AutoSave


This is my second post of the day here on BWidgets, and it's also about Daniel Bryan wp-config.php file and WordPress AutoPost. This time I'll show you one three ways to disable WordPress AutoSave feature.

When editing a post, the changes you make are automatically saved every 2 minutes. You can also use our last trick to Modify AutoSave Interval.

By Editing wp-config.php File:

This is the easiest way to disable WordPress AutoSave feature. To modify autosave interval simply open wp-config.php (located in root of WordPress installation) and add following code to the end of file:

define('AUTOSAVE_INTERVAL', 86400);

We've set WordPress AutoSave interval to 86400 seconds which is an entire day. So this effectively disables the autosave functionality. Thanks to Jacob Nicholson for the idea.

By Editing post.php File:

This is the smartest way to disable AutoSave feature without any heavy codes. Thanks to Shane G. for sharing this trick on a WordPress forum.

  • Open your wp-admin/post.php file and wp-admin/post-new.php files.
  • You will find this line of code there:

wp_enqueue_script('autosave');

  • Add // to the beginning of this code. It'll look something like this:

// wp_enqueue_script('autosave');

The AutoSave option will be disabled for your existing and new posts.

By Editing functions.php File:

Last but not the least. This is the most preferred way to disable this feature, as we don't have to re-edit our post.php and post-new.php file after every WordPress update, nor we have to adjust AutoSave interval. This trick will simple disable AutoSave feature. Thanks to Egill R. Erlendsson for sharing this trick on a WordPress forum.

Simple throw this in your functions.php file:

add_action( 'admin_init', 'disable_autosave' );
function disable_autosave() {
        wp_deregister_script( 'autosave' );
}

That's it folks. Thanks for all amazing guys that I mentioned on the article for sharing these amazing tricks around the internet. Don't forget to give us a backlink if you're sharing this article on your blog.

Sunday 12 May 2013

Easiest Way To Remove WordPress Logo From Admin Bar


Today, I'm going to share the easiest way to remove this annoying WordPress logo/links from your admin bar without using any plugin.

Last month, I tried to remove this whole WP logo menu from my site by editing those hard php files, and I was successful in it. But it was a temporary success, because soon I saw an error message on my dashboard.

Now, let me share two simple ways to remove that annoying thing from your admin bar.

Easiest Way: By Using CSS:

It's funny, but you can also remove that logo menu by adding display:none; CSS property. I know some of you guys are screwed up over this fact. Let me show you how. I'm using this CSS hack on this WP site.

  • Visit your File Manager or FTP from your hosting provides. Mine is from GoDaddy, which looks something like this:


  • Now in your file manger, visit webroot/wp-includes/css/admin-bar.min.css
  • In admin-bar.min.css, add following css code:

li#wp-admin-bar-wp-logo.menupop{display:none;}

  • Save/Update your admin-bar.min.css file and that's it.

Important: You may have to clear your browser's cache to see this change or you can use private window to see this simple css hack.

By Using php:

If you're not satisfied with our css hack, then you can also use php snippet to hide this menu list. Credits for this snippet goes to WP-Snippets.

  • Add this code to your functions.php file to remove the WordPress logo on admin bar:

<?php
function annointed_admin_bar_remove() {
        global $wp_admin_bar;

        /* Remove their stuff */
        $wp_admin_bar->remove_menu('wp-logo');
}

add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove', 0);
?>

That's it...Don't forget to leave a comment.

Saturday 11 May 2013

Bulk Delete WordPress Users Based On Their Role


Owning a blog with public registration is a huge mess, especially with all those nasty spammers. It's really hard to get rid of all those nasty spammers. In future, I'll share a very effective way to get rid of all future spam users, but first it's important to delete existing spam users. For that we have to delete all public registered accounts.

You can also use this tutorials for any other thing, since it's always not about spammers. Instead of deleting users of a specific role one-by-one, you can bulk delete them. As always, "Bulk Is Better" - Hardeep Asrani (Me)

As I know, there are five different roles in WordPress, which are admins, developers, authors, editors, contributors and subscribers. You can easily delete users of any of these roles easily by running a very simple code.

How To Bulk Delete All Users Based On Their Role:

  • Go to your WP Dashboard > Appearance > Editor
  • Open the functions.php file from the right sidebar.
  • Add the following lines of code (based on user role) to your functions.php file and publish.

Codes For User Roles:

For Admin:

function remove_administrators() {
    global $wpdb;
    $args = array( 'role' => 'Administrator' );
    $administrators = get_users( $args );
    if( !empty($administrators) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $administrators as $administrator ) {
            if( wp_delete_user( $administrator->ID ) ) {
                $i++;
            }
        }
        echo $i.' Administrators deleted';
    } else {
        echo 'No Administrators deleted';
    }
}
remove_administrators();

For Developers:

function remove_developers() {
    global $wpdb;
    $args = array( 'role' => 'Developer' );
    $developers = get_users( $args );
    if( !empty($developers) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $developers as $developer ) {
            if( wp_delete_user( $developer->ID ) ) {
                $i++;
            }
        }
        echo $i.' Developers deleted';
    } else {
        echo 'No Developers deleted';
    }
}
remove_developers();

For Authors:

function remove_authors() {
    global $wpdb;
    $args = array( 'role' => 'Author' );
    $authors = get_users( $args );
    if( !empty($authors) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $authors as $author ) {
            if( wp_delete_user( $author->ID ) ) {
                $i++;
            }
        }
        echo $i.' Authors deleted';
    } else {
        echo 'No Authors deleted';
    }
}
remove_authors();

For Editors:

function remove_editors() {
    global $wpdb;
    $args = array( 'role' => 'Editor' );
    $editors = get_users( $args );
    if( !empty($editors) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $editors as $editor ) {
            if( wp_delete_user( $editor->ID ) ) {
                $i++;
            }
        }
        echo $i.' Editors deleted';
    } else {
        echo 'No Editors deleted';
    }
}
remove_editors();

For Contributors:

function remove_contributors() {
    global $wpdb;
    $args = array( 'role' => 'Contributor' );
    $contributors = get_users( $args );
    if( !empty($contributors) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $contributors as $contributor ) {
            if( wp_delete_user( $contributor->ID ) ) {
                $i++;
            }
        }
        echo $i.' Contributors deleted';
    } else {
        echo 'No Contributors deleted';
    }
}
remove_contributors();

For Subscribers:

function remove_subscribers() {
    global $wpdb;
    $args = array( 'role' => 'Subscriber' );
    $subscribers = get_users( $args );
    if( !empty($subscribers) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $subscribers as $subscriber ) {
            if( wp_delete_user( $subscriber->ID ) ) {
                $i++;
            }
        }
        echo $i.' Subscribers deleted';
    } else {
        echo 'No Subscribers deleted';
    }
}
remove_subscribers();

For None:

There is also a role in WordPress called "none." Here is the code to bulk delete users with "none" rule. I'm not 100% about this code, so please leave a comment if it's working:

function remove_noroleusers() {
    global $wpdb;
    $args = array( 'meta_key' => 'wp_capabilities','meta_value' => 'a:0:{}','meta_compare' => '=') ;
    $noroleusers = get_users( $args );
    if( !empty($noroleusers) ) {
        require_once( ABSPATH.'wp-admin/includes/user.php' );
        $i = 0;
        foreach( $noroleusers as $noroleuser ) {
            if( wp_delete_user( $noroleuser->ID ) ) {
                $i++;
            }
        }
        echo $i.' Users deleted';
    } else {
        echo 'No Users deleted';
    }
}
remove_noroleusers();

Final Step:

Once you have went to your site go back to your functions file and remove the code. Don't forget to leave a comment.

Tuesday 7 May 2013

Add Numbered Page Navigation To WordPress


If your WP blog has a large number of posts, then adding numbered page navigation is a great way to give your readers an easy navigation to your blog's archives. The default WordPress pagination comes with ‘older posts’ and ‘newer posts’ but those look good if your blog has only few articles.

In this tutorial, I'll tell you how to add numbered page navigation to your WordPress, both manually and automatically. Let's get started with automatic method (by using a plugin).

By Using A Plugin:

I recommend you to use this way, since it's easy and much better. Click here to download or search and install WP-PageNavi to your blog. Activate this plugin and that's it.

Custom Method:

You can also manually add numbered page navigation to your WP, just follow these simple steps:

  • Go to your WP Dashboard > Appearance > Editor
  • Open the functions.php file from the right sidebar.
  • Add the following lines of code to your functions.php file and publish.

function pagination($pages = ”, $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == ”)
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo “<div class=\”pagination\”><span>Page “.$paged.” of “.$pages.”</span>”;
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo “<a href=’”.get_pagenum_link(1).”‘>&laquo; First</a>”;
if($paged > 1 && $showitems < $pages) echo “<a href=’”.get_pagenum_link($paged – 1).”‘>&lsaquo; Previous</a>”;
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? “<span class=\”current\”>”.$i.”</span>”:”<a href=’”.get_pagenum_link($i).”‘ class=\”inactive\”>”.$i.”</a>”;
}
}
if ($paged < $pages && $showitems < $pages) echo “<a href=\”".get_pagenum_link($paged + 1).”\”>Next &rsaquo;</a>”;
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo “<a href=’”.get_pagenum_link($pages).”‘>Last &raquo;</a>”;
echo “</div>\n”;
}
}

  • One-by-one open index.php, archive.php, search.php, and loop.php (only if exist) file from the right sidebar.
  • Find ‘older posts’ or ‘older entries’ and replace those lines with the below code.

<?php if (function_exists(“pagination”)) {
pagination($additional_loop->max_num_pages);
} ?>

That's it! You can also add some CSS to make it look better.

  • On editor, open the style.css file from the right sidebar.
  • Add following CSS add the bottom of style.css file:

.pagination {
clear:both;
padding:20px 0;
position:relative;
font-size:11px;
line-height:13px;
}
.pagination span, .pagination a {
display:block;
float:left;
margin: 2px 2px 2px 0;
padding:6px 9px 5px 9px;
text-decoration:none;
width:auto;
color:#fff;
background: #555;
}
.pagination a:hover{
color:#fff;
background: #3279BB;
}
.pagination .current{
padding:6px 9px 5px 9px;
background: #3279BB;
color:#fff;
}

That's it! Don't forget to leave a comment.

Friday 3 May 2013

Adding Favicon To WordPress Admin Panel


As I noted before, I recently migrated my WWE blog from Blogger To WordPress, and I'm having too much fun with all amazing WordPress features and plugins. Soon I'll post an article about some cool WordPress plugins so don't forget to subscribe to BWidgets (from your favorite feed reader or email updates).

Let's get back to topic. After adding a favicon to our WordPress blog, we notice that our favicon is not appearing in our blog's admin panel. It's good to add a favicon to your blog's admin panel. Also, it's more important if your blog's users are also allowed to write for your site.

I tried to add it to my blog's admin panel by editing php files via ftp, and I was lost in those heavy scripted files. But after few minutes, I was successful in changing my blog's admin panel's favicon. So it's start the tutorial:

  • Visit your WordPress hosting provides. For example, I bought mine from GoDaddy.
  • Now visit your file manager or ftp client. Below is a screenshot of my file manager:


  • In my hosting, webroot is my root folder. Upload your favicon image in this webroot folder or skip to next point if your favicon is already uploaded..
  • Now visit webroot/wp-admin/admin-header.php. Now, in admin-header.php search for following line:

<title><?php echo $admin_title; ?></title>

  • Post following code just below it:

<link rel="shortcut icon" href="FAVICON IMAGE URL" />

  • Don't forget to replace FAVICON IMAGE URL with your the link of your favicon. Just like following image:


Now save your file and that's it!! If you still can't see favicon then clear your browser's cache files or try another browser. More WordPress tutorials are coming soon. I'll post new things as soon as I'll explore them :)

Another Way:

JP shared another great way in comments to permanently add a favicon to your WordPress' Admin Panel. He posted following in the comments:

Are you editing Wordpress core files? I strongly discourage that. Any changes you make to core files will be overwritten when you update WordPress. Instead, put something like this into your theme's functions.php file, or into a custom plugin.

add_action('admin_head', 'show_favicon');
function show_favicon() {
echo '<link href="FAVICON IMAGE URL" rel="icon" type="image/x-icon">';
}

Code that you put in either of those two places will survive updates.

Popular Posts

 
Powered by Blogger.