ThemeLab » WordPress Tutorialshttp://www.themelab.com
We build High Quality, Good Looking Premium WordPress Themes that are Easy to Use and ready for just about anything.Fri, 27 Feb 2015 16:18:33 +0000en-UShourly1http://wordpress.org/?v=4.1.1How to Automatically Embed Tweets in WordPress Posts or Pageshttp://www.themelab.com/automatically-embed-tweets-wordpress-posts-pages/
http://www.themelab.com/automatically-embed-tweets-wordpress-posts-pages/#commentsFri, 27 Jun 2014 18:12:43 +0000http://www.themelab.com/?p=5882Twitter is the web’s most happening space. This is why most bloggers set up WordPress to automatically tweet a new post. It has also become a common practice to quote an individuals Tweet in the media. You must have seen articles in your favorite online publications with actual Tweets embedded right inside the content. In

]]>Twitter is the web’s most happening space. This is why most bloggers set up WordPress to automatically tweet a new post. It has also become a common practice to quote an individuals Tweet in the media. You must have seen articles in your favorite online publications with actual Tweets embedded right inside the content. In this article, I will show you how to automatically embed Tweets in WordPress posts or pages.

First thing you need to do is to find out the permanant link of the Tweet you want to embed. This permanent link can be found on the timestamp of every Tweet in your Twitter stream. This timestamp is displayed next to the user’s Twitter handle.

Now you need to right click and select ‘Copy link location’ to copy the permanent link for an individual Tweet.

Open the WordPress post or page where you want to embed the Tweet and simply paste the URL by pressing CTRL+V.

That’s all, you can now save your post and then click on the preview button in the post editor. You will see the Tweet automatically embedded into your WordPress post.

I hope this article helped you embed Tweets in your WordPress posts and pages.

]]>http://www.themelab.com/automatically-embed-tweets-wordpress-posts-pages/feed/2How to Set up Pretty Permalinks in WordPresshttp://www.themelab.com/how-to-set-up-pretty-permalinks-in-wordpress/
http://www.themelab.com/how-to-set-up-pretty-permalinks-in-wordpress/#commentsWed, 25 Jun 2014 17:00:14 +0000http://www.themelab.com/2008/03/11/how-to-set-up-pretty-permalinks-in-wordpress/By default, WordPress uses URLs like this http://example.com/?p=123. This URL structure is not SEO friendly. This is why most WordPress site owners use Pretty Permalinks, which is a term used for SEO friendly URLs in WordPress. These SEO friendly Permalinks contain post, category, tag, title in the URL, allowing you to use Keywords inside your

]]>By default, WordPress uses URLs like this http://example.com/?p=123. This URL structure is not SEO friendly. This is why most WordPress site owners use Pretty Permalinks, which is a term used for SEO friendly URLs in WordPress. These SEO friendly Permalinks contain post, category, tag, title in the URL, allowing you to use Keywords inside your URLs. In this article, I am going to show you how to set up Permalinks in WordPress and make your website more SEO friendly.

To set up Permalinks on your WordPress site, simply go to Settings » Permalinks. There you will see a number of options to choose from:

Choosing a Permalink Structure

Most WordPress SEO experts agree that the best permalink structure is the one that uses the post name in the URL. The default options allow you to choose from day and name, month and name, and post name URL structure. There is also an option to have numeric permalinks. Numeric permalinks are better than the ugly URLs but they are not considered as good as the URLs with post name.

You need to choose one of the default options by clicking the radio button next to it and save your settings.

Apart from the default options, you can also create your own Permalink structure using special tags. The available tags that you can use are:

%year% – Four digit year of post, e.g. 2014.

%monthnum% – Two digit month of post, e.g 07.

%day% – Two digit day of the mont, e.g. 18.

%hour%

%minute%

%second%

%post_id% – The unique ID number of the post example, 123.

%postname% – The title of the post sanitized to be used in post URL, e.g. how-to-set-up-permalinks-in-wordpress

%category% – Category name sanitized to be used in URL. e.g. wordpress-tutorials.

%author% – Author name sanitized to be used in URL. e.g. syed-balkhi.

WordPress allows you to use whatever structure you want. However, you are required to use %postname% and %post_id% for your individual post. These tags are unique identifier that help WordPress locate the post from the database. Same goes for your category archives. They must have %category% in them.

We recommend reviewing which permalink structure you prefer before you start publishing content. Note that if you change your permalink structure at a later date, your post and page URL’s will also change. Therefore if you change your permalink structure in the future, you will need to set up 301 redirect in your .htaccess file in order to redirect users to proper content.

Troubleshooting User-Friendly Permalink Structure

In rare cases, your server may not have this module enabled. In that case you will see a notice in your Permalink settings page informing you that WordPress cannot write to the .htaccess file.

WordPress Permalinks need mod_rewrite module installed on your web server. WordPress also uses a special server configuration file called .htaccess to use pretty permalinks. All our recommended WordPress hosting providers have set up their servers with necessary configuration to run WordPress. This means your server should already be configure to use Apache’s mod_rewrite module.

The two most common causes for permalinks not working are:

1. Incorrect File Permissions

In correct file permissions stop WordPress from creating or writing to the .htaccess file. To solve this issue you would need to connect to your website using an FTP client. Make sure your FTP client is set to show hidden files. You will see a .htaccess file in your website’s root directory. If you do not see it, then you need to create one. After that simply copy and paste the rewrite code shown in Permalinks settings into your .htaccess file and save it.

2. mod_rewrite Module is Not Available on Your Server

For permalinks to work, you need mod_rewrite module enabled on your webserver. If they are not then you need to contact your hosting service provider and ask them to enable it for you.

I hope this article helped you set up Permalinks in WordPress. It will certainly provide the SEO boost your site deserves.

]]>http://www.themelab.com/how-to-set-up-pretty-permalinks-in-wordpress/feed/5How to Install Google Analytics on Your WordPress Sitehttp://www.themelab.com/install-google-analytics-wordpress-site/
http://www.themelab.com/install-google-analytics-wordpress-site/#commentsMon, 09 Jun 2014 17:00:52 +0000http://www.themelab.com/?p=5479Understanding your website visitors and how they use your website is crucial for your business. There are plenty of tools that allow you to do that. However, the most important one is probably Google Analytics. It allows you to see where your visitors are coming from, what they do on your site, which content is

]]>Understanding your website visitors and how they use your website is crucial for your business. There are plenty of tools that allow you to do that. However, the most important one is probably Google Analytics. It allows you to see where your visitors are coming from, what they do on your site, which content is popular, and so on. In this article, I will show you how to install Google Analytics on your WordPress site.

Signing Up for a Google Analytics Account

First thing you need to do before installing Google Analytics is to sign up for the service. Google Analytics is available for free, all you need to do is to visit Google Analytics and sign up with your Google account.

In the upper right hand corner there will either be two links to sign in or create a new account for gmail, or the button shown in the picture above to access Google Analytics. If you do not have a gmail account you will need to create one to use analytics. When you have signed in Google will take you to a page asking you to sign up for the service and you can either track a mobile app or a website so ensure you choose the correct tracking.

Should you choose, you can edit if you want your site to share data with different aspects of Google for services like technical support and account sprcialists. With the information on this page filled out I will be given a Google Analytics tracking code. This code is important as it will need to be added to my site in one of a few ways.

Installing Analytics

There are a few ways to add this code depending on how you want it to be added and how knowledgeable you are about WordPress.

Installing Google Analytics Code Using a WordPress Plugin

The easiest way to install Google Analytics tracking code on your WordPress site is by using a plugin. To do that you would need to install and activate Insert Header and Footer plugin. Upon activation, go to Settings » Insert Headers and Footers and place the code into the plugin’s settings.

Manually Adding Google Analytics Code in header.php

A slightly more complex way would be to open your theme’s header.php and place the code right after the <body&gt tag.

Please note that if you add the code to your main theme, then it will be overwritten whenever you update your theme. Using a child theme is the best way to add customizations like this to your WordPress site.

Manually Adding Google Analytics Code in functions.php

If you feel confident in your knowledge of php you can open your theme’s functions.php and add the following code replacing the fourth line with your Google analytics code.

Using any of those methods you should have added Google Analytics to your site.

Please note, that you will not immediately start seeing information on your Google Analytics dashboard. It will Google Analytics 12 to 24 and sometimes even 48 hours before you can see any data in your Analytics account.

I hope this article helped you install Google Analytics on your WordPress site. Its a great way to optimize your website, plan your content, and grow your business.

]]>http://www.themelab.com/install-google-analytics-wordpress-site/feed/3How to Find Royalty Free Images for Your Bloghttp://www.themelab.com/find-royalty-free-images-blog/
http://www.themelab.com/find-royalty-free-images-blog/#commentsMon, 02 Jun 2014 17:00:12 +0000http://www.themelab.com/?p=5105For any aspiring blogger out there, gaining the attention of viewers is incredibly important. You can usually best achieve this with images. However, not all images on the internet are free to use as you wish. In this article, I will show you some of the resources I use to find royalty free images. You

]]>For any aspiring blogger out there, gaining the attention of viewers is incredibly important. You can usually best achieve this with images. However, not all images on the internet are free to use as you wish. In this article, I will show you some of the resources I use to find royalty free images. You can use these resources to find royalty free images for your blog posts.

Creative Commons search is one resource that can help search across multiple websites for images. However, the catch to using this site is that you must understand the Creative Commons licenses. Most copyright simply want attribution which can usually be handled by a link back to the source. For the small cost of giving attribution you can get access to a plethora of images from different sources for your site.

Wikimedia Commons is a little bit different than Creative Commons in that a large of the images, audio, and video are free to use. There are sill a few items on there that may have a creative commons license requiring attribution, so be sure to check the license information below each image you view.

Stock Photos for Free is where we get into images that are completely free. They have a very neat and organized library with a great collection of still images. They even take the images from HD videos, the only thing they ask is you register to their site before downloading any images.

If you want an icon or anything clip art, Open Clip Art library is where you want to go. All of the clip art in this library are considered public domain so you can do whatever you want with these images. To make them even easier to use they usually come in both PNG and SVG formats.

This last one is very useful but the one you have to be the most cautious about. Just about everyone uses Google to do searches but, one thing you may not know about is an option in advanced image search. Its normally at the very bottom called usage rights. While you can set this to search for free to use commercially, Google still tells you to look at the license for each image as they can not guarantee all images being considered public domain.

These are just a few of your options available out there to find royalty free images. I hope this article helped you spice up your site with new images.

]]>http://www.themelab.com/find-royalty-free-images-blog/feed/1The Importance of Backing Up Your WordPress Sitehttp://www.themelab.com/the-importance-of-backing-up-your-wordpress-site/
http://www.themelab.com/the-importance-of-backing-up-your-wordpress-site/#commentsFri, 30 May 2014 17:42:36 +0000http://www.themelab.com/?p=195Backing up your website regularly is vital. Every experienced website owner knows this. Unfortunately, many new WordPress users do not take backing up as seriously as they should. This is sometimes down to the assumption that everything will be ok. Others have looked into backing up their files and decided it was either too time-consuming

]]>Backing up your website regularly is vital. Every experienced website owner knows this. Unfortunately, many new WordPress users do not take backing up as seriously as they should. This is sometimes down to the assumption that everything will be ok. Others have looked into backing up their files and decided it was either too time-consuming or too costly. In reality, if something with your website goes wrong, you could lose everything.

It is not uncommon for website owners to not take backups seriously until something goes wrong. It is a hard lesson that they never forget. If you are smart, you will get into the habit of backing up your website from the beginning.

In this article, we will look into why backing up your WordPress website is so important.

Server Outages

No hosting company in the world can offer 100% uptime and reliably. Even the best WordPress hosting companies have had severe problems with their datacenters. Customers can experience irregular downtime and problems during this time for days.

There is a number of reasons why a server can go down. In 2007, for example, RackSpace’s power was completely cut off when a truck crashed into one of their electrical generators. Other outages have been caused by things such as software errors, hard drive failures or a change in server structure.

When a server goes down, the hosting company usually (but not always) can retrieve their data. However, even if your website data can be retrieved, you might have to return to a version of your website that is a few days old. Therefore, when your hosting company goes down, you could potentially lose a few days of work.

WordPress Websites are Vulnerable to Hacking

WordPress is by far the most used content management system for publishing content online. This is perhaps why it is targeted so frequently by hackers and spammers. Even if you use a security plugin on your website, there is still the risk of a malicious script injecting malware or a virus onto your website.

You may not even know that someone has infected your website. One common technique hackers use is to overwrite one of your template files with their own code. This allows them to do a number of different things such as using your website to send thousands of spam emails. As a result, your server will be blacklisted by spam monitors and none of the emails you send from your websites will be received.

It Does Not Matter How Experienced You are: Mistakes Happen

Even experienced website owners who have been working online for several years make mistakes from time to time. The wrong files can get overwritten or deleted. Or a simple MySQL command with a small error can cause lots of data to be wrongly updated.

Mistakes are not something that happens every day, but when they do happen, you will be glad of any recent backups you have.

You Cannot Rely Solely On Your Hosting Company for Backups

Many website hosting companies offer daily or weekly backups of your website. In the event of something going wrong, this allows you to get a copy of your last working version of your website. Most hosting companies can roll back your whole website to a point when your website was working correctly. Be aware that shared hosting plans cannot always offer this feature as there are hundreds of other websites hosted on the same server.

Having your websites backed up daily by your hosting company does offer some piece of mind, however it is dangerous to solely rely on your hosting company as your only backup solution. Hosting companies backup systems are always in-house. As noted previously, hosting companies can suffer outages due to electrical problems, drive failures and more. So if your backup is hosted in the same location, your backup may also be lost during an outage.

Be Smart and Back Up Often

It is important to back up your websites regularly. Backing up to your personal computer every day is not always practical, particularly if your website uses a lot of storage. A more practical solution is to use an external service to backup your files.

There are a number of WordPress backup plugins that will backup your a zipped version of your website to your server. There is no real benefit to doing this if your hosting company offers daily backups as the backup file will be stored on your main hosting package. So if your website goes offline, you will not be able to access the backup file.

I recommend using BackupBuddy to backup your WordPress site. It allows you to automatically schedule your backups and store them directly on the cloud.

Do not be fooled into thinking that because nothing bad has happened to your website in the past, nothing bad will happen in the future. Incidents usually occur when you least expect them. When they do, you will find out quickly that they are time-consuming, stressful and costly. Be smart and back up your WordPress website frequently. You will be glad you did.

]]>http://www.themelab.com/the-importance-of-backing-up-your-wordpress-site/feed/0How To Make a WordPress Privacy Policy Pagehttp://www.themelab.com/how-to-make-a-wordpress-privacy-policy/
http://www.themelab.com/how-to-make-a-wordpress-privacy-policy/#commentsTue, 27 May 2014 17:00:38 +0000http://www.themelab.com/?p=262A privacy policy is a statement that discloses the information you collect about visitors on your website. It is a legal requirement and highly recommended for any business website. Many advertising programs, like Google Adsense, ask the participating websites to include a privacy policy page on their websites. In this article, I will show you

]]>A privacy policy is a statement that discloses the information you collect about visitors on your website. It is a legal requirement and highly recommended for any business website. Many advertising programs, like Google Adsense, ask the participating websites to include a privacy policy page on their websites. In this article, I will show you how to add a privacy policy page in WordPress.

What is a Privacy Policy Page?

A privacy policy page is a statement from you, disclosing the information you collect on your website about visitors. This include information you collect through cookies, registration, comments, and subscription forms, etc. Usually, site owners collect this information by installing web analytics software such as Google Analytics, or stats counter. Users also voluntarily provide information by filling out their information in comments, registration, and subscription forms.

A privacy policy page is also an statement disclosing how you use this information. For example, website owners serving advertisements through third party websites, are usually sharing this information with their advertising partners. If you are building an email list, then you need to ensure users that their email addresses will not be sold or shared with any third-party website.

How To Create a Privacy Policy Page?

There are several websites that allow you to generate your own privacy policy. Many websites offer privacy policy templates which you can use on your site by filling it with your own site name, url, email, address, and phone number. A simple privacy policy page should contain these sections.

Information you collect on your site.

How do you collect information, e.g. site logs, cookies and web beacons, sign up and registration forms, comment forms, etc.

Third Party Advertisers like Google Adsense, may also serve cookies and web beacons through your website to serve targetted ads. Include the links to each advertising program you are participating in, and their own privacy policy pages.

You can add your privacy policy page in WordPress by simply creating a new page and naming it Privacy Policy. Draft your policy by following the examples from other websites or privacy policy templates. Once you are done, save and publish the page.

Adding Link to Your Privacy Policy Page in WordPress

You can add a link to your privacy policy by editing the footer area of your website. If you have a widget area in the footer of your theme, then you can drag and drop a text widget to add a link to your privacy policy.

Alternately, you can also create a new menu by visiting Appearance » Menus. Add links to your about, contact, terms and conditions and privacy policy pages in this menu and then save it.

Come back to Appearance » Widgets and drag and drop the Custom Menu widgets to your footer sidebar. Select the menu you just created and save your widget.

There are millions of privacy policies on the internet. It is common for websites to link to their privacy policy in the footer area of their website. Most privacy policies cover the same subjects in their statement. Therefore, by examining other privacy policies, you can create your own statement. You just need to ensure that your own privacy policy correctly references your business and policies.

Adding a Privacy Policy Page Using a Plugin

If manually creating a privacy policy page seems too complicated, then you can use a WordPress plugin to generate it for you.

This plugin allows you to put your own details into a modified version of Automattic’s terms and conditions statement and privacy policy statement. Automattic are well known for being the developers of WordPress and owners of popular services such as WordPress.com, Jetpack and VaultPress. Their terms and conditions and private policy statements are released under the Creative Commons Sharealike license, therefore everyone is allowed to use it.

After activating the plugin, you need to go to Settings » Auto TOS & PP to configure the plugin.

Simply fill out your company and website information, and then turn on the shortcode. Now simply enter the shortcode [my_privacy_policy] into your privacy policy page to display your auto generated privacy policy.

The ability to insert your privacy policy into any page or post is useful; however, for most website owners a dedicated privacy policy page is sufficient. Therefore, once you have generated a privacy policy page using the plugin, you can copy the statement onto your WordPress page and then uninstall the plugin. This makes it straightforward to apply changes to your privacy policy statement in the future too.

]]>http://www.themelab.com/how-to-make-a-wordpress-privacy-policy/feed/1How to Install a WordPress Pluginhttp://www.themelab.com/install-wordpress-plugin/
http://www.themelab.com/install-wordpress-plugin/#commentsThu, 22 May 2014 17:00:34 +0000http://www.themelab.com/?p=3701Plugins are the reason why WordPress is such a flexible content management system. They can be used to enhance WordPress or add new functionality such as an eCommerce shop, ticket support system, discussion forum and more. In simpler terms, a plugin is a series of functions that can be inserted into your website (hence the

]]>Plugins are the reason why WordPress is such a flexible content management system. They can be used to enhance WordPress or add new functionality such as an eCommerce shop, ticket support system, discussion forum and more.

In simpler terms, a plugin is a series of functions that can be inserted into your website (hence the name plugin). Once a plugin has been installed and activated on your website, this additional functionality will be available to you.

In this tutorial, I will show you three different ways you can install a WordPress plugin. It does not matter which method you use, as plugins are always installed in the same location.

Plugins can be installed directly from your WordPress admin area. Clicking on the “Add New” link in the plugins menu brings you to the “Install Plugins” section. The first tab is this section is the search tab. You can search for plugins by performing a manual search yourself or clicking on a keyword from the tag cloud.

If you are new to WordPress, you may not know what plugins to install. The featured and popular tabs linked at the top of the page are a good way of finding out about quality plugins. There is also a list of the latest plugins that have been added to the directory.

A feature that most WordPress users do not take advantage of is favoriting. If you login to WordPress.org, you will see an option to favorite a plugin underneath the download link. All plugins you favorite will be listed in your profile.

A list of all your favorite plugins is also listed in the Favorites page in your admin area. All you have to do is enter your WordPress.org username in order to list all of your bookmarked plugins. This is a great way of remembering your preferred plugins across all of your WordPress powered websites.

Within your admin area, a description of every plugin is listed next to the plugin. You can click on the details link to read a more comprehensive summary. All you have to do to install a plugin is click on the install link.

WordPress will download and install the plugin on your website. You can then activate a plugin right away, or activate it later by clicking on the Plugins menu.

How to Install a WordPress Plugin From a Zip File

Only plugins listed within the official WordPress plugin directory can be installed through the search, featured, popular, newest and favorite tabs. All plugins, whether it be a free plugin from WordPress.org or a premium plugin from a different source, can be downloaded as a zip file.

Within the “Install Plugins” area, there is an option to install a plugin by uploading a zip file. To install a plugin in this way, all you have to do is select the plugin zip file and click on the install button.

WordPress will extract the contents of the zip file and install the plugin on your website.

It is common for the zip files of premium themes and plugins to contain additional folders such as documentation and original Photoshop files. Whenever you download a plugin in zip format from outside the official WordPress directory, you should check that the plugin files are located in the top level of the zip file. If not, you need to extract all files and then zip up the appropriate plugin folder.

How to Upload and Install a WordPress Plugin Manually

When you install a plugin by zip file or by directly from the official WordPress plugin directory, WordPress extracts the plugin files and stores them in your /wp-content/plugins/ folder. Alternatively, you can upload the plugin to that location yourself.

If you install WordPress at the root of your domain, your plugins folder will be located at www.yourwebsite.com/wp-content/plugins/. Plugin files would be located under the plugin name e.g. www.yourwebsite.com/wp-content/plugins/contact-form-7/index.php.

You can upload files using an FTP client such as FileZilla. To connect to your website using FTP, you need to know your FTP connection details. This information is normally provided by your hosting company in their welcome email. Contact your host if you have not received it.

To connect to your website using FTP, you need to know your website’s hostname, FTP username and FTP password. This login information is sometimes the same as your hosting control panel login information.

Once you are connected to your website, navigate to your plugins folder (this folder lists your other installed plugins). Next, upload the plugin(s) you want to install.

Any plugins you upload via FTP will be listed in your installed plugins list. Next, activate the plugin and configure it to your liking.

I hope this article helped you learn how to install a WordPress plugin. Explore WordPress plugins to discover the true power of a WordPress website.

]]>http://www.themelab.com/install-wordpress-plugin/feed/0The Ultimate Guide to WordPress Conditional Tagshttp://www.themelab.com/ultimate-guide-wordpress-conditional-tags/
http://www.themelab.com/ultimate-guide-wordpress-conditional-tags/#commentsWed, 21 May 2014 17:00:54 +0000http://www.themelab.com/?p=3792WordPress conditional tags are a great feature of WordPress that allows you to control what content is displayed on a page. There are conditional tags for different areas of your website such as your home page, blog posts and pages. This allows you to change what is displayed around your website. For example, you could

]]>WordPress conditional tags are a great feature of WordPress that allows you to control what content is displayed on a page. There are conditional tags for different areas of your website such as your home page, blog posts and pages. This allows you to change what is displayed around your website. For example, you could change your website logo in different areas of your website.

In this tutorial, I will explain what conditional tags are available to you and show you how they can be used in themes and plugins.

How WordPress Conditional Tags Work

Conditional tags are a boolean data type that can only return true or false. The tag is_home(), for example, refers to the blog index. We can use this tag to display a message to visitors of our blog. This message will not be displayed elsewhere.

The code is straightforward. All we are doing below is checking if the page being shown is the blog index page. If it is, we display our message.

<?php
if ( is_home()) {
echo "Welcome to Our Blog!!";
}
?>

The above is a basic example of what can be achieved using conditional tags, however that is essentially all there is to it. You are simply checking the type of page that is being displayed. Depending on whether the result is true or false, another piece of code is actioned.

Before we look at more examples of how conditional tags can be used, let us first look at popular conditional tags that you are likely to see used in your WordPress themes.

is_home() – Checks if the blog post index is being displayed. This may or may not be your home page as well.

is_front_page() – Checks if your home page is being displayed. This works whether your front page settings are set up to display blog posts (i.e. blog index) or a static page.

is_single() – Checks to see whether any type of single post is being displayed (excluding attachments).

is_multi_author() – Checks if more than one author has published posts on the website. True is returned if two or more people have published posts. If only one author has published posts, or if no posts have been published at all, false is returned.

There are six time based conditional tags that you will also find useful. These tags refer to date archive pages. For example, the URL http://www.yourwebsite.com/2013/12/ is a month based archive page.

If any of the following conditional tags returns true, is_archive() would also be true.

is_new_day() – Checks if today is a new day. If the current post was published on a different day from the previous post that was published, it would return true. False will be returned if both posts were published on the same day.

You will come across conditional tags such as is_home() and is_single() frequently, however you do not need to remember all of these conditional tags. Most WordPress users refer to the WordPress codex for the appropriate conditional tag when they need to set up a conditional function.

Conditional Tags Examples

Many conditional tags allow parameters to be passed to the function. This gives you much more control over what conditions have to be met before something is actioned. is_page() is a good example of this. The tag allows you to check whether the page that is being displayed is a page. is_page() will return a value of true if any page is displayed, however you need to specify the $page parameter if you want to be more specific. The $page parameter can be the page ID, page title or page slug.

Let us consider a regular website that has an about page and you want to customise the about page differently from all other pages. For example, you could display a photograph of your company at the top of the sidebar, or you could display additional information at the bottom of the about page.

To do this, you need to define the $page parameter. If the page ID was 10, you could open up your conditional statement with something like this:

if ( is_page(10) ) {

A specific page can also be specified by passing the page title to the function.

if ( is_page( 'About Us' ) ) {

The page slug can also be used. As you may recall, the page slug is the unique named identifier at the end of the URL. If your about page URL was www.yourwebsite.com/about-our-company/, the page slug would be about-our-company.

if ( is_page( 'about-our-company' ) ) {

Some conditional tags, such as is_page(), can also pass parameters in an array. The following conditional statement will return a value of true if either of the conditions are true.

if ( is_page( array( 10, 'About Us', 'about-our-company' ) ) ) {

It is common for developers to set more than one condition when using conditional tags. Let us go back to the simple task of displaying a welcome message to blog visitors. This is something that a corporate website might want to add to their blog area but not to other areas of their website (e.g. home page, contact page, about page etc).

They can do this by using the is_home() and is_single() conditional tags; which represent the blog index and single posts respectively. To display a message in both areas, you need to use the logical OR operator ||. This is illustrated in the code below. The initial if statement checks whether the page is the blog index or a single post. If either is true, the message is displayed.

Another logical operator that is very useful is the AND operator &&. This is used when you want two or more conditions to be true before something is actioned. The following if statement checks if a page is both an archive page and categorised under the news category. In the news category pages, the welcome message will be displayed. Nothing will be displayed in other categories.

AND and OR operators can be combined. The example below is taken from the functions.php template of the default WordPress theme Twenty Thirteen. The function is used to display the page title in the browser, however only part of the function is shown below.

The if statement returns a value of true if there is a site description and the user is viewing the blog index or the home page. The site description can be entered via the tagline field in your general settings area. If you complete this field, on your blog index and home page the title bar will display “Site Title | Site Description” (note: the separator is displayed using the string $sep in the code below). If you do not, the title bar will display “Site Title”.

As you can see, is_home and _is_front_page are viewed as a single entity due to the OR operator. This is why they are wrapped inside brackets.

Another PHP logical operator you can use is the not operator !. This is more practical to use in many circumstances. For example, let us say you want to display a photograph on all of your pages except your archives. There is no need to set up a long conditional statement that asks “Is this the home page, is this a single post, is this a page…”. It is more practical to simply ask “Is this not an archive page?”.

To do this, simply add an exclamation mark before the conditional tag. The code below shows how straightforward this is in practice. It will display an image on every page of your website except archive pages.

<?php
if ( !is_archive() ) { ?>
<img src="photo.jpg" />
<?php
}
?>

Up until now, we have looked at basic examples of conditional tags being used where something is either actioned or it is not actioned (i.e. if A is true, do B). In practice, there is usually another action to do if a condition is not met. Additionally, there may be several conditions that can be met, with a different response to each one.

Rather than write lots of individual statements for this, it is practical to use else and elseif statements. This allows you a greater degree of control over what is displayed on your website.

We can show this using an example. Let us say that you want to show a different logo on your website in different areas of your website. How would you do this? The answer is simple: We use else and elseif statements. The code below shows how this can be achieved.

Depending on what area of the website a visitor is viewing, one of five logos would be displayed using the above code. It is a basic example that illustrates how easily else and elseif statements can be used to control many different areas of your website.

Elseif statements are also used in other parts of WordPress. Most functions.php templates use them and many WordPress themes use them to change how their website title is displayed in browsers.

More Conditional Tags

There are a number of additional conditional tags available. Many of these are used by developers in themes and plugins.

Below is a list of some of the other conditional tags that are available to you.

is_paged() – Checks whether the page you are currently viewing is a paginated page other than page one. Posts and pages are paginated when you use the nextpage quicktag in your content to split up large posts.

is_admin() – Checks whether the user is logged into the administrator area. It is not used to check whether a user has administrator privileges, only whether they are logged into the WordPress dashboard.

is_page_template() – Checks whether the page being viewed is using a page template. A specific page template can be defined, if necessary.

is_preview() – Checks whether a blog post is being viewed in draft mode.

has_excerpt() – Checks whether the current post has an excerpt. Specific posts can be defined.

has_nav_menu() – Checks whether a menu location has a menu assigned. This is used by theme developers to show something in the event that the user has not added a menu.

in_the_loop() – Checks whether the caller is still within the WordPress loop.

Conditional tags are an important WordPress concept. Due to how useful they are, there are few WordPress themes that are designed without them. Once you understand else statements, elseif statements and logical operators such as AND, OR and Not; you will be able to tackle any conditional function.

I hope this guide helped you learn how to use WordPress conditional tags in your themes.

]]>http://www.themelab.com/ultimate-guide-wordpress-conditional-tags/feed/3How to Switch Off a Website With One Line of Codehttp://www.themelab.com/switch-website-one-line-code/
http://www.themelab.com/switch-website-one-line-code/#commentsTue, 20 May 2014 17:00:36 +0000http://www.themelab.com/?p=4039Sometimes you may need to switch off a WordPress site without affecting data. If you are an aspiring developer, then sometimes you may want to remotely make a site disappear without actually destroying anything. This is where CSS comes in handy. In this article, I will show you how to switch off a website with

]]>Sometimes you may need to switch off a WordPress site without affecting data. If you are an aspiring developer, then sometimes you may want to remotely make a site disappear without actually destroying anything. This is where CSS comes in handy. In this article, I will show you how to switch off a website with one line of code.

There are two ways to do this, first method remotely loads a tiny CSS file into your site, which you can turn on from a remote URL. This method is particularly helpful for theme developers who are looking for a way to deal with non-paying clients.

The second method is to add the CSS yourself. This method is useful, when you just want to hide the style and appearance of a website.

Remotely Switching Off a Website

First thing you need to do is to visit CSS Kill Switch and create an account for your website by providing a site ID and password.

Once you have added your site, CSS kill switch will show you two lines of code. You need to use either one of them, I would recommend using the @import line. Simply copy and paste it in your WordPress theme’s stylesheet.

Now to turn off your website, all you need to do is revisit CSS Kill Switch and sign in with your site’s ID and Password. Once you are logged in, simply check the Engage box to turn off the website.

Manually Switching Off a Website

What CSS Kill Switch does, can be done manually as well. Simply add this single line of code in your theme’s stylesheet:

body * { display: none !important; }

CSS Kill Switch also adds black as background color. If you are doing this manually and want to deceive someone into believing that there is something wrong with the website, then leaving it white will make people think they are facing the dreadful WordPress white screen of death error. It will take them sometime to figure out that there is nothing wrong with WordPress on their website.

Please note that by switching off the appearance of a website, you are not actually shutting down a website. The contents of the site will still be visible to search engines and text only browsers. Any user can still see the content by viewing the source code of the page. The CSS code is also easily detectable by clicking on Inspect Element any where on a web page.

I hope this article helped you learn how to switch off a website with just one line of code.

]]>http://www.themelab.com/switch-website-one-line-code/feed/4How to Highlight Author Comments in WordPresshttp://www.themelab.com/highlight-author-comments-wordpress/
http://www.themelab.com/highlight-author-comments-wordpress/#commentsMon, 19 May 2014 17:00:24 +0000http://www.themelab.com/?p=4042It becomes difficult for users to notice author’s comments on posts with many comments. This sometimes results into users asking same question in comments, even though the author has already answered in an earlier comment. This is why most popular blogs highlight author comments in WordPress. It is really simple, and in this article I

]]>It becomes difficult for users to notice author’s comments on posts with many comments. This sometimes results into users asking same question in comments, even though the author has already answered in an earlier comment. This is why most popular blogs highlight author comments in WordPress. It is really simple, and in this article I am going to show you how to highlight author comments in WordPress.

WordPress adds CSS classes to many elements in the HTML output it generates. These default CSS classes can be used or by themes or plugins. WordPress also adds a CSS class to comments added by a post’s author. This CSS class is .bypostauthor and you can see it by right clicking on your own comments for posts you have written.

Now all you have to do is to use this class to highlight author comments differently. Lets change the background color of author comments in WordPress. Add this CSS to your theme or child theme’s stylesheet.

.bypostauthor {
background: #eee;
}

Here is another example, this one just adds a border to highlight author’s comment:

The easiest and quickest way to learn how to modify themes is by studying other themes. Lets see how the default Twenty Twelve theme adds ‘Post Author’ label to highlight author comments in WordPress.

Twenty Twelve uses a function called twentytwelve_comment. Defined in the theme’s functions.php file, this function acts as the callback function for wp_list_comments() function. Adding a callback function to wp_list_comments allows themes to control the display of comments.

In Twenty Twelve’s functions file you find these lines which add the Post Author label to comments added by the post’s author: