Multi Environment WordPress wp-config.php

When you are developing any website you will always have different environments for your website, the number of environments you need will depend on the size of the project and how many people are involved in the project.

The main environments you will find in any website project are:

  • Development Server - The developers local machine to development the website.
  • Testing Server - Once development is finished this will be where all testing takes place.
  • User Acceptance Testing Server - Where the client will review the changes to the website.
  • Production Server - The live website.

The two environments you will always need to have is a development environment and a production environment. You will normally only have these two environments if the client doesn't need to view the site before it goes live. This means that once the developer has finished working on the site they can simply put this onto the production server. These are normally for the very basic of projects where the developer can simply test on their local development server before going live.
Continue reading »

Absolute Center Images With CSS

Here is a technique about how you can absolute center position an element on the horizontal and vertical in CSS.

Center Images Horizontally

To center something on the horizontal in CSS it's quite easy all you need to do is set the width on the element and apply an auto margin-left and margin-right on to the image. The browser will work out the exact margin on both the right and left side of the image. This will position the image in the center of the parent element just by using the width and the margin properties.

<img src="example.html" alt="Center Images" />

img {
     width:250px;
     margin: 0 auto;
}

Continue reading »

Disable Scroll Wheel Zoom On Google Maps

Google Map

When you are using the Google map API on your website it will override the the scroll mouse event and act as the zoom on the maps. This feature makes it really easy to zoom on a certain position on the map, you just hover over the location and use your mouse wheel to zoom in or out.

But this causes a problem when you have a large or full screen map, as you scroll on the page the map will zoom out, making it difficult to scroll on the page using the mouse wheel.
Continue reading »

Set Minimal Comment Limit In WordPress

The reason for comments being turned on a blog is to add more in-depth discussion to a certain post. But some people comment just to get a link back to their own site, these comments will consist of things like "nice post", "thanks", that add nothing to the discussion of the post.

Most of the time blog owners will use moderation to delete these types of comments from appearing on the site, but if you have a popular site this can take up a lot of time. If you are getting annoyed with having to delete all comments like this then we can place a minimal character count on your comments.

For this you can use the filter preprocess_comment to check the comment before it is saved to the database. Within this filter you can check the character count of the comment by using the strlen() function.
Continue reading »

Force SSL On Certain WordPress Pages

There are going to be certain pages that you want to force SSL, so that the browser will redirect you to this page using a HTTPS connection so the data that is posted from this page is encrypted and secure.

This might be for any custom login pages you create in your application, or a credit card payment page. Normally you will have to write a script to force this page to be in SSL, but if you are making your application with WordPress you can use WordPress to redirect the browser to HTTPS by using the force_ssl filter.

This filter will run on every page and is used to decide if WordPress should redirect to HTTPS. Returning true from this function will automatically redirect the user to HTTPS and allow you to collect secure data from this page.
Continue reading »

Check A Credit Card Number Passes Luhn Check

When you accept credit card numbers on your website you need to do a number of different validation checks on the credit card numbers the user enters. When the form is submitted you will need to check that the user has filled out the correct credit card number, expiry date and security code number. Before you send this to the bank or payment gateway for them to validate and process you can do a couple of basic checks to make sure your not sending them poor data.

For example you can check if the expiry date is not empty and is in the correct format of mm/yy, you can check that the security code number is 3 digits and check that the credit card number is 16 digits long.
Continue reading »

Check If A Sidebar Has Widgets Assigned

To display a sidebar in your WordPress site you first need to register the sidebar so that WordPress can assign widgets to it. When a sidebar is registered it will appear in the widget dashboard so that you can assign widgets to each sidebar.

When you have widgets assigned to your sidebar you need to add some code to your theme so it can be displayed on your site. To display a sidebar in your WordPress theme you need to use the function dynamic_sidebar(), this will allow you to pass in a sidebar id to display the contents of the sidebar.

There are times when you will want to check if the sidebar has widgets assigned to it before you include it on the page, this is commonly for styling them correctly. For example if you are creating a grid layout with your sidebars inside then you need to know which sidebars have widgets before you and them to the page.

The following code will use the WordPress function is_active_sidebar() to decide which sidebars have active widgets so we know what layout to use to display the two sidebars. The is_active_sidebar() function takes one parameter which is the id you give to the sidebar when it is registered, and it will return a boolean value, TRUE if the sidebar has widgets assigned and FALSE if the sidebar has no widgets assigned to it.
Continue reading »

Formatted Strings With PHP

When you are using PHP to putput a string on the page most of the time you will use the syntax echo, which will take the following string and display that string in HTML.

You can even concaternate multiple strings or variables together to be outputted by the echo syntax.

Here is an example of using echo to output multiple options in a select box.

< ?php
        $options = array(
                        'option1' => 'title1',
                        'option2' => 'title2',
                        'option3' => 'title3',
                        'option4' => 'title4',
                        'option5' => 'title5'
                        );
        echo '
<select>';
        foreach($options as $key => $val)
        {
            echo '
<option value="'.$key.'">'.$val.'</option>
';
        }
        echo '';
?>

Continue reading »

Encode Email Addresses With PHP

Spam bots will crawl your pages exactly the same way Search Engines crawls your pages, but while Search Engines are crawling to index your content, spam bots are crawling to find certain information.

One of the most common bits of data they are searching for are email addresses, this is because they can store these email addresses in a list and send out spam emails.

Displaying your email address on your web page makes it very easy for your visitors to store this and send you email, but you will need to get around the spam bot problem to do this you will need to HTML encode your email address.

While encoding your email address will make it display correctly in the browser in the source code the email address will be encoded so that spam bots can not read the email.
Continue reading »

Add Custom Image Sizes Into Media Library

In a previous code snippet we found about how you can define new custom images sizes to use on your WordPress theme.

This will use the WordPress built in function called add_image_size().

add_image_size( 'custom-image-size1', 300, 900, true );

This function takes 4 parameters:

  • Image Size Name
  • Width in pixels
  • Height in pixels
  • Crop the image to the size.

Add this function into your functions.php file and everytime WordPress uploads a new image it will resize it to be the size you defined in this function.

The way you can use this in your theme is by passing the image size name into the_post_thumbnail() function, like this.

the_post_thumbnail( 'custom-image-size1' );

Continue reading »