Archive | Source Code

19 December 2011 ~ 4 Comments

Google Translate API v2 using jQuery and Ajax

Since before the 1st December 2011, Google Translate API use to be free. It’s a great tool for many web developers out there. Now since version 2 of the API has been released you have to pay to be able to use it and some data has changed in able to fetch the translation results.

I used Google Translate using jQuery before on a small code project to automatically translate documents upon load but the method has now changed slightly and I spent the past 3 days figuring how to make it work for version 2, again using jQuery.

I have succeeded in doing so and would like to share it with you for the cost of a small fee of $4,99. Documentation is also provided on how to use it.

Just click on the PayPal buy now button and once payment is successful, you will get a zip file called gtapiv2jquery.zip from sebastien[at]sebs-studio.com within 24 hours. If any queries please contact me using the contact page.


Continue Reading

Tags: ,

30 June 2011 ~ Comments Off

WordPress Multi-Site Support for WooThemes

I was setting up a new multi-site in the early hours of the morning and I noticed that when a WooThemes theme is active on another blog, the link to ‘Update Framework’ was still showing under the theme options. I thought too my self that this was wrong and it would allow any one to update the framework for that theme without permission of the proper administrator of the main site.

So I did a little research on how to detect the main site and the first blog. If both equal as One then the link to update the framework would show. Otherwise any other site created using the theme it would not. This also applies to the admin bar.

Here is what I changed and is fairly easy to do your self if setting up a multi-site with WooThemes.
You will need to make a backup of two files in the WooThemes theme you are sharing.
‘admin-interface.php’ and ‘admin-functions.php’

Please not that if you update the framework after this modification it could be very well removed. If you are sharing just the one WooTheme like I am on a multi-site, then this will be easy and done in no time at all. If you are sharing more than one or all of the themes… then it will take you sometime unless all themes have the exact same framework version from the start before modifying them.
[...]

Continue Reading

01 June 2011 ~ 2 Comments

Creating a Shortcode

I finally learned how to create a proper shortcode for WordPress and now capable of creating loads of great shortcodes for my framework Ocean Themes.

This is one of the first shortcodes that I have created and will be included in the framework and thought I share with you on how you can create your own using my own as an example.
[...]

Continue Reading

13 April 2011 ~ Comments Off

Detect Status of Skype User using PHP

If you wish to display your Skype status on your website, use this code to start of with.

<?php
function check_skype_status($skypeusername){
        $url = 'http://mystatus.skype.com/smallicon/' . $skypeusername;
        if(base64_encode(file_get_contents($url)) == "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAA3NCSVQICAjb4U/gAAAANlBMVEX////////29/fy8/Pu7+/l5+fh4+Pd39/a3d3W2NjMzMzDx8fAxMS6vr62u7uxtbWvtLSssLAwgBwiAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAK8AAACvABQqw0mAAAABV0RVh0Q3JlYXRpb24gVGltZQA4LzE4LzA1oj6Z/QAAACV0RVh0U29mdHdhcmUATWFjcm9tZWRpYSBGaXJld29ya3MgTVggMjAwNId2rM8AAAB6SURBVBhXXc9BDsQgCEBRUForjij3v2xBSdsMu/9CIgKA2sBntBwHf9D7LLVWDlLvy5q5LVHrM3ln7i5ay5UwMSekANs3SZhljC2VW0brOecC7lP+QAhzRnJZ73TrYUixMqVRF+nURGSf8nsnrlWO2ccuMvT/RD8IcAMCGwfk9MNc3gAAAABJRU5ErkJggg=="){
                $status = 'Offline';
        }
        else{
                $status = 'Online';
        }
        return $status;
}
echo check_skype_status("yourusername");
?>

Continue Reading

Tags:

03 April 2011 ~ Comments Off

Backing up your MySQL Database using PHP

Backing up your database is the far most important thing that you need to do. With out it your site does not work and loosing all of that data is a night mare. Most of the time you forget about backing up your database because you don’t regularly login to your web host account to do so unless there is a problem with your site or you need to tweak something on the server.

Show »

Well I have created a cron-job script that you can setup on your web host that will back up your database for you.

[...]

Continue Reading

23 March 2011 ~ Comments Off

Read and Fetch Hidden File Data

Ever wanted to fetch data from a file that is hidden if you were to access the file directly, well here is a piece of code that can do just that. You can display the information any where you want once the data has been fetched.

First of all you will need to copy two functions in to your website that will be called.

The first one opens the file you have asked the function to read in read only mode. This function reads the very top of the file for the headers you have in place. These headers must match the default headers you have set in the second function.

// This gets file data when called and reads the file in read only.
function get_file_data($file, $default_headers){
        // Just open for reading.
        $fp = fopen($file, 'r');

        // Pull only the first 8kiB of the file in.
        $get_data = fread($fp, 8192);

        // PHP will close file handle, but we are good citizens.
        fclose($fp);

        foreach($default_headers as $field => $regex){
                preg_match('/'.preg_quote($regex, '/').':(.*)$/mi', $get_data, ${$field});
                if(!empty(${$field})){
                        ${$field} = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', ${$field}[1]));
                }
                else{
                        ${$field} = '';
                }
        }

        $get_data = compact(array_keys($default_headers));

        return $get_data;
}

The second will need adjusting to what information you would like to fetch from that file. The headers can be what ever you want as long as you make sure the header is the same as the default headers in the function.

// This fetches the file data and returns back the information received.
function fetch_file_data($file){
        $default_headers = array(
                'Name' => 'Name',
                'Email' => 'Email',
                'URI' => 'URI'
        );

        // This calls the other function to read the headers from the file specified.
        $file_data = get_file_data($file, $default_headers);

        $file_data['Name'] = $file_data['Name'];
        $file_data['Email'] = $file_data['Email'];
        $file_data['URI'] = $file_data['URI'];

        return $file_data;
}

How do I use these functions?
You simply use the ‘fetch_file_data’ function and echo in place were you want to display the data from that file like so.

<?php
$file = 'Place the destination of your file here!';
$data = fetch_file_data($file);
echo $data['Name'];
echo $data['Email'];
echo $data['URL'];
?>

For the email and web url you can display them as actual links.

echo '<a href="mailto:'.$data['Email'].'">'.$data['Email'].'</a>';
echo '<a href="'.$data['URL'].'">'.$data['URL'].'</a>';

How do I write my hidden data at the top of the file?

<?php
/*
Name: John Smith
Email: [email protected]
URI: http://www.example.com
*/
?>

Continue Reading

Tags: ,

21 February 2011 ~ Comments Off

Translating Strings

Translating your website can be the most time consuming thing to do if you are supporting other languages for a more global audience and to do that, you require a system that you can easily keep building and translate your strings later without having to go back and replace all the strings that requires a function to load the language the visitor wishes to read your website in.

WordPress is one example of a system that allows developers to provide either there themes or plug-ins in several languages by simply translating a file that stores all the strings side by side (original / translation) of each other. If a string is not translated it will load the original string.

I have created a similar function that you can use for your site if it is coded in PHP.

Simply copy and paste this function in the file you wish to store it.

function text($pages,$string){
        global $page;

        if($page[$pages][$string] == ''){
                $loadString = $string;
        }
        else{
                $loadString = $page[$pages][$string];
        }
        echo $loadString;
}

To use it, simply write:

<?php text('pagename', 'Your text string goes here.'); ?>

Now you are wondering what do I change ‘pagename’ too. That’s easy, if the string is on the homepage only then you would put ‘homepage’ in place of ‘pagename’. If the string is something that is shared or loaded on every page like your menu navigation then you would put ‘mainnavigation’ in place instead.

How do I translate my strings?

First you need a new file created and saved as the default strings you are going to produce translations of i.e. ‘default.php‘ .

Now you write your strings like so:

<?php
$page['homepage']['Welcome to my website.'] = 'Welcome to my website.';
?>

Copy the default strings file and save it as another language i.e. ‘japanese.php‘ . Now you can translate the strings in the new language like so:

<?php
$page['homepage']['Welcome to my website.'] = '私のウェブサイトへようこそ.';
?>

And that’s it. How you decide to load your language files on your site is up to you.

Good luck.

If you liked this post, why don’t you share it with others using Twitter, Facebook, Digg or Stumbleupon on the right hand side or subscribe below to receive new posts in your email.

Continue Reading

Tags: ,

04 December 2010 ~ 4 Comments

Write a WordPress Widget

We are now currently been given one of the best Open Source CMS’s out on the web and we are now on (should be all on) WordPress v3.

Writing a widget for WordPress has changed many times over. How to write the functionality of the widget, what code is no longer accepted and what needs to be changed and because of that you are constantly having to update your widgets for the next new major version release of WordPress. We hope that WordPress won’t be changing to much in the future but just evolve.

WordPress has opened many doors for many ways of using it and one of the great features of WordPress is the widget section. They can be created simply as a Plugin for any user to add to any theme or you can attach it to your theme functions.php file to be built in specifically for that theme.

I have not been able to find any tutorial easily for writing widgets to the full extent of there ability and when I did, I thought it would be best to explain again my self how to write a widget in this tutorial.
[...]

Continue Reading

30 November 2010 ~ 1 Comment

XML Parsing using PHP

I had wanted to write this tutorial sometime ago but never got time to finish it. Now I have. This is a tutorial I hope will help others who have had similar problems parsing XML data using php. [...]

Continue Reading

Tags: ,

24 November 2010 ~ 2 Comments

Tim Thumb Positioning Mods

Many of you want to display your images just right on your site and the majority of you use Tim Thumb script for quick easy cropping and quality control. One of the flaws in Tim Thumb script is that you can’t position the image exactly were you want to crop, you only get a compass cropping system.

  • North = t, tl, lr, tr, rt
  • South = b, bl, lb, br, rb
  • West = l, tl, lt, bl, lb
  • East = r, tr, rt, br, rb

The script basically calculates the height and width of the image and then by the size of the image you want it to be. I find my self in needing something a bit more specific that will allow me to crop a large image to a specific area.

So i decided to add my own combination’s by adding a divider. I’m still testing this out my self and so far it is working so if you wish to use it for yourself you are quite welcome to add the following code to the Tim Thumb script.

Simple open the timthumb.php and locate positional cropping. On the last break; before default: add the following and then save.

case 'ml2':
$src_x = $width/2; // Divides the width by 2
break;
case 'ml3':
$src_x = $width/3; // Divides the width by 3
break;
case 'ml4':
$src_x = $width/4; // Divides the width by 4
break;
case 'ml5':
$src_x = $width/5; // Divides the width by 5
break;
case 'ml6':
$src_x = $width/6; // Divides the width by 6
break;
case 'ml7':
$src_x = $width/7; // Divides the width by 7
break;

This allows you to position the image exactly were you want it in the middle from the left.

To add this mod to your image simply add a=ml0 in your html image string. Change the zero to the number you want to use.

I will add other combination’s in the future.

Enjoy.

Continue Reading

Tags: , ,

23 July 2010 ~ Comments Off

Theme Update Counter like Plugins

Recently WordPress had updated there new default Theme 2010 to version 1.1 and as most WordPress users who are using that Theme you would go and update and download the new Theme version.

One thing got me thinking about how when a user gets notified on a plugin, there is a counter displaying how many plugins need updating. So why not have the Themes menu act the same way.

I did a little digging in the ‘wp-admin’ folder and searched for the ‘menu.php’ file and located where the Plugin menu was. I then copied the same function for the Themes menu and was able to get a counter for any Themes that need updating.

When I first looked at the change in my control panel, it was unable to fit fully. So now that we got the function to work we need to get the counter to fit properly with the label of the menu.

I altered the word ‘Appearance’ and changed it to ‘Themes’ and that was it. Now we can be notified just on how many Themes need updating.

Copy and Paste the code below in the appropriate location to replace in the ‘menu.php’ file under ‘wp-admin’ and save.

$update_themes = get_site_transient( 'update_themes' );
$update_count = 0;
if ( !empty($update_themes-&amp;amp;gt;response) )
$update_count = count( $update_themes-&amp;amp;gt;response );

if ( current_user_can( 'switch_themes') ) {
$menu[60] = array( sprintf( __('Themes %s'), &amp;quot;&amp;amp;lt;span class='update-plugins count-$update_count'&amp;amp;gt;&amp;amp;lt;span class='update-count'&amp;amp;gt;&amp;quot; . number_format_i18n($update_count) . &amp;quot;&amp;amp;lt;/span&amp;amp;gt;&amp;amp;lt;/span&amp;amp;gt;&amp;quot; ), 'switch_themes', 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'div' );
$submenu['themes.php'][5]  = array(__('Themes'), 'switch_themes', 'themes.php');
if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
$submenu['themes.php'][10] = array(__('Menus'), 'edit_theme_options', 'nav-menus.php');
} else {
$menu[60] = array( sprintf( __('Themes %s'), &amp;quot;&amp;amp;lt;span class='update-plugins count-$update_count'&amp;amp;gt;&amp;amp;lt;span class='update-count'&amp;amp;gt;&amp;quot; . number_format_i18n($update_count) . &amp;quot;&amp;amp;lt;/span&amp;amp;gt;&amp;amp;lt;/span&amp;amp;gt;&amp;quot; ), 'edit_theme_options', 'themes.php', '', 'menu-top menu-icon-appearance', 'menu-appearance', 'div' );
$submenu['themes.php'][5]  = array(__('Themes'), 'edit_theme_options', 'themes.php');
if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) )
$submenu['themes.php'][10] = array(__('Menus'), 'edit_theme_options', 'nav-menus.php' );
}

Upload the altered file to your server in the ‘wp-admin’ folder and your done. Enjoy!

If you have any comments about this please leave them below. Any one is welcome to try and convert it to a plugin or ask Matt Mullenweg to make this change in an upcoming WordPress update.

Continue Reading

Tags:
One of 338 websites proudly supporting Earth Hour.    Use WordPress? Get the plugin.
Performance Optimization WordPress Plugins by W3 EDGE