How to: Add a text and date field to WP-PHPList
WP-PHPList is a great plugin for Wordpress that integrates PHPList with Wordpress. It allows you to add a newsletter subscription form to any Wordpress page. Currently the plugin only supports one text and email field in the form. I needed extra functionality for a friends website so I decided to make additions to the plugin. The following guide and code will show you how to add another text field and a date field. This is not a straight forward guide. I will explain the changes I made to each function, but you must look at the code to understand it. An understanding of PHP code is recommended. My comments for the changes are in the code and are prefixed with “ronak”.
Changes are made to version 1.5 of the plugin.
Also, the code disables the subpanel created in Wordpress and all configuration is done through the code. You can change this if you like.
Download Code
Design
- The design is fairly simple. User input is taken in, validation is done on the input, and cURL is used to POST the input to the same page that the PHPList subscription page POSTs to.
- The code comprises of some global variables and functions. I don’t necessarily agree with the design of the plugin, but it works great.
Variables (Note: These are not all the variables used, only the one I edited.)
- $phplist_strings - This global variable is an array that holds the html for each field in the form. Just follow the code to add more fields.
Functions (Note: These are not all the functions used, only the ones I edited or created)
- generate_date_input($o, $day, $month, $year) – I created this function to create the html for the date fields. It takes an array of options ($o) returned by phplist_get_options() which I will get to soon and $day, $month, $year (ranges: 1-31, 01-12, and 1925 – current year respectively). If any of those three variables are set then that field will be selected with that value. If any of the three variables are equal to an empty string, then that part of todays date is used. This is used for keeping the values you selected persistant when an error or invalid input is triggered.
- phplist_subscribe($input_data) – This function takes in the data from the subscription form POST, formats the data, and performs the cURL POST. I made modifications to how array data is formatted in the POST since the original code did not deal with dates.
- phplist_get_options() – This function returns the options for the form. The changes made to this function add options for new fields and also disables the loading of options from the subpanel page.
- phplist_check_input() – This function validates user input. I added validation for the added text field and I’ve also added persistence for the date field since the page is reloaded when invalid input is entered.
- phplist_construct_form() – This function creates the newsletter subscription form. The form has been redesigned.
The changes I made are not complicated and I feel that they are easily decipherable. Post any questions in the comments.
April 22nd, 2009 at 4:58 am
[...] has put together a code example explaining how to add additional fields to the PHPList plugin. Thanks Ron! if (typeof [...]
August 16th, 2009 at 12:51 am
is the plugins can be use, without any changes ?i had try it, no luck..