1. 云栖社区>
  2. PHP教程>
  3. 正文

15 Cool WordPress Coding Tips You Must Know

作者:用户 来源:互联网 时间:2017-12-01 09:52:07

15 Cool WordPress Coding Tips You Must Know - 摘要: 本文讲的是15 Cool WordPress Coding Tips You Must Know, First released on May 27, 2003, by Matt Mullenweg and Mike Little,WordPresshas come a long way. WordPress is a free an

First released on May 27, 2003, by Matt Mullenweg and Mike Little,WordPresshas come a long way. WordPress is a free and open-source blogging tool and is a content management system based on PHP and MySQL. Due to its ability to provide a great degree of customisation to its user, it is one of the most popular blogging systems today.

15 Cool WordPress Coding Tips You Must Know

Many a times, when we spot a great feature or a function on somebody else’s website or blog, we try to incorporate that in our blog too. Also, if you are working on theme design or you are customising your website, sometimes, digging for codes becomes quite inconvenient. It is a great help if we are able to locate help with tips on WordPress during those times. Following are the cool 15 tips among many which we can make use of when working with WordPress.

1. Show Great Images

To save on space and bandwidth, WordPress doesn’t use the best quality of images on the website. By default, it uses a quality of 90%. But if you are interested in showing or using good high quality images, you can use the following code:

add_filter( 'jpeg_quality', 'your_jpeg_quality' );function your_jpeg_quality() { return 100;}

Source

2. Quick File Uploader

Even though WordPress has lots of uploading functions, but a function called as wp_upload_bits() is particularly interesting as it can be used to upload a file to the uploads directory.

$upload = wp_upload_bits( $_FILES['myfile']['name'], null, file_get_contents( $_FILES['myfile']['tmp_name'] ) );echo 'Well uploaded! The path to this file is ' . $upload['file'] . ' and the url to this file is ' . $upload['url'];

Source

3. Display Recent Comments

Many websites show most recent comments by users with user gravatars and if you want to incorporate the same, simply paste the following code anywhere you would like them to be displayed.

<?php	$query = "SELECT * from $wpdb->comments WHERE comment_approved= '1'	ORDER BY comment_date DESC LIMIT 0 ,4";	$comments = $wpdb->get_results($query);	 	if ($comments) {	echo '<ul>';	foreach ($comments as $comment) {	$url = '<a href="'. get_permalink($comment->comment_post_ID).'#comment-'.$comment->comment_ID .'" title="'.$comment->comment_author .' | '.get_the_title($comment->comment_post_ID).'">';	echo '<li>';	echo '<div class="img">';	echo $url;	echo get_avatar( $comment->comment_author_email, $img_w);	echo '</a></div>';	 	echo '<div class="txt">Par: ';	echo $url;	echo $comment->comment_author;	echo '</a></div>';	echo '</li>';	}	echo '</ul>';	}	?>

Source

4. Setting up a FeedBurner

Does setting up FeedBurner pose a problem? Go for:

add_action( 'template_redirect' , 'your_rss_redirect');function your_rss_redirect() { if ( is_feed() AND !preg_match( '/feedburner|feedvalidator/i', $_SERVER['HTTP_USER_AGENT'] ) ){header( 'Location: http://feeds.feedburner.com/my_your_feed' );header( 'HTTP/1.1 302 Temporary Redirect' ); }}

Source

5. Send Emails

If you want to send emails to the users from your WordPress website, use wp_mail() function as shown below:

$message = 'Hello, I am glad you are revisiting.';wp_mail( '[email protected]', 'Thanks for reading my blog!', $message);

Also, if you have to send HTML content with the help of a filter, use:

add_filter ("wp_mail_content_type", "your_mail_content_type");function your_mail_content_type() { return "text/html";}

Source

6. Add Widgets

If you use shortcodes in text widgets, the product will be more flexible and hence easy to use by the user. It is a great tool and all you have to do is:

add_filter( 'widget_text', 'do_shortcode' );

Source

7. Displaying Fuzzy Dates

With the help of a plugin called WP-Relative Date, you can display relative date on your website or blog. Just download the plugin and activate it and then change to:

<?php relative_post_the_date(); ?>

From:

<?php the_date(); ?>

in your single.php, index.php, and page.php.

Source

8. Display external Feed on your site

For extra traffic as well as promotion of your blog, you can display the blog’s feed on your website. All one has to do is to paste the following code anywhere in the theme.

<?php include_once(ABSPATH.WPINC.'/feed.php');	$rss = fetch_feed('http://feeds.feedburner.com/wpbeginner');	$maxitems = $rss->get_item_quantity(5);	$rss_items = $rss->get_items(0, $maxitems);	?>	<ul>	<?php if ($maxitems == 0) echo '<li>No items.</li>';	else	// Loop through each feed item and display each item as a hyperlink.	foreach ( $rss_items as $item ) : ?>	<li>	<a href='<?php echo $item->get_permalink(); ?>'	title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>	<?php echo $item->get_title(); ?></a>	</li>	<?php endforeach; ?>	</ul>

Source

9. Random Posts

How about including a link in your navigation, which takes you to a page that shows a random page? And you visit a new post every time you refresh the page. It’s pretty easy, just create your custom page and template and paste the following code:

<?php	query_posts(array('orderby' => 'rand', 'showposts' => 1));	if (have_posts()) :	while (have_posts()) : the_post(); ?>	 	<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>	 	<?php the_content(); ?>	 	<?php endwhile;	endif; ?>

Source

10. Author’s Comment Shown Differently

To show author’s comments separately from other comments, follow these directions.

Open your style.css in your template folder and add

authorstyle { background-color: #B3FFCC !important; }

Second, open comments.php from your themes folder and replace

<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>"></li>

With

<li class="<?php if ($comment->user_id == 1) $oddcomment = "authorstyle"; echo $oddcomment; ?>"></li>

Source

Make sure you change user id 1 to the user id of the author. After this, you will notice that author’s comments will be highlighted with a separate background as compared to the rest of the comments.

11. Get Rid Of Your Trash

WordPress has a great trash system. When content is deleted, it goes to the trash, from where it can be deleted for forever or can be restored. In a way it is a very good system as it allows recovering the content if it is deleted accidently. But, trash takes up a lot of space in the database. WordPress deletes trash automatically after thirty days. For that long, it becomes difficult to retrieve the information as the database gets bigger. You can easily shorten this time period to, say, 10 days by adding the following:

define ('EMPTY_TRASH_DAYS', 10);

You can even set the number of days to zero, if you want to disable the trash system by adding:

define ('EMPTY_TRASH_DAYS', 0);

Source

12. Lessen the Revisions for Post

WordPress allows for unlimited number of revisions, even though it is not really needed. We can always lower this limit to 3 or 4 according to our requirement. It can be done by adding the following code to wp-config.php file.

define( 'WP_POST_REVISIONS', 3 );

Or, if you want to disable the post revision system fully, just add the following code:

define( 'WP_POST_REVISIONS', false );

One important point to remember here is that blog posts need to be revised for a number of times, so one should be very careful if opting to completely disable the revision system.

Source

13. Setting up Gzip Compression

A website page is transferred to a browser at up to 70% of its original size with Gzip compression. Then the page is decompressed by the browser and is displayed to the user. To enable Gzip, you have to visit the WordPress options page at www.yourwebsite.com/wp-admin/options.php and just set the value of the Gzip field from 0 to 1.

Source

14. Browser Caching

As a website developer, we know that some of the files like Javascript files, logo of the website, and CSS files are not changed regularly or frequently. If we enable browser caching, it helps the users to reduce the loading time of your website as a cached copy of the website is stored locally. Once the set time limit expires, files are downloaded again.

To enable browser caching, add the following code to your .htaccess file:

## EXPIRES CACHING ##<IfModule mod_expires.c>ExpiresActive OnExpiresByType image/jpg "access 1 year"ExpiresByType image/jpeg "access 1 year"ExpiresByType image/gif "access 1 year"ExpiresByType image/png "access 1 year"ExpiresByType text/css "access 1 month"ExpiresByType text/html "access 1 month"ExpiresByType application/pdf "access 1 month"ExpiresByType text/x-javascript "access 1 month"ExpiresByType application/x-shockwave-flash "access 1 month"ExpiresByType image/x-icon "access 1 year"ExpiresDefault "access 1 month"</IfModule>## EXPIRES CACHING ##

Source

15. Perfect URLs

To make sure that URLs are properly formed, use esc_url() function.

$my_url = 'http://example.com/?page=true';$url = esc_url( $my_url );

Source

15 Cool WordPress Coding Tips You Must Know

Guest Author: Tanuj Rastogi

Tanuj Rastogi, director of Sigma Infotech and Web Circle , has helped many businesses with their WordPress Development needs. He is well-versed in customisation, knows how to best use the available plug-ins, and excels at optimising the developed sites for optimisation purposes.

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。