Filter Variable Substitution

This page is a continuation of documentation of the “filter” attribute of shortcodes. First see this page.

Identifying Logged-in User

If the user is logged in when viewing the page with the shortcode, you can try to match a filter value against some user information. If the user was logged in when he submitted the form, then ‘Submitted Login’ will be captured (since version 1.4.4) So if the user is also logged in to view a page with this shortcode, you could have the table filter to show him only his submissions using:

  • [cfdb-table form="your-form" filter="Submitted Login=$user_login"]

Similarly, if the user entered his email in a form field, (say “email”), and perhaps was not logged in but entered the same email address as is associated with his WordPress account, then later came back to view a page when logged in, you could show him his entry using:

  • [cfdb-table form="your-form" filter="email=$user_email"]

All of the following variables are supported

  • $user_login
  • $user_email
  • $first_name or $user_firstname
  • $last_name or $user_lastname
  • $user_nicename
  • $id or $ID

Using HTTP GET, POST and COOKIE variables

When viewing a page or post, you can add HTTP GET parameters on the URL, for example the URL to view post #85 might be:

to which you could add some arbitrary parameter:

in this case you might want to use that email=joe@nowhere.com in the shortcode filter. Assuming the table you are querying has a field named ‘contact_email’, you could use the shortcode:

  • [cfdb-table form="your-form" filter="contact_email=$_GET(email)"] This looks for form submissions where the submitted value for the form’s contact_email field is equal to joe@nowhere.com.

This syntax can be used:

  • $_GET(http_get_parameter) for URL parameters as described above or forms posting to the page on which the shortcode is located, where the form uses method=GET
  • $_POST(http_post_parameter) for forms posting to the page on which the shortcode is located, where the form uses method=POST
  • $_COOKIE(http_cookie_name) to reference Cookies.

WARNING PHP programmers: note the syntax and don’t get confused with similar PHP syntax:

  • $_GET(value) not $_GET['value']
  • $_POST(value) not $_POST['value']
  • $_COOKIE(value) not $_COOKIE['value']

Summary of differences from PHP syntax:

  • Parentheses are used instead of square brackets because the shortcode already has brackets and we can’t nest them within it.
  • Quoting ‘value’ is not necessary since you are already quoting the shortcode attribute, and this would result in nested quotes.

Problems when using $_GET

1. ‘name’ Issue

Avoid using 'name' as a GET parameter. This example will not work:

this gives you a page with the error:

  • Apologies, but the page you requested could not be found. Perhaps searching will help.

The problem is with using name. Use something else, like name1 and use $_GET(name1) in your filter. In WordPress, your URL does not go directly to the page, it goes to http://mywordpress.com/ in this example and that takes the parameters and dispatches it to the appropriate page/post etc. So you have to choose GET parameters names that do not conflict with those that WordPress uses. “name” is such a conflict. I don’t a list of all conflicts, but look for the above error.

2. Using a Form with method=”GET” to post to your page with table

Imagine this scenario:

  • (start) You create a page with a form on it where users input parameters
  • (end) That form posts to another page (or same page) that has a shortcode using $_GET in the filter

If the URL to your end page already has a URL parameter in it, then you need to add it to your form as a hidden field. For example, your end page URL is http://mysite.com/?p=246

Then on your start page, you need to add that “p” as a hidden parameter, for example:

Using GET: start page:

<form action="http://mysite.com/" method="GET">
<input type="hidden" name="p" value="246/>
Field1: <input name="f1"/> <br/>
Field2: <input name="f2"/> <br/>
<input type="submit"/>
</form>

Using GET: end page

[cfdb-datatable form="myform" filter="Field1=$_GET(f1)&&Field2=$_GET(f2)"]

But if you use “POST”, then you don’t have this problem:

Using POST: start page:

Note the p=246 is in the action URL, unlike the “GET” example.
<form action="http://mysite.com/?p=246" method="POST">
Field1: <input name="f1"/> <br/>
Field2: <input name="f2"/> <br/>
<input type="submit"/>
</form>

Using POST: end page:

[cfdb-datatable form="myform" filter="Field1=$_POST(f1)&&Field2=$_POST(f2)"]

  1. June 17th, 2011 at 05:29 | #1

    Can $_GET(f1) be used with search instead of filter? With the filter option the column has to match exactly… it’d be nice if it could look for part of the column instead of just starts with. Is that possible?

  2. Michael Simpson
    June 17th, 2011 at 10:05 | #2

    @Allyn Beth
    Unfortunately no. I agree that would be good to add.

  3. Tristan
    August 4th, 2011 at 20:25 | #3

    Awesome plugin, this does exactly what I needed (submit to database then retrieve from database). Thanks!

  4. March 10th, 2012 at 13:59 | #4

    I am trying to use the following filter without success. Any help would be appreciated.
    filter=”items~~/.*$_GET(itemsearch).*/i”

    The only thing I’m able to get to work is filter=”items=$_GET(itemsearch)”.

    And BTW, the plugin works awesome! Thank you.

    • Michael Simpson
      March 12th, 2012 at 17:19 | #5

      A good observation. I started working on updating the code to make this work. Hopefully I can push an update with that within a week.

  5. Michael Simpson
    March 18th, 2012 at 22:48 | #6

    @Bob McCool
    Try it now in version 2.3.

  6. Juan
    June 27th, 2012 at 14:13 | #7

    Can I use 2 filters at the same time?

    • Michael Simpson
      June 27th, 2012 at 17:31 | #8

      Only one “filter” value is allowed in one short code. But you can use logical AND && and OR || to combine filter constraints.

  7. February 21st, 2013 at 07:41 | #9

    Hi Michael,
    Please visit search property page of my website and suggest me the the code to filter according to user requirements by Sale value and city(both the filter at the same time). The user need not login, he can just search his requirement without registering…Please help me out, thanks for the great plugin can be used as a ecommerce plugin.
    Thanks in advance

  8. Umeed
    April 4th, 2013 at 08:29 | #11

    Hello,

    Thanks for lovely plugin. I have place the short code in a page. and when the user submit the form the thank page will be the page I have entered the data. The problem is here when the user submit the form he will redirect to a page which he see all if the emails submitted all time. So I want that he just get the email he submited then if he wants can print it.

    is it possible?

    Thanks
    Umeed

  9. SeanW
    July 9th, 2014 at 13:21 | #12

    I created a form with a Date field and a Time field that get populated on submit.
    Is there a way to filter between two dates based on user input?
    Thanks

  10. Michael Simpson
    July 10th, 2014 at 00:38 | #13

    @SeanW
    In a short code you can call the PHP strtotime function to covert the string date to a number and compare it. User input can be passed in as GET or POST parameters. The short code would look something like:

    [cfdb-table form=”myform” filter=”strtotime(date_field)>=$_GET(min_date)&&strtotime(date_field)< =$_GET(max_date)]

    Filters in functions: http://cfdbplugin.com/?page_id=1073
    Variables in functions:
    http://cfdbplugin.com/?page_id=116

  11. mike123
    August 8th, 2014 at 00:38 | #14

    Hi Michael –
    I want to limit a cfdb-datatable and a cfdb-html to rows in which the date field “first-day” is no farther in the past than one day or one week.

    I have tried the following three things:
    filter=”strtotime(first-day)>-1 days”
    filter=”strtotime(first-day)>-1 day”
    filter=”strtotime(first-day)>-1 week”

    All of them filter out all the rows — nothing is displayed. Any suggestions will be greatly appreciated!
    Mike

  12. mike123
    August 8th, 2014 at 00:40 | #15

    I have tried these in the datatable. Haven’t played with the cfdb-html yet.

  13. Michael Simpson
    August 8th, 2014 at 12:47 | #16

    @mike123
    try:
    filter=”strtotime(first-day)>strtotime(-1 days)”

  14. mike123
    August 8th, 2014 at 13:10 | #17

    Hi Michael –

    Thanks for the response. Unfortunately, this also removes all the data from the datatable. All i see is the message, “No data available in table”.

    Any other ideas will be greatly appreciated!

    Mike

  15. Michael Simpson
    August 8th, 2014 at 14:32 | #18

    @mike123
    I was missing single quotes: filter=”strtotime(first-day)>strtotime('-1 days')”
    See http://cfdbplugin.com/?page_id=1195

  16. mike123
    August 8th, 2014 at 14:47 | #19

    Hmmm. This doesn’t filter anything off the table. I also tried putting single quotes on ‘first-day’ but that didn’t help.

    I’m thinking maybe this type of filtering works on cfdb-table but not dfdb-datatable ?

    • Michael Simpson
      August 8th, 2014 at 16:44 | #20

      @mike123
      Make sure the double quotes are normal straight quotes, not the slanted ones that this blog displays them as. The type of short code (cfdb-table or cfdb-datatable) is irrelevant.

      I was able to successfully use this short code for a form that I have which has a “date” field.

      [cfdb-table form="Date Form" filter="strtotime(date)>strtotime('-1 month')"]
You must be logged in to post a comment.