Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, August 7, 2012

Read Write Cookie with PHP

Save cookie
(before the body tag, set variables and the save cookie function for page postback)

if(isset($_GET['save_cookie']))
{
   setcookie("event[$id]","$time:$page:$link",time()+(86400*7),'/',$domain);
}

(within the body, use a form to incur post back)
<form class="add" method="get">
<input name="save_cookie" type="hidden" />

<input type="submit" value="+ Add to your day" />

<span class="ic-addtoyourday">Add to your day</span>
</form>

Read cookie
(loop through the cookie array)
    
    foreach ($_COOKIE['event'] as $key => $value)
    {
        //echo "$key:$value";
        $arr = explode(':', $value);
        echo '<a href="'.$arr[2].'">'; // link
        echo $arr[1]; // title
    }
    
    if(!isset($_COOKIE['event']))
    {
        echo 'You have not added any events.';
    }
    

Friday, November 25, 2011

[WordPress Contact Form 7] Add Numeric Type Field

This is about adding a numeric type "num" in Contact Form 7 (CF7, version 3.0.1) with minimum effort. This type only allows input to be numbers. Basically we will reuse a lot of the existing code in \wp-content\plugins\contact-form-7\modules\text.php It doesn't matter where do you put the code as long as they are in the same file.

1.
Register two shortcode:
wpcf7_add_shortcode( 'num', 'wpcf7_text_shortcode_handler', true );
wpcf7_add_shortcode( 'num*', 'wpcf7_text_shortcode_handler', true );

2.
Register the validation filter and implement the filter:
add_filter( 'wpcf7_validate_num', 'wpcf7_num_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_num*', 'wpcf7_num_validation_filter', 10, 2 );

function wpcf7_num_validation_filter( $result, $tag ) {
    $type = $tag['type'];
    $name = $tag['name'];

    $_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) );

    if ( 'num' == $type || 'num*' == $type ) {
        if ( 'num*' == $type && '' == $_POST[$name] ) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
        } elseif ( '' != $_POST[$name] && ! is_numeric( $_POST[$name] ) ) {
            $result['valid'] = false;
            $result['reason'][$name] = 'Numbers are required.';
        }
    }

    return $result;
}


3.
You can try to add your Tag Generator for the new type, but I just don't bother. To use the new field, it is the same as using [text] field, e.g. [num mobile /10 class:mobile]

Tuesday, November 22, 2011

[WordPress Contact Form 7] Populate Select Dropdown from Database

Contact Form 7 (CF7) is a popular plugin for the WordPress CMS. It supports a few basic form components that allows user building a form quickly. One particular problem I had is to populate a drop down selection box from content stored in the database (e.g., a list of vacant positions). To dynamically populate dropdown options is not an uncommon task for site builders. And if you know something about programming, this wouldn't be hard either.

With CF7, you can only fill your options by hand. I have looked around for solutions, and found this post on the forum. However, it only appears to be working but not quite -- every time it rebuilds the options from database regardless user input, and nothing will be sent on submission (see the last comment in this post).

I then had to worked out my way; I believe this is original, so copy right reserved *_^. Here is how. (By the way, the version I am using is Contact Form 7 3.0.1).

In the wp-content\plugins\contact-form-7\modules\ folder, find the select.php. This is the file to be modified. (In general, I do NOT like to hack into the plugin source code. This will make upgrade difficult.)

1.
In function wpcf7_select_shortcode_handler, $values, $labels and $defaults need to be redefined for your customised dropdown. They are arrays for dropdown values, dropdown texts and default selection respectively. I have the following snippet right after $options are parsed (after line 50). Here my select field ID is "position".

if ($name == 'position') {
  // query the positions
  $positions = new WP_Query('YOUR_QUERY');
  
  while( $positions->have_posts() ) : $positions->the_post();
    array_push( $values, get_the_ID() );     // post ID as option value
    array_push( $labels, get_the_title() );  // post title as option text
  endwhile;
  
  // set the key of the default selection
  array_push( $defaults, 1 + array_search($YOUR_DEFAULT_VALUE, $values) );
} 

2.
In function wpcf7_select_validation_filter, you need to re-fill the dropdown option values before the post operation because they cannot be found in the shortcode. They will be needed in $_POST later. I have the following snippet right after the local variable definitions. The code is very similar.

if ($name == 'position') {
  // query the positions
  $positions = new WP_Query('YOUR_QUERY');
  while( $positions->have_posts() ) : $positions->the_post(); 
    array_push( $values, get_the_ID() );
  endwhile;
} 

And that is it.