Five Useful WordPress Snippets

Five Useful WordPress Snippets

Code snippets are short functions that can be inserted into your WordPress website theme. Once added, they will change the way that WordPress works or add additional functionality.

Today I would like to share with you all five WordPress snippets that I believe you will find useful. All of these snippets should be added to your theme’s functions.php file.

1. Redirect To Post If Search Results Return One Post

This is one of my favorite code snippets for WordPress because it addresses an issue that exists in the WordPress core.

When you search for something on a WordPress website, you are redirected to a results page that lists all relevant articles. Then you simply choose the article you want to view.

WordPress still takes you to a search results page if there is only one result. The snippet below will redirect a user directly to the post or page in question if there is only one result. This saves them from needlessly viewing the results page.

[php]add_action(‘template_redirect’, ‘redirect_single_post’);
function redirect_single_post() {
if (is_search()) {
global $wp_query;
if ($wp_query->post_count == 1 && $wp_query->;max_num_pages == 1) {
wp_redirect( get_permalink( $wp_query->;posts[‘0’]->;ID ) );
exit;
}
}
}
[/php]

Source

2. Allow PHP in Default Text Widgets

The default text widget in WordPress only allows text and HTML. This can be a bit limiting.

With the following code, you can use PHP within the text widget. This is useful for calling external scripts and executing additional functions.

[php]add_filter(‘widget_text’, ‘php_text’, 99);[/php]
[php]
function php_text($text) {
if (strpos($text, ” . $text);
$text = ob_get_contents();
ob_end_clean();
}
return $text;
}
[/php]
Source

3. Disable the Theme and Plugin Text Editor

Allowing your themes and plugins to be edited through the admin area can prove to be a security risk and can cause you headaches in the future if you allow clients to use it. The following code snippet will disable the theme and plugin editor in the admin area.

[php]// Disable the theme / plugin text editor in Admin
define(‘DISALLOW_FILE_EDIT’, true);
[/php]

4. Allow Contributors to Upload Files

By default, users who are assigned to the contributor user group cannot upload files. This is a pain as it means that they cannot add images to their posts.

I normally address this issue by installing User Role Editor and manually adding the permission to upload files; however there is an alternative way to do it.

You can also allow contributors to upload files by adding this code snippet to your theme’s functions.php file.

[php]if ( current_user_can(‘contributor’) &&; !current_user_can(‘upload_files’) )
add_action(‘admin_init’, ‘allow_contributor_uploads’);

function allow_contributor_uploads() {
$contributor = get_role(‘contributor’);
$contributor->;add_cap(‘upload_files’);
}
[/php]

5. Change the WordPress Login Logo

The WordPress logo is displayed at the top of the login form. While it does no harm, a custom logo may be preferable if you have members, customers, or staff, using the login form on a daily basis.

The following snippet will change the logo to your own custom logo. As you can see, it assumes that you save your custom login logo in the images folder of your theme folder. However, this location can be changed if desired.

[php]function my_custom_login_logo() {
echo ‘
h1 a { background-image:url(‘.get_bloginfo(‘template_directory’).’/images/loginlogo.jpg) !important; }
‘;
}

add_action(‘login_head’, ‘my_custom_login_logo’);
[/php]
Source

Do you know of any other useful WordPress code snippets? If so, please share them in the comment area below 🙂

Thanks,
Kevin

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.