{"id":1073,"date":"2014-07-01T21:31:58","date_gmt":"2014-07-02T01:31:58","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=1073"},"modified":"2016-08-22T19:41:03","modified_gmt":"2016-08-22T23:41:03","slug":"filter-functions","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=1073","title":{"rendered":"Filter Functions"},"content":{"rendered":"<h3>Situation<\/h3>\n<p>In a short code or export link you want to have a filter expression that calls a PHP function on a field value. Example: You want to check for a value in a case-insensitive way: [cfdb-table form=&#8221;form1&#8243; show=&#8221;first_name&#8221; <span style=\"color: #0000ff;\">filter=&#8221;strtoupper(first_name)=MIKE&#8221;<\/span>] Output looks like:<\/p>\n<table>\n<tbody>\n<tr>\n<td>first_name<\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #0000ff;\">Mike<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Here we use the PHP function <a href=\"http:\/\/www.php.net\/manual\/en\/function.strtoupper.php\" target=\"_phpdoc\">strtoupper<\/a> to upper-case the value in the <strong>first_name<\/strong> field and compare it with &#8220;<strong>MIKE<\/strong>&#8220;. This does not upper case first_name when it is displayed. It only upper cases it for the purpose of doing the comparison. So if the actual value for first_name is &#8220;Mike&#8221; the filter would pass but you would still see &#8220;Mike&#8221; displayed by the short code output. Alternatively, a filter does not need to be compared to a value if it just returns true or false. For example, the following short code filters for all &#8220;your-email&#8221; fields that have &#8220;@gmail.com&#8221; in them. [cfdb-table form=&#8221;myform&#8221; <span style=\"color: #0000ff;\">filter=&#8221;strpos(your-email,@gmail.com)&gt;1&#8243;<\/span>]<\/p>\n<h3>Filter\u00a0Functions vs.\u00a0Transforms Functions<\/h3>\n<p>Instead of upper-casing first_name in the filter, we can upper-case it before it gets to the filter. Using a transform, we change the actual value of the first_name field. Then when it gets to the filter, it is already upper-cased. Furthermore, it is displayed by the short code as upper-cased. [cfdb-table form=&#8221;form1&#8243; show=&#8221;first_name&#8221;\u00a0<span style=\"color: #0000ff;\">trans=&#8221;first_name=strtoupper(first_name)&#8221; filter=&#8221;first_name=MIKE&#8221;<\/span>] Output looks like:<\/p>\n<table>\n<tbody>\n<tr>\n<td>first_name<\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #0000ff;\">MIKE<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>See more about <a href=\"http:\/\/cfdbplugin.com\/?page_id=1076\">Transform Functions<\/a>. The rest of this page focuses on Filter Functions<\/p>\n<h3>What Functions Can Be Used?<\/h3>\n<p>Potentially any PHP function can be used, including those you create. But there are only a small set that you can use with default settings. These include most string-related PHP functions.<\/p>\n<h3>Function Security<\/h3>\n<p>A user who can create a short code (or export link) could potentially run a PHP function that might harm your installation or data. Therefore security restrictions are in place by default. This security can be turned off entirely in the administration page <strong>Contact Form DB<\/strong> -&gt; <strong>Options<\/strong>\u00a0-&gt;\u00a0<strong>Allow Any Function in Short Codes<\/strong>. If that is set to <strong>true<\/strong> then users which role for\u00a0<strong>Can See Submission when using shortcodes<\/strong> can write a short code or compose a URL that can run any PHP function. It is best to keep this option to <strong>false<\/strong>. Alternatively, individual functions that the administrator deems as safe can be registered as being permissible to use.<\/p>\n<h3>Permitting a Function and Creating Custom Functions<\/h3>\n<p>To permit individual functions, you will need to be able to add PHP code to register function names. First, install the\u00a0<a href=\"http:\/\/wordpress.org\/extend\/plugins\/add-actions-and-filters\/\" target=\"_blank\">Shortcodes Actions and Filters plugin<\/a>\u00a0and activate it. From the administration page, go to the\u00a0<strong>Tools<\/strong>\u00a0menu -&gt;<strong>\u00a0Shortcodes Actions and Filters<\/strong>. Put your code there and save. Syntax errors will be displayed when you save. Example: to register a custom function &#8220;cfdb_reverse&#8221; define the function the call cfdb_register_function.<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBPermittedFunctions.php');\r\n\r\nfunction cfdb_reverse($text) {\r\n  return strrev($text);\r\n}\r\n\r\ncfdb_register_function('cfdb_reverse');<\/pre>\n<p>You can then use the function in a filter, e.g. [cfdb-table form=&#8221;myform&#8221; <span style=\"color: #0000ff;\">filter=&#8221;cfdb_reverse(first_name)=ekim&#8221;<\/span>] Existing PHP and WordPress functions can be added using cfdb_register_function(<em>function_name<\/em>).<\/p>\n<h3>Function Parameters<\/h3>\n<p>Your custom function can take zero or more parameters. Parameters can be:<\/p>\n<ul>\n<li><strong>Form field names<\/strong> (such as first_name) in which case the value of the field is used, e.g.\n<ul>\n<li><span style=\"color: #0000ff;\">filter=&#8221;strtoupper(first_name)=MIKE&#8221;<\/span><\/li>\n<\/ul>\n<\/li>\n<li><strong><a href=\"http:\/\/cfdbplugin.com\/?page_id=116\">Filter variables<\/a><\/strong>, e.g. $user_login, POST, GET, and COOKIE values. E.g.\n<ul>\n<li><span style=\"color: #0000ff;\">filter=&#8221;last_name=my_lookup_user($user_login)&#8221;<\/span><\/li>\n<li><span style=\"color: #0000ff;\">filter=&#8221;last_name=my_lookup_user($POST(lname))&#8221;<\/span><\/li>\n<\/ul>\n<\/li>\n<li><strong>PHP constants<\/strong>, e.g. &#8220;STR_PAD_BOTH&#8221; in\n<ul>\n<li><span style=\"color: #0000ff;\">filter=&#8221;str_pad(first_name,\u00a010,\u00a0&#8220;_&#8221;,\u00a0STR_PAD_BOTH)=&#8221;___Mike___&#8221;<\/span><\/li>\n<\/ul>\n<\/li>\n<li><strong>Literal values<\/strong>. Anything not recognized as one of the above will be passed as a string to the function. Do not quote the string.\n<ul>\n<li>Any function call that is not recognized or not permitted is treated as a string literal<\/li>\n<\/ul>\n<\/li>\n<li><strong>Multiple parameters<\/strong>: any combination of the above. Separate parameters with commas.\n<ul>\n<li><span style=\"color: #0000ff;\">filter=&#8221;user=my_lookup_user(first_name,last_name,$user_login)&#8221;<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Passing the entire form entry: if no parameters are listed as being passed to the function, then an associative array of all the form fields is passed. This is useful if you would like to write a function that filters on more than one field as a time. Example:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBPermittedFunctions.php');\r\n\r\nfunction check_address($street, $state, $zip){\r\n  \/\/ TODO: put your code here\r\n  return true;\r\n}\r\n\r\nfunction has_valid_address($entry) {\r\n  return check_address(\r\n    $entry['street'],\r\n    $entry['state'],\r\n    $entry['zip']);\r\n}\r\n\r\ncfdb_register_function('has_valid_address');<\/pre>\n<p>You can then use the function in a filter, e.g. [cfdb-table form=&#8221;myform&#8221; <span style=\"color: #0000ff;\">filter=&#8221;has_valid_address()&#8221;<\/span>]<\/p>\n<h3>More on Filter Function Syntax<\/h3>\n<p>General forms you can use: 1. Functions that return true or false: your function returns true or false indicating if the filter passes: [cfdb-table form=&#8221;myform&#8221;\u00a0<span style=\"color: #0000ff;\">filter=&#8221;has_valid_address()&#8221;<\/span>] 2. Functions that return a value. Transforms a field value for comparison with a literal value [cfdb-table form=&#8221;myform&#8221;\u00a0<span style=\"color: #0000ff;\">filter=&#8221;strtoupper(first_name)=MIKE&#8221;<\/span>] 3. Combinations using <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#filter\">existing filter Boolean logic<\/a> (&amp;&amp;=AND, ||=OR) [cfdb-table form=&#8221;myform&#8221;\u00a0<span style=\"color: #0000ff;\">filter=&#8221;strtoupper(first_name)=MIKE&amp;&amp;strtoupper(last_name)=SIMPSON&#8221;<\/span>]<\/p>\n<h3>Debugging<\/h3>\n<p>Add to your short code <span style=\"color: #0000ff;\">debug=&#8221;true&#8221;<\/span> to make the short code display how it parses its expression. The important thing to remember for functions is that a function will be parsed into an array where the first value is the function name followed by an array element for each parameter. If the function is not allowed or not recognized then the entire function is shown still as a whole string. See <a href=\"http:\/\/cfdbplugin.com\/?page_id=124\">Debugging Filter Expressions<\/a>. <strong>Function is parsed:<\/strong><\/p>\n<pre style=\"color: #2b2b2b;\">'cfdb_reverse(first_name)=ekiM'\r\nArray\r\n(\r\n    [0] =&gt; Array\r\n        (\r\n            [0] =&gt; Array\r\n                (\r\n                    <span style=\"color: #0000ff;\">[0] =&gt; Array\r\n                        (\r\n                            [0] =&gt; cfdb_reverse\r\n                            [1] =&gt; first_name\r\n                        )\r\n<\/span>\r\n                    [1] =&gt; =\r\n                    [2] =&gt; ekiM\r\n                )\r\n\r\n        )\r\n\r\n)<\/pre>\n<p><strong>Function is not parsed:<\/strong><\/p>\n<pre style=\"color: #2b2b2b;\">'cfdb_reverse(last_name)=ekiM'\r\nArray\r\n(\r\n    [0] =&gt; Array\r\n        (\r\n            [0] =&gt; Array\r\n                (\r\n                    <span style=\"color: #0000ff;\">[0] =&gt; cfdb_reverse(last_name)<\/span>\r\n                    [1] =&gt; =\r\n                    [2] =&gt; 1ih\r\n                )\r\n\r\n        )\r\n\r\n)<\/pre>\n<pre style=\"color: #2b2b2b;\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Situation In a short code or export link you want to have a filter expression that calls a PHP function on a field value. Example: You want to check for a value in a case-insensitive way: Output looks like: first_name Mike Here we use the PHP function strtoupper to upper-case the value in the first_name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1069,"menu_order":2,"comment_status":"open","ping_status":"open","template":"page-without-sidebar.php","meta":{"footnotes":""},"class_list":["post-1073","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-hj","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1073"}],"version-history":[{"count":22,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1073\/revisions"}],"predecessor-version":[{"id":1367,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1073\/revisions\/1367"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1069"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}