Unfortunately Microsoft over the years has left us disappointed year after year with their web browser Internet Explorer. Yes, I know we all have had frustration with the little quirks that all browsers have but Internet Explorer has been the bane of the web developer. IE 8 like previous incarnations appears at first to be step up until you look closely and notice the issues with its animation rendering capability.

Anyhow enough rambling, Recently I came across a nifty little meta tag that IE 8 supports that tells it to render the page content as IE 7 would. Quickly and easily removing any quirks introduced to your website that are IE 8 specific. Now instead of 2 or 3 version of IE that you need to support you are back down to simply IE 7 or if you are one of the unfortunate ones, IE 6 as well.

<meta content="IE=EmulateIE7" http-equiv="X-UA-Compatible">

Enjoy, and here is to hoping that for IE 9 they do a full rewrite of this web browser known as Internet Explorer

, , , ,

When developing within a large web application it may be needed to check if a JavaScript function/object exists prior to being calling. The benefits to checking if the method exists are:

  1. No JavaScript errors are Generated.
  2. if needed you can dynamically load and initialize your method or object

In most cases you won’t need to test for a function but in the rare cases that you find yourself not sure of what has been loaded into the DOM (Document Object Model) you can check by simply calling window.methodName. By wrapping it in a if statement it will let you know if the method exists.

if( window.methodname ) {
  // method exists
} else {
  // method does not exist
  function methodName() {
 
  }
}
,

Many if not all plugin and theme developers of the popular WordPress software are familiar with the wp_list_pages function. However very few individuals are aware of the Walker classes of WordPress and how they can be extended to customize your page lists with unique and custom functionality. The following article is going to attempt to familiarize you with the underlying Walker Classes and show a brief example of how to extend the Page Walker to output a custom sitemap.

What is the WordPress Walker Class?

To begin with the first question we need to answer is simply what is a Walker Class? In WordPress a Walker Class contains all the functionality needed to render an HTML representation of a collection of WordPress Objects. Where these objects could include Tags, Posts, Pages, Comments, and etc. To put it in plain english the walker is what creates the end HTML that will be displayed when calling a function. Without knowing it by calling the wp_list_pages function you are actually having the Walker_Page class render the final HTML based on your provided options.

A Walker class has 5 basic api methods that are used and will need to be extended based on your goal.

  • walk

    This function steps through the array of elements and calls the necessary start/end_lvl and start/end_el functions for each. The basic logic is as follows:

    1. If the element is a child of the previous element it calls start_lvl.
    2. For each element call start_el followed by end_el.
    3. If the element is no longer a child element then it calls end_lvl.
  • start_lvl

    concatenates the HTML element opening tag that will contain child elements to the output.

  • end_lvl

    concatenates the HTML element closing tag that contains the child elements to the output.

  • start_el

    concatenates the HTML representation of the object minus the closing HTML tag to the output.

  • end_el

    concatenates the HTML closing tag element of the object. to the output.

Creating your own Walker

The WordPress Walkers are contained in the core WordPress files and as a developer of a theme or plugin you want to define your walker in your own code base. For example if you are a theme developer you need to define your Walker in your Theme Functions file (functions.php). This way as new versions of WordPress come out your code does not get overwritten and you are also not asking users of your theme to modify WordPress core installation files.

To create a new Walker it is easiest and usually most time effective to extend a Walker already defined by WordPress. For our example of building a Sitemap Walker we will start by extending the Walker_Page class.

class Walker_Sitemap extends Walker_page {
  // code 
}

Now that we have created our Walker_Sitemap Walker class we need to override the display functions to display our sitemap. In this example we are adding a page description that will be outputted with the page title. Instead of utilizing the WordPress functionality to get a snippet of a page I chose to output a custom meta field called description.

function start_el(&$output, $page, $depth, $args, $current_page) {
		if ( $depth )
			$indent = str_repeat("\t", $depth);
		else
			$indent = '';
 
		extract($args, EXTR_SKIP);
 
		$output .= $indent . '<li><div class="link"><a href="' . get_page_link($page->ID) . '" title="' . esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $page->post_title, $page->ID ) ) ) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a></div>';
 
                $description = get_post_meta($page->ID, 'description', true);
 
                if( ! empty( $description ) ) {
			$output .= '<div class="description">' . $description . '</div>';
                }		
 
	}

As you can see in the code what we have done is simply overwritten the start_el function of the Walker_Page class with our desired HTML output. We have made it so that in addition to outputting the page link we are also outputting a div element that contains the pages description if it exists for the current page.
If wanted you could override the other Walker functions to change the container elements from a list (UL) to a DIV or any other desired HTML elements.

Using your newly created Walker

Now that you have created your Walker in your Theme Functions file the logical next step is to use it. This is done by calling the appropriate WordPress function passing in an instance of your Walker class. In our example we are outputting a sitemap which contains Pages and so we will be calling the wp_list_pages and passing in the parameter walker with an instance of Walker_Sitemap.

$sWalker = new Walker_Sitemap();
wp_list_pages( array( 'exclude' => $post->ID, 'title_li' => null, 'walker' => $sWalker ) );

You many want to note the exclusion of the current Page in the options. This is so that the sitemap page does not show up in the website’s sitemap as the visitor is already there and does not need a link to it.

Take Away

There are a million different uses that Walkers could be used for in WordPress. The next time you find yourself iterating through a collection of pages, posts, tags, or similar objects ask yourself if a Walker could save you time and effort by handling the iteration so you can focus on the output.

, , , , , , , , ,

A common task when creating a form based windows application is to allow a user to save their information and to also load saved information. In this short tutorial I will show you the basic steps to save a form’s information to a file and then load up the contents of the saved file.

Saving a Form’s Data to a File

The process for saving data to a file is pretty straightforward in Visual Basic. They include opening up a writer to a specified file, writing the contents, and then closing the writer when completed.

  1. Define and open your File Writer
    In this case we will utilize the StreamWriter class of the System.IO package. I will assume for these instructions that you have imported the System.IO library.
    Dim FileWriter As StreamWriter ' define the stream writer
    
    ' Open the File for writing
    FileWriter = New StreamWriter("c:\temp\sampleFile.txt", False)
  2. Write the desired data to the file
    To write data to the file you can utilize the WriteLine function. In the case of forms you may want to write different data. For a text field saving the Text is appropriate while when using a check box or radio button you will want to save the Checked property.
     ' Save Component values
    FileWriter.WriteLine(textBox.Name & "|" & textBox.Text)
    FileWriter.WriteLine(comboBox.Name & "|" & comboBox.SelectedIndex.ToString())
    FileWriter.WriteLine(radioBtn.Name & "|" & radioBtn.Checked.ToString())
  3. Close and Save your data to the File
    To complete your write you simply need to call Close on your writer. However if you want you can first call Flush to have the data written out. This step is optional however as the Close function will call Flush if needed.
     ' Save and Close File
    FileWriter.Flush() ' optional as close will flush
    FileWriter.Close()

Reading Data from a File

The process for reading information out of a File is similar to the writing process. First you want to open up the file, then you want to read a line of data, single character, or etc depending on how you are going to process the data until you reach the end. After you have read all the data you would lastly close the File.

  1. Define and open your File Reader
    In this case we will utilize the StreamReader class of the System.IO package.
     Dim FileReader As StreamReader   ' define the File Reader
    FileReader = New StreamReader("c:\temp\sampleFile.txt", False)
  2. Iterate until the End of File is reached
    Do Until FileReader.EndOfStream
        ' Processing Code goes here
    Loop
  3. Read in and Process the File’s data
    In the below example we will read in the file’s contents 1 line at a time. To see the full example of how the data is processed please view the full source code below.
    'Read the next line
    prop = FileReader.ReadLine()
  4. Close the File Reader
    After the loop close the FileReader as all the data has been read.
    FileReader.Close()

A complete sample Save and Write Class

For those that learn better by example below is a Simple Class that holds a complete example of saving data to a File from a Form in addition to loading the saved data from the form and setting the components to the correct state. The example contains basic error checking including validating that the saved file exists, however it does not validate the data in the file is of the correct format and not corrupted.

Imports System.IO
 
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadSavedData()
    End Sub
 
    Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
        Close() 'Close Application
    End Sub
 
    Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
        loadSavedData()
    End Sub
 
    Private Sub loadSavedData()
 
        Dim FileReader As StreamReader   ' define the File Reader
        Dim prop(2) As String            ' define array of length 2

        ' Test if the Saved File exists
        If File.Exists("c:\temp\sampleFile.txt") Then
 
            'Open the File for reading
            FileReader = New StreamReader("c:\temp\sampleFile.txt", False)
 
            ' Continue until EOF reached
            Do Until FileReader.EndOfStream
 
                'Read the next line and split its value based on the | character
                prop = FileReader.ReadLine().Split("|")
 
                ' If TextBox Component set it's property
                If String.Equals(prop(0), textBox.Name) Then
 
                    textBox.Text = prop(1)
 
                    ' If ComboBox Component set it's property
                ElseIf String.Equals(prop(0), comboBox.Name) Then
 
                    comboBox.SelectedIndex = Integer.Parse(prop(1))
 
                    ' If RadioButton set if checked
                ElseIf String.Equals(prop(0), radioBtn.Name) Then
 
                    radioBtn.Checked = Boolean.Parse(prop(1))
 
                ElseIf String.Equals(prop(0), RadioButton1.Name) Then
 
                    RadioButton1.Checked = Boolean.Parse(prop(1))
 
                ElseIf String.Equals(prop(0), RadioButton2.Name) Then
 
                    RadioButton2.Checked = Boolean.Parse(prop(1))
 
                End If
 
            Loop
 
            ' close the file
            FileReader.Close()
 
        End If
 
    End Sub
 
    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
 
        Dim FileWriter As StreamWriter ' define the stream writer

        ' Open the File for writing
        FileWriter = New StreamWriter("c:\temp\sampleFile.txt", False)
 
        ' Save Component values
        FileWriter.WriteLine(textBox.Name & "|" & textBox.Text)
        FileWriter.WriteLine(comboBox.Name & "|" & comboBox.SelectedIndex.ToString())
        FileWriter.WriteLine(radioBtn.Name & "|" & radioBtn.Checked.ToString())
        FileWriter.WriteLine(RadioButton1.Name & "|" & RadioButton1.Checked.ToString())
        FileWriter.WriteLine(RadioButton2.Name & "|" & RadioButton2.Checked.ToString())
 
        ' Save and Close File
        FileWriter.Flush() ' optional as close will flush
        FileWriter.Close()
    End Sub
End Class
, , , , , , , ,

Recently I was playing around with the SSL configurations within my PHP Wrapper Library for Harvest API and noticed that I was getting an access error. As it turns out cURL was behaving properly and trying to verify the SSL Certificate of the server, and as no CA Certificate was associated with the library it threw an exception and would fail. Doing some research on the subject I found myself with 2 viable options either download the server certificate and pass that along with my library or simply turn of validation of the SSL certificate. In my case validation is not important so I decided to turn it off.

The CURLOPT_SSL_VERIFYPEER option

One of the standard cURL options defined in PHP is CURLOPT_SSL_VERIFYPEER. This option is used to specify if when the url is SSL enabled if the certificate should be verified. To disable it simply set the option to false for the cURL instance.

$ch = curl_init();
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

If you are providing a data feed from your own server instead of accessing one provided by a 3rd party you may want to validate the certificate. In those cases you can find a useful tutorial on how to do so over at unit step.

, , ,