Wordpress

Adding country code prefix to customers’ phone numbers in WooCommerce

Have you come across the issue where you have used an SMS plugin for WooCommerce however, the messages simply couldn’t be sent to the customers. The root cause of this issue being the missing country code prefix in your customers’ phone numbers as required by your preferred SMS gateway.

WooCommerce, by default, doesn’t specifically ask customers to enter their country code prefix while entering their phone numbers on the registration form. So how can we overcome this situation so that the customers can still be sent SMS’s without any issue?

WooCommerce has a hook called ‘woocommerce_checkout_process’. By plugging in your custom function to this hook you can modify the customer phone number to dynamically add the country code prefix. The following code does the same thing. Here +44 is the country code for UK.


// Prefix country code to customer phone number
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {

// Add +44 as prefix to buyer phone number
$number_length = strlen($_POST['billing_phone']);

if($number_length == 11) {
// Remove first char if it is 0 and add +44
if($_POST['billing_phone'][0] == 0) {
$_POST['billing_phone'] = substr($_POST['billing_phone'], 1);
$_POST['billing_phone'] = '+44'.$_POST['billing_phone'];
}
} elseif($number_length == 10) {
$_POST['billing_phone'] = '+44'.$_POST['billing_phone'];
}
}

WordPress Pagination

WordPress comes with its own default pagination with links that appear at the bottom of the blog index page. These links are however limited to only Previous and Next. While these links do help users in navigating the blog content, they do not prove very helpful especially when a visitor wishes to quickly browse through the blog content that is embedded deeper into the blog pages.

WordPress allows this to be overridden with the help of a plugin or hook. You can instead display page numbers which allow users to quickly navigate to any blog page they wish. If you wish to choose the easier way out, then here’s a very neat plugin called WP-PageNavi. All you have to do is install this plugin and then write a single of code within your blog index page. The code should look something like mentioned below.

If you are using ‘the twentyfourteen’ theme then replace the below line of code and you are done –

<?php twentyfourteen_paging_nav();   ?>

with

<?php wp_pagenavi(); ?>

What if you want to show pagination on a page where you are using custom post type? Then you will need the following piece of code instead.

<?php
$paged = get_query_var(‘paged’) ? get_query_var(‘paged’) : 1;
$loop = new WP_Query( array( ‘post_type’ => ‘News’, ‘posts_per_page’ => 3, ‘paged’ => $paged ) );

while ( $loop->have_posts() ) : $loop->the_post();

the_post_thumbnail();
the_title();

endwhile;
?>

<?php wp_pagenavi(array( ‘query’ => $loop)); ?>

See how an array needs to be passed to the wp_pagenavi function. This array should contain the query object that is returned by the WP_Query function in WordPress.

Hope this will prove very useful in customizing your default pagination in WordPress pages.

Secure Your Videos

If you have been faced by a challenge to secure videos on your website then I am sure you must have heard about the “Unlisted videos” feature from YouTube.com and Vimeo.com. However this feature doesn’t actually keep your videos secure. Once a user comes to a page having an unlisted video, he can see the source code of the generated webpage in the browser and grab the video URL. There’s no stopping him from sharing that URL with others. When you make a video unlisted, you are only telling YouTube or Vimeo to keep the video away from search results. However if someone gets access to an unlisted video he can further share that video.

I came across a WordPress specific solution that completely protects your videos from un-authorised access. This solution also saves server space considering you have very large video files. I am talking about Amazon S3. You just need to subscribe to use Amazon’s S3 services. You can upload your videos to S3 using any tool like CloudBerry Explorer or a Firefox plugin called S3Fox.

You then need this very neat WordPress plugin called “s3flowshield”. It’s available here. It’s not free though. See how you can use the plugin in this video. It allows you to specify the a key and secret available from Amazon so that the plugin can connect to your S3 storage. You can then specify the S3 URL of your video and the rest is taken care by this plugin.

When the page containing the video is loaded, you will notice that there is no way for a user to view the source code of the loaded page and grab the video URL. The plugin uses JavaScript code in encrypted form which eventually displays the video on the webpage.

You can also set the plugin so that the video is made available as a downloadable link instead of an embedded video. In this case you can specify that the video URL should not remain active for more than a certain number of seconds. That way the video link will no longer remain active after the above time limit is reached.

I hope you’ll like this solution.