A Drop in the Ocean: Couple of Useful WordPress Hacks

WordPress is something very interesting, versatile and functional. Therefore, apart from usual standard features, you can find extremely original solutions – let’s call them the secrets of wordpress hacks. In this section you will find some recently developed items. In spite of this, the information is appropriate for every user who runs and understands what’s WordPress admin panel and function.php file.

***

Disabling RSS feeds

Upon default, WordPress is up and running this popular feature, as the ribbon RSS, which is great for blogs, and more. But if you use WordPress, as a static site that for some reason does not need RSS, you can disable it quite easily.

This code, which is provided below disables RSS-feeds on your website. Simply paste the code into functions.php.


function cwc_disable_feed() {
wp_die( __('No feed available,please visit our homepage!') );
}
add_action('do_feed', 'cwc_disable_feed', 1);
add_action('do_feed_rdf', 'cwc_disable_feed', 1);
add_action('do_feed_rss', 'cwc_disable_feed', 1);
add_action('do_feed_rss2', 'cwc_disable_feed', 1);
add_action('do_feed_atom', 'cwc_disable_feed', 1);

***

How ’bout to add some thumbnails to RSS

This is a very nice and useful feature that will automatically add thumbnails to RSS. Paste the code into the file functions.php. And do not forget that you need to use the theme in WordPress, which supports the use of thumbnails.


function cwc_rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '' . get_the_post_thumbnail($post->ID) .
'
' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'cwc_rss_post_thumbnail');
add_filter('the_content_feed', 'cwc_rss_post_thumbnail');

***

Setting the “Site is under construction right now”

Sometimes, you need to stop your website when you perform any work there. There are certainly many plug-ins, but also has a simple and quick solution. Just paste the following code in your functions.php file and that’s all. Now your site will be accessible only for administrators. After the performance, do not forget to remove the code.


function cwc_maintenance_mode() {
if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
wp_die('Maintenance, please come back soon.');
}
}
add_action('get_header', 'cwc_maintenance_mode');

***

Need to limit the length of the title?

Sometimes, due to certain features of WordPress website design the name of the post should be cut up to a specified length. Here are two solutions: when you need to shorten the title to the specific number of characters or the number of words.

Limiting the length of the title t a specific number of characters

Paste following function into the functions.php file:


function trim_title_chars($count, $after) {
$title = get_the_title();
if (mb_strlen($title) > $count) $title = mb_substr($title,0,$count);
else $after = '';
echo $title . $after;
}

Change the code in a template, which displays the name of the post – < ? Php the_title ();? > – to this:

< ? php trim_title_chars (30, '...');? >

Here, number 30 is the number of characters to the amount of which the title will be trimmed, and … – Will be after the cut text. There you can insert, either any character or a picture.

Limiting the length of the title to a specific number of words

In this case, all along the lines, just a little different function.

The following code must also be inserted in the file functions.php:


function trim_title_words($count, $after) {
$title = get_the_title();
$words = split(' ', $title);
if (count($words) > $count) {
array_splice($words, $count);
$title = implode(' ', $words);
}
else $after = '';
echo $title . $after;
}

After that change the post title – < ? Php the_title ();? > – to this:

< ? php trim_title_words (3, '...');? >

Here, the number 3 – the number of words from which to trim the title, and … – What will be after the cut text.

***

Changing the name and e-mail of a sender (the “From”) in a letter

WordPress sending letters administrator, or to those who subscribed to the comments on the site, in the “From” substitutes the name that you specified in the profile, and e-mail from there.

You can change this information in the letter, without changing the profile. To do this in the functions.php file of your template to insert the following code:


function res_fromemail($email) { return '[email protected]'; }
function res_fromname($email){ return 'Name'; }
add_filter('wp_mail_from', 'res_fromemail');
add_filter('wp_mail_from_name', 'res_fromname');

where [email protected] – is an e-mail, а Name – is a name which will be displayed in the letter.

***

How to put posts into two columns?

This code allows the list of posts to divide into two columns. For example, if a page is displays 7 posts, the first four posts will be located in the first column, and the remaining three – in the second.

The essence of the php-code added by me to a standard code of the post, is that in certain places to insert html-tags to help with CSS to make posts in the columns.


< ?php get_header(); ? >
< ?php if (have_posts()) { ? >
	< ?php $i = 0; ? >
	< ?php $per_column = ceil($posts_per_page / 2); ? >
	< ?php if ($wp_query->post_count <= $posts_per_page) $per_column = ceil($wp_query->post_count / 2); ? >
			< div class="column" >
	< ?php while (have_posts()) { the_post(); $i++; ? >
				< div class="post" >
					< h2>" rel="bookmark">
					< div class="date">< ?php the_time('d.m.Y') ?>< /div >
					< div class="entry" >
					  
					< /div >
				< /div >< !-- .post -->
		< ?php if ($i == $per_column) { ? >
			< /div >< !-- .column -- >
			< div class="column right" >
		< ?php } ? >
	< ?php } ? >
			< /div >< !-- .column -- >
			< div class="clear" >< /div >
	  	< div class="navigation" >
				<  div class="alignleft">< /div >
				< div class="alignright">< /div >
			< /div >
	< ?php } ? >

< ?php get_sidebar(); ? >
< ?php get_footer(); ? >

Also you need to add this CSS code into the CSS file:


.column {
float: left;
width: 48%;
}
.column.right {
float: right;
}
.clear {
clear: both;
height: 0;
overflow: hidden;
}

Got something to share, feel free to use comments section.

25 Reasons Why Your Website Design Falls Flat.
22 Mar 2022

25 Reasons Why Your Website Design Falls Flat

Designing a website’s become a tough ask these days. Gone are the…

Scouting All the Right Options for Your Java Development Project.
31 Jan 2022

Scouting All the Right Options for Your Java Development Project

In this current age, only the websites and web apps with high-power…

Written by

Alexander B.

Post Comment

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

Managed by Quantum VXenon