{"id":444,"date":"2011-06-21T15:53:18","date_gmt":"2011-06-21T19:53:18","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=444"},"modified":"2016-12-15T21:55:33","modified_gmt":"2016-12-16T02:55:33","slug":"create-your-own-short-code","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=444","title":{"rendered":"[Create Your Own Short Code]"},"content":{"rendered":"<p>If the available short codes are not giving you what you want, try creating your own.<\/p>\n<p><!--span style=\"font-style:italic; background-color: #fffacd;\" >Don't want to do programming? <a href=\"http:\/\/cfdbplugin.com\/?p=707\">I'll write your short code for you for a fee<\/a>.<\/span--><\/p>\n<p>You can achieve this by using a plugin that allows you to create short codes that include PHP code that you provide and using an API provided by this plugin to retrieve saved form data from the database.<\/p>\n<p><strong>To get set up to do this<\/strong><\/p>\n<ol>\n<li>Install the plugin <a href=\"https:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\" target=\"_blank\">Shortcodes Actions and Filters<\/a>. This plugin provides a page in your WP administration area that allows you to create shortcodes and provide it with PHP code to execute.<\/li>\n<li>Have a look at the FAQ on <a href=\"http:\/\/cfdbplugin.com\/?page_id=367\" target=\"_blank\">Accessing Form Data via PHP<\/a> for some information on the API.<\/li>\n<\/ol>\n<p><strong>To create your own short code with your own code:<\/strong><\/p>\n<ol>\n<li>Go to your WP Admin, <strong>Tools<\/strong> -&gt; <strong>Shortcodes Actions and Filters<\/strong><\/li>\n<li><strong>Add New<\/strong><\/li>\n<li>Give it a name it the top box (like &#8220;myshortcode&#8221;). Name it whatever you want (but no spaces in the name)<\/li>\n<li>Check &#8220;Active&#8221; and &#8220;Shortcode&#8221;<\/li>\n<li>Copy the <strong>template code<\/strong> below, and paste into the code section<\/li>\n<li>Click <strong>Save<\/strong>, just to get it saved<\/li>\n<li>Replace &#8220;\/\/ Add your stuff here!&#8221; with your code (see below)<\/li>\n<li><strong>Save<\/strong> it.<\/li>\n<li>The best way to test is to <strong>create a new draft unpublished post<\/strong>, put your shortcode in there minimally with a form name <strong>[myshortcode form=&#8221;myform&#8221;]<\/strong> then preview the post to see what is displays.<\/li>\n<li>Use the shortcode on any pages\/posts you like. Use : <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#show\" target=\"_blank\"><strong>show<\/strong><\/a>, <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#hide\" target=\"_blank\"><strong>hide<\/strong><\/a>, <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#limit\" target=\"_blank\"><strong>limit<\/strong><\/a>, <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#orderby\" target=\"_blank\"><strong>orderby<\/strong><\/a>, <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#search\" target=\"_blank\"><strong>search<\/strong><\/a>, and <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#filter\" target=\"_blank\"><strong>filter<\/strong><\/a> just as you would for other shortcodes.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2016\/01\/newshortcode.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-489\" title=\"myshortcode1\" src=\"http:\/\/cfdbplugin.com\/wp-content\/uploads\/2016\/01\/newshortcode.png\" alt=\"\"\/><\/a><\/p>\n<p><strong>Template code<\/strong>: copy this code and paste in this code:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBFormIterator.php');\r\n$exp = new CFDBFormIterator();\r\n$exp-&gt;export($atts['form'], $atts);\r\nwhile ($row = $exp-&gt;nextRow()) {\r\n\u00a0\/\/ Add your stuff here!\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&#8220;<strong>Add your stuff here!<\/strong>&#8221; Delete this line and replace with PHP code that outputs the HTML you want to see for one entry. Use PHP &#8220;echo&#8221; to output text, reference form values using <strong>$row[&#8216;field-name&#8217;]<\/strong> where field-name is the name of the field or column. Let&#8217;s look at a real user&#8217;s example:<\/p>\n<p><strong>Example<\/strong>: One user was trying to use [cfdb-html] but it didn&#8217;t quite give him what he wanted. For one of his fields, &#8220;url&#8221;, he only wanted it to output an HTML link for that url but only if it was not blank. Unfortunately, [cfdb-html] outputs each form submission exactly the same, so it would make empty links for empty url fields. More generally speaking, [cfdb-html] can&#8217;t change its output based on what data is (or is not) in the form submission.<\/p>\n<p>So he created his own shortcode named &#8216;bios&#8217; like this:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBFormIterator.php');\r\n$exp = new CFDBFormIterator();\r\n$exp-&gt;export($atts['form'], $atts);\r\nwhile ($row = $exp-&gt;nextRow()) {\r\n\u00a0\u00a0\u00a0 echo '&lt;h2&gt;' . $row['name'] . '&lt;\/h2&gt;';\r\n\u00a0\u00a0\u00a0 echo '&lt;p&gt;' . $row['bio'] . '&lt;\/p&gt;';\r\n\u00a0\u00a0\u00a0 if ($row['url'] != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo '&lt;p&gt;&lt;a href=\"' . $row['url'] . '\"&gt;Website&lt;\/a&gt;&lt;\/p&gt;';\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>Notice how the code outputs a new paragraph &lt;p&gt; with a link &lt;a&gt; ONLY for only those submissions that have a URL entry.<\/p>\n<p>Since he named his shortcode &#8216;<strong>bios<\/strong>&#8216;, and his form name was &#8220;<strong>people<\/strong>&#8220;, he can use use the following shortcode on a page or post:<\/p>\n<pre>[bios form=\"people\"]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Hard-coding &#8216;form&#8217;<\/strong>: But in this case, the shortcode is only for the &#8220;people&#8221; form since only that form has the &#8216;name&#8217;, &#8216;bio&#8217; and &#8216;url&#8217; columns. If he wanted to, he could hard-code the form name in the short code&#8217;s PHP code by changing:<\/p>\n<pre><code>$exp-&gt;export(<span style=\"color: #ff0000;\">$atts['form']<\/span>, $atts);<\/code><\/pre>\n<p>to:<\/p>\n<pre><code>$exp-&gt;export(<span style=\"color: #ff0000;\">'people'<\/span>, $atts);<\/code><\/pre>\n<p>then on his page\/post, he could just use:<\/p>\n<pre><code>[<\/code><code>bios<\/code><code><\/code><code><\/code><code><\/code><code>]<\/code><\/pre>\n<p>And of course, if he wanted to just show only 10 submissions sorted by name, he could use:<\/p>\n<pre><code>[<\/code><code>bios<\/code><code> <\/code><code>limit=\"10\" orderby=\"name\"<\/code><code><\/code><code>]<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Warning about &#8220;extract&#8221;<\/strong>: OK, so you know PHP, you looked at the code above and said, &#8220;I don&#8217;t want to use the verbose $row[&#8216;field-name&#8217;] syntax, I&#8217;m going to the <a href=\"http:\/\/php.net\/manual\/en\/function.extract.php\" target=\"_blank\">PHP extract function<\/a> to make my fields into variables then I can use actual field names as variables and embed them into double-quoted strings.&#8221; Kind of like this:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBFormIterator.php');\r\n$exp = new CFDBFormIterator();\r\n$exp-&gt;export($atts['form'], $atts);\r\nwhile ($row = $exp-&gt;nextRow()) {\r\n\u00a0\u00a0\u00a0 extract($row);\r\n\u00a0\u00a0\u00a0 echo \"&lt;h2&gt;$name&lt;\/h2&gt;\";\r\n\u00a0\u00a0\u00a0 echo \"&lt;p&gt;$bio&lt;\/p&gt;\";\r\n\u00a0\u00a0\u00a0 if ($url != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo \"&lt;p&gt;&lt;a href='$url'&gt;Website&lt;\/a&gt;&lt;\/p&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0} \r\n}<\/pre>\n<p>Seems a bit more concise, right? It is and it works in this example but only because the field names (&#8220;name&#8221;, &#8220;bio&#8221;, &#8220;url&#8221;) can be PHP variable names. When you create a form using Contact Form 7, it&#8217;s default\/generated form has fields names have a dash in them (like &#8220;your-name&#8221;, &#8220;text-157&#8221;, &#8220;textarea-553&#8221;). The PHP extract will not work right as coded above because you can&#8217;t have a PHP variable with a dash in it ($your-name not allowed!). So you either have to<\/p>\n<ol>\n<li>Use the $row[&#8216;your-name&#8217;] to refer to a value (and consequently can&#8217;t embed that in a PHP double-quoted string) or<\/li>\n<li>You have to ensure all form field names are suitable to be PHP variable names, then you can use &#8216;extract&#8217; as in the above example.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>About $atts<\/strong>: the $atts variable is an associative array of all the &#8220;attributes&#8221; of the shortcode.\u00a0 In this code, we are passing $atts to $exp-&gt;export(). That mean that all of this plugin&#8217;s standard short code options can be used. They are: <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#show\" target=\"_blank\"><strong>show<\/strong><\/a>, <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#hide\" target=\"_blank\"><strong>hide<\/strong><\/a>, <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#limit\" target=\"_blank\"><strong>limit<\/strong><\/a>, <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#orderby\" target=\"_blank\"><strong>orderby<\/strong><\/a>, <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#search\" target=\"_blank\"><strong>search<\/strong><\/a>, and <a href=\"http:\/\/cfdbplugin.com\/?page_id=89#filter\" target=\"_blank\"><strong>filter<\/strong><\/a>. Thus, you could write a short code like: <strong>[myshortcode form=&#8221;Contact form 1&#8243; show=&#8221;col1,col2&#8243; search=&#8221;Mike&#8221; limit=&#8221;10&#8243;]<\/strong>.<\/p>\n<p><strong>Add your own attributes<\/strong>: you can add your own attributes simply by adding them to your shortcode call and referencing them in the PHP via $atts[&#8216;attribute-name&#8217;]. For example, you might add an &#8220;email_user&#8221; field like this:<\/p>\n<p><strong>[myshortcode form=&#8221;Contact form 1&#8243; show=&#8221;col1,col2&#8243; search=&#8221;Mike&#8221; limit=&#8221;10&#8243; <span style=\"color: #ff0000;\">email_user=&#8221;true&#8221;<\/span>]<\/strong><\/p>\n<p>And have code like this:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBFormIterator.php');\r\n$exp = new CFDBFormIterator();\r\n$exp-&gt;export($atts['form'], $atts);\r\nwhile ($row = $exp-&gt;nextRow()) {\r\n    echo '&lt;p&gt;';\r\n    foreach ($row as $name =&gt; $value) {\r\n    \techo \"$name=$value&lt;br\/&gt;\";\r\n    }\r\n    if ($atts['email_user'] == 'true') {\r\n       wp_mail($row['email'], 'Hello ' . $row['name'], 'How are you doing?');\r\n}\r\n\u00a0echo '&lt;\/p&gt;';\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>Issue with line breaks inside fields<\/strong><\/p>\n<p>You may have a field that contains text with line breaks. This comes from text area type form fields. But when you echo it out, you don&#8217;t see the line breaks. This is because you need to convert regular line break characters (which browsers ignore) to HTML line break tags (&lt;br\/&gt;). To do this, use the PHP <a href=\"http:\/\/www.php.net\/manual\/en\/function.nl2br.php\" target=\"_blank\">nl2br<\/a>\u00a0function, like this (assuming you had a &#8220;description&#8221; field that might have line breaks in it):<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBFormIterator.php');\r\n$exp = new CFDBFormIterator();\r\n$exp-&gt;export($atts['form'], $atts);\r\nwhile ($row = $exp-&gt;nextRow()) {\r\n    echo nl2br($row['description']);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Links to uploaded files<\/strong><\/p>\n<p>(Since version 2.5.5) In cases where you have uploaded files in your form you may want to have a link to the file or embed it if it is an image. In these cases, the $row array will include an additional entry with the URL for each field. For example, if your form had a field called &#8220;upload&#8221; for uploading a file, then you will have the file name as $row[&#8216;upload&#8217;] and the URL to that file as $row[&#8216;upload_URL&#8217;]. By convention, for any field &#8220;x&#8221; that is a file, you will have a &#8220;x_URL&#8221; entry in $row. (Be sure not to name form fields that conflict with this).<\/p>\n<p>Example: where a form has an &#8216;upload&#8217; field that contains a file<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBFormIterator.php');\r\n\r\n$exp = new CFDBFormIterator();\r\n\r\n\/\/ $atts is the array of short code attributes in Shortcode Exec PHP\r\n$formName = $atts['form'];\r\n\r\n\/\/ Name of the field that is a file uploaded from a form\r\n$fieldName = 'upload'; \/\/ change to name of your form's field that has a file\r\n\r\n\/\/ Pseudo field name that is the URL to the uploaded file\r\n$fieldNameUrl = $fieldName . '_URL';\r\n\r\n$exp-&gt;export($formName, $atts);\r\nwhile ($row = $exp-&gt;nextRow()) {\r\n    $fileName = $row[$fieldName];\r\n    if (isset($row[$fieldNameUrl])) {\r\n        $fileUrl = $row[$fieldNameUrl];\r\n\r\n        \/\/ Make a link to the file\r\n        printf('&lt;a href=\"%s\"&gt;%s&lt;\/a&gt;&lt;br\/&gt;', $fileUrl, $fileName);\r\n   \t\/\/ or embed file image\r\n   \t\/\/printf('&lt;img alt=\"%s\" src=\"%s\"\/&gt;&lt;br\/&gt;', $fileName, $fileUrl);\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<div><strong><br \/>\n<\/strong><\/div>\n<p><strong>Adding Security<\/strong><br \/>\nYou may wish to limit what kind of user can see the results of your short code. The short codes that are provided by this plugin such as cfdb-table are all controlled by the plugin setting: in the admin page, Database Options =&gt; <strong>Can See Submission when using shortcodes<\/strong>. This can be set to values such as &#8220;Anyone&#8221;, &#8220;Subscriber&#8221;, &#8220;Editor&#8221;, etc.<\/p>\n<p>By default, your custom short code does is <strong>NOT<\/strong> controlled by this setting. You may wish to make it be, or you might want to have it controlled independently.<\/p>\n<p>If you wish to have your custom short code be restricted based on the &#8220;<strong>Can See Submission when using shortcodes<\/strong>&#8221; setting, then put at the top of your short code PHP code:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CF7DBPlugin.php');\r\n$plugin = new CF7DBPlugin;\r\nif (!$plugin-&gt;canUserDoRoleOption('CanSeeSubmitDataViaShortcode')) return;\r\n<\/pre>\n<p>Instead, if you wish to restrict your short code to only those uses with &#8216;Editor&#8217; role, then put in this code at the top of your short code:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CF7DBPlugin.php');\r\n$plugin = new CF7DBPlugin;\r\nif (!$plugin-&gt;isUserRoleEqualOrBetterThan('Editor')) return;\r\n<\/pre>\n<p>Replace &#8216;Editor&#8217; with any of:<\/p>\n<ul>\n<li>Administrator<\/li>\n<li>Editor<\/li>\n<li>Author<\/li>\n<li>Contributor<\/li>\n<li>Subscriber<\/li>\n<li>Anyone<\/li>\n<\/ul>\n<p><strong>Misc Note:<\/strong><\/p>\n<p>As of version 1.8.8, there is a &#8216;submit_time&#8217; value (that is <strong>$row[&#8216;submit_time&#8217;]<\/strong>) which contains the submit time-stamp in the form of seconds since 1970 with miliseconds (e.g. &#8220;1308521822.8621&#8221;) which can be used for comparing the submit time to a similar unformatted value. It can also be used as a DB key value to identify all the fields of a form submission.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"_mcePaste\" class=\"mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">\n<h2>Shortcode Exec PHP<\/h2>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If the available short codes are not giving you what you want, try creating your own. You can achieve this by using a plugin that allows you to create short codes that include PHP code that you provide and using an API provided by this plugin to retrieve saved form data from the database. To [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1577,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"page-without-sidebar.php","meta":{"footnotes":""},"class_list":["post-444","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-7a","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/444","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=444"}],"version-history":[{"count":24,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/444\/revisions"}],"predecessor-version":[{"id":1366,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/444\/revisions\/1366"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1577"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}