Format posted data in contact form 7 array - wordpress

I created a contact form 7 tag called [pureair] in which I display a calculator.
The user can add rooms and windows to calculate the needed values.
The form is located inside a Wordpress plugin boilerplate partial
<div id="depcore-pureair-caclulator" class='depcore-pureair-caclulator'>
<section class="room" data-room-number='1'>
<h3 class="room-title"><?= __('Room', 'depcore-pureair') ?></h3>
<div class="room-fields">
<p class="form-field"><label for="room-height-1"><?= __('Height', 'depcore-pureair') ?></label><input type="number" class='room' name="pureair[][room-height-1]" id="room-height-1" min=1 step=1> cm</p>
<p class="form-field"><label for="room-width-1"><?= __('Width', 'depcore-pureair') ?></label><input type="number" class='room' name="pureair[][room-width-1]" id="room-width-1" min=1 step=1> cm</p>
<p class="form-field"><label for="room-length-1"><?= __('Length', 'depcore-pureair') ?></label><input type="number" class='room' name="pureair[][room-length-1]" id="room-length-1" min=1 step=1> cm</p>
<p class="calculation-result"><label><?= __('Volume', 'depcore-pureair') ?></label> <span></span>m<sup>3</sup></p>
</div>
<div class="windows">
<h3 class="window-title"><?= __('Glass', 'depcore-pureair') ?></h3>
<div class="window" data-window-number="1">
<p class="form-field"><label for="room-1-window-1-height"><?= __('Height', 'depcore-pureair') ?></label><input class='window' type="number" name="pureair[][room-1-window-1-height]" id="room-1-window-1-height" min=1 step=1> cm</p>
<p class="form-field"><label for="room-1-window-1-width"><?= __('Width', 'depcore-pureair') ?></label><input class='window' type="number" name="pureair[][room-1-window-1-width]" id="room-1-window-1-width" min=1 step=1> cm</p>
<p class="window-calculation-result"><label><?= __('Surface area', 'depcore-pureiar') ?></label><span></span>m<sup>2</sup></p>
<div class="window-actions">
<div class="remove-window depcore-remove-button"><svg viewBox='0 0 30 29'>
<use xlink:href='#minus-icon'></use>
</svg><span><?= __('Remove window', 'depcore-pureair') ?></span></div>
<div class="add-window depcore-add-button"><svg viewBox='0 0 30 29'>
<use xlink:href='#plus-icon'></use>
</svg><span><?= __('Add window', 'depcore-pureair') ?></span></div>
</div>
</div>
</div>
<div class="depcore-pureair-warning">
<p><?= __('This area is too small to effectively clear the room. Add a window', 'depcore-pureiar') ?></p>
</div>
<div class="room-actions">
<div class="add-room depcore-add-button"><svg viewBox='0 0 28 29'>
<use xlink:href='#plus-icon'></use>
</svg><span><?= __('Add room', 'depcore-pureair') ?></span></div>
<div class="remove-room depcore-remove-button"><svg viewBox='0 0 28 29'>
<use xlink:href='#minus-icon'></use>
</svg><span><?= __('Remove room', 'depcore-pureair') ?></span></div>
</div>
</section>
</div>
I'm using the filter $this->loader>add_filter('wpcf7_special_mail_tags', $plugin_admin, 'calculator_wpcf7_pureair_mail_tag', 10, 3 ); to display the fields inside the email
public function calculator_wpcf7_pureair_mail_tag($output, $name, $html){
$name = preg_replace('/^wpcf7\./', '_', $name); // for back-compat
$submission = WPCF7_Submission::get_instance();
if (! $submission) {
return $output;
}
if ('pureair' == $name) {
return $submission->get_posted_data("pureair");
}
return $output;
}
The problem is that the values in the email are displayed as a coma separated string (for example 270,200,300).
I've tried to use the $this->loader>add_filter('wpcf7_posted_data', $plugin_admin, 'calculator_wpcf7_posted_data'); filter but then all the values are removed.
What I would like to achieve is to loop through the array and create a formatted result inside email message with the data. For example
Room 1 height: 270cm, width: 200cm, length: 400cm
Windows:
1: height: 90cm, width: 110cm
Cost: xxx
Room 2 ...
I've searched but cannot find ho to get the values as array inside the filter.
After some digging into the suggestion from Howard E. I've tried to use the wpcf7_before_send_mail as follows just to test if I can change the values
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$posted_data = $submission->get_posted_data();
foreach ($posted_data as $key => $value) {
if($key == 'pureair') $posted_data['pureair'] = "<table><thead><tr><th>test</th></thead></table>";
if($key == "your-name") $posted_data['your-name'] = "blabla#pl.pl";
}
}
But this doesn't work. Any idea why?

One way to achieve a similar result if to use the Smart Grid-layout extension. It allows you to build a repetitive tabbed form sections as well as repetitive table of fields construct (see this online tutorial on how to achieve this).
Your form would have a repetitive tabbed section of rooms, 1 tab per room, with the fields height/length/width, as well as a table construct within each tab for windows. The table would have window-height/window-width in each row, allowing a user to add multiple windows to each room. In addition each tab could also have a read-only room-cost field.
Your users would then be able to add several tabs of rooms, and in each tab the dimensions of the room as well as multiple rows of windows. Each time a new row is added to the table construct by the user, an event is fired on the table, allowing you to display a calculated value in your room-cost field based on the added rooms/windows.
When the form is submitted, the table fields are submitted as an array of rooms, and an array of arrays of windows. You can filter the way these fields are displayed in the notification email (see this tutorial) as an HTML <table/>, which is the only way to display tables structures in HTML mail body, something like this
add_filter( 'cf7sg_mailtag_pureair', 'filter_cf7_mailtag_pureair', 10, 3);
function filter_cf7_mailtag_pureair($tag_replace, $submitted, $cf7_key){
//$submitted an array of submitted fields
if('my-pureair-form'==$cf7_key ){
$tag_replace = '<table>'.PHP_EOL;
foreach($submitted['room-height'] as $idx=>$height){ //tables_to_repair.
$tag_replace .= '<tr><td>Room '.($idx+1).'</td>';
$tag_replace .= '<td>height: '.$height.'cm, width: '.$submitted['room-width'][$idx].'cm, length: '.$submitted['room-length'][$idx].'cm </td>';
foreach($submitted['window-height'][$idx] as $rdx=>$wheight){
$tag_replace .= '<td>Windows: '.($rdx+1).': height: '.$wheight.'cm, width: '.$submitted['window-width'][$idx][$rdx].'cm </td>';
}
$tag_replace .= 'Cost: '.$submitted['room-cost'].'USD</tr>'.PHP_EOL;
}
$tag_replace .= '</table>'.PHP_EOL;
}
return $tag_replace;
}

Related

ACF into shortcode

Hello good people of StackOverflow, I was wondering is there any way I can use advanced custom fields in shortcode?
function highlight($atts) {
return '
<div class="col-lg-6 ">
<div class="highlighted">
<p class="page-title">TEST</p>
</div>
</div>';
}
add_shortcode('scbox', 'highlight');
so I would like to put something like {{ the_field('text') }} where the "TEST" is now, I'm using blade template if its of any help
You can use the get_field() function to obtain the value that you are looking for. (get_field() will return the value, whereas the_field() will print the value wherever invoked.)
You can then concatenate the value into your returned string:
function highlight($atts) {
$text = get_field('text');
return '
<div class="col-lg-6 ">
<div class="highlighted">
<p class="page-title">' . $text . '</p>
</div>
</div>';
}

In wpgeodirectory add event how to make mandatory the field to connect event with place (business link)

The business link (Place) is not mandatory when inserting a new event. How to make this mandatory. Every event should be connected with a place !
Visuals
Add class "required_field" and * in the respective Business Link row by replacing this at plugins\geodir_event_manager\gdevents_template_functions.php line 1035
<div id="geodir_link_business_row" class="geodir_form_row clearfix">
<label>
<?php _e( 'Link Business', 'geodirevents' );?>
</label>
with
<div id="geodir_link_business_row" class="required_field geodir_form_row clearfix">
<label>
<?php _e( 'Link Business', 'geodirevents' );?>
<span>*</span>
</label>
in the same function replace also
<input type="button" id="geodir_link_business_autofill" class="geodir_button button-primary" value="<?php echo EVENT_FILL_IN_BUSINESS_DETAILS; style="float:none;margin-left:30%;" ?>"
</div>
with this
<input type="button" id="geodir_link_business_autofill" class="geodir_button button-primary" value="<?php echo EVENT_FILL_IN_BUSINESS_DETAILS; ?>" style="float:none;margin-left:30%;" />
<span class="geodir_message_note"><?php _e('Basic HTML tags are allowed', 'geodirevents');?></span>
<span class="geodir_message_error"> <?php _e("This field is required","gdevents") ?> </span>
</div>
Functional: prevent form from submit
a. deactivate listing_validation.min.js and enable listing_validation.js in
\plugins\geodirectory\geodirectory_template_tags.php line 198
i.e. change this
wp_enqueue_script('geodirectory-listing-validation-script', geodir_plugin_url() . '/geodirectory-assets/js/listing_validation.min.js#asyncload');
to this
wp_enqueue_script('geodirectory-listing-validation-script', geodir_plugin_url() . '/geodirectory-assets/js/listing_validation.js#asyncload');
b. add the check in plugins\geodirectory\geodirectory-assets\js\listing_validation.jsline 159 (validate_form())
if (jQuery('#geodir_link_business')[0].selectedIndex == 0) {
jQuery('#geodir_link_business').closest('.required_field').find('.geodir_message_error')[0].style.display = "inline";
is_validate = false;
} else {
jQuery('#geodir_link_business').closest('.required_field').find('.geodir_message_error')[0].style.display = "";
}

WordPress pods pagination not taking limit parameter

I’m new using the pods plugin so maybe it’s a misuse of the pagination function.
I have a simple code where I call in a custom template page a pods taxonomy, I list them and then I put the pagination. In my request I put a limit of 20 object by request, as I understand it, this parameter should be passed on to the pagination function and the display should be of 20 entries by page.
Or when navigating , I have a random number of entries depdending on the letter I’m trying to display (9,14,12,10).
I would like to be able to lock it at 20 if possible, could you help me finding what I'm doing wrong?
The code in question:
if(isset($_GET["let"])){
$let = $_GET["let"];
$params = array(
"limit" => 20, // -1 Return all rows
"where" => "t.name LIKE '".$let."%'"
);
}else{
$let = "all";
$params = array(
'limit' => 20 // -1 Return all rows
);
}
$adherents = pods("adherent",$params);
echo "<div class='row'>
<div class='col-lg-5' id='cibe-adherent-title'>
<h1>Nos Adhérents</h1>
</div>
</div>
<div class='row' id='cibe-annuaire-tri'>
<div class='col-lg-12 text-lg-center'>
<a href='".get_permalink()."'";
if($let == "all") echo " class='cibe-active-link'";
echo ">Tous</a> ";
for ($i=65; $i<=90; $i++) {
echo "<a href='".get_permalink()."/annuaire-des-adherents/?let=".chr($i)."'";
if($let == chr($i)) echo " class='cibe-active-link'";
echo ">".chr($i)."</a> ";
}
echo " </div>
</div>";
while($adherents->fetch()){
$current_url = add_query_arg('term_id', $adherents->field("term_id"), get_permalink());
if($adherents->field("adherent") == 1)
echo "<div class='row'>
<div class='col-lg-11 offset-lg-1'>
<a href='".$current_url."'>".$adherents->field("name")."</a>
</div>
</div>";
}
echo "<div class='row' id='cibe-annuaire-pagination'>
<div class='col-lg-12 text-lg-center'>
".$adherents->pagination()."
</div>
</div>";
As I said in my post, it may be a misuse, and it is! Thinking on my code, I have this condition
if($adherents->field("adherent") == 1)
That is why some are not displaying.
I did this at the beginning when I didn't know you could use where condition in the pods parameters.
If I correct this it should work.

How do I filter out certain results based on the value of a faceted Attribute with Algolia Search for Wordpress?

I'm trying to filter out some results in an Algolia faceted search within Wordpress. I want to show only certain results to a user logged in with a specific WP user role. Here is what I have in my results now and it's not returning any result, but the pagination for the search does show up, so I know the script is running without errors. I also do not have any console errors.
This is my current script from instantsearch.php:
<script type="text/html" id="tmpl-instantsearch-hit">
<article itemtype="http://schema.org/Article">
<?php
// Get data
$post_title = '{{{ data._highlightResult.post_title.value }}}';
$aim_of_work = '{{{ data._snippetResult.aim_of_organisations_work.value }}}';
$organisation_region = '{{{ data._highlightResult.organisation_region.value }}}';
?>
<?php
// If user is limited to Americas and the Carribbean
if( in_array('americas', $user_info->roles) ) {
if($organisation_region == 'Americas and the Caribbean') { ?>
<!-- Print Americas Results -->
<div class="ais-hits--content">
<h3 itemprop="name headline"><?php echo $post_title; ?></h3>
<div class="ais-hits--tags">
<# for (var index in data.taxonomies.post_tag) { #>
<span class="ais-hits--tag">{{{ data._highlightResult.taxonomies.post_tag[index].value }}}</span>
<# } #>
</div>
<div class="excerpt">
<p>
<?php echo $aim_of_work; ?>...
</p>
<p class="text-small">Region: <?php echo $organisation_region; ?></p>
</div>
</div>
<div class="ais-clearfix"></div>
<?php } //END if
} else { ?>
<!-- Print All Results -->
<div class="ais-hits--content">
<h3 itemprop="name headline"><?php echo $post_title; ?></h3>
<div class="ais-hits--tags">
<# for (var index in data.taxonomies.post_tag) { #>
<span class="ais-hits--tag">{{{ data._highlightResult.taxonomies.post_tag[index].value }}}</span>
<# } #>
</div>
<div class="excerpt">
<p>
<?php echo $aim_of_work; ?>...
</p>
<p class="text-small">Region: <?php echo $organisation_region; ?></p>
</div>
</div>
<div class="ais-clearfix"></div>
<?php } // END if ?>
</article>
</script>
My concern is that my conditional is not working: if($organisation_region == 'Americas and the Caribbean')
I feel like there is a better way to do this, but I would take any way that works right now.
/**-- UPDATE --*/
Here is my facet widget:
/* Region refinement widget */
search.addWidget(
instantsearch.widgets.menu({
container: '#facet-org-region',
attributeName: 'organisation_region',
sortBy: ['isRefined:desc', 'count:desc', 'name:asc'],
limit: 10,
templates: {
header: '<h3 class="widgettitle">Region</h3>'
}
})
);
Your solution seems to mix JavaScript code with PHP.
PHP code gets parsed and executed on the server side and JavaScript on the client side in the browser.
In your example, $organisation_region will always equal the string '{{{ data._highlightResult.organisation_region.value }}}'.
You probably want to add organisation_region as a facet and then refine on that.
To achieve that, you can take a look how other facets are implemented here https://community.algolia.com/wordpress/customize-search-page.html
Also, here is how to register your custom facet: https://community.algolia.com/wordpress/indexing-settings.html#register-custom-facet

PHP Keeps Duplicating Table Elements in WordPress Theme

I'm working on this test site, which is a WordPress website (using version 5.5) and using the Canvas WooTheme. The front page of it is supposed to display today's calendar events, which are provided by the WordPress plugin, Events Manager.
What I would like for them to do is display, such as this events page (and the screenshot below - seen in this screenshot), does. Instead, the code below somehow has the rest of the template (including the sidebar) sucked into the table, continuing to duplicate table rows and columns into itself (as seen from this screenshot).
The code it shoots out into Firebug is the following:
<table cellspacing="0" cellpadding="0" id="current-events"><tbody><tr><td class="startdate_cost_td">
<span class="start_time"><table cellspacing="0" cellpadding="0" id="current-events"><tbody><tr>
<td class="startdate_cost_td">
<span class="start_time">12:00 AM</span><br>
<span class="cost">Free</span>
</td>
<td class="event_image"> <img src="../../../wp-includes/images/default_img.jpg"></td>
<td class="event_title">Sex Addicts Anonymous meeting<br>
<i><span class="location_name"># No location listed</span></i>
<div id="read_more_wrapper">
<img src="../../../wp-includes/images/little_pink_arrow.jpg"><a class="pink_link" href="http://testbuzz.illinimedia.com/events/sex-addicts-anonymous-meeting-90">READ MORE</a></div></td></tr>
<!--12:00 AM</span><br/><span class='cost'>Free</span></td><td class='event_image'><img src='../../../wp-includes/images/default_img.jpg' /></td><td class='event_title'>Sex Addicts Anonymous meeting<br/><i><span class='location_name'>#No location listed</span></i><br />Mind, body, & spirit<div id='read_more_wrapper'><img src='../../../wp-includes/images/little_pink_arrow.jpg' /><a href='http://testbuzz.illinimedia.com/events/sex-addicts-anonymous-meeting-90' class='pink_link'>READ MORE</a></td></tr></div><tr><td class='startdate_cost_td'>
<span class='start_time'>-->
<tr>
<td class="startdate_cost_td">
<span class="start_time">8:30 AM</span><br>
<span class="cost">Free</span>
</td>
<td class="event_image"><img alt=""Inside India" with Larry Kanfer" src="http://testbuzz.illinimedia.com/wp-content/uploads/locations-pics/event-35214.jpg"> </td>
<td class="event_title">“Inside India” with Larry Kanfer<br>
<i><span class="location_name"># Alice Campbell Alumni Center</span></i>
<div id="read_more_wrapper">
<img src="../../../wp-includes/images/little_pink_arrow.jpg"><a class="pink_link" href="http://testbuzz.illinimedia.com/events/inside-india-with-larry-kanfer-31">READ MORE</a></div></td></tr>
......more events similar to this......
<div class="fix"></div>
<div class="nav-entries">
<span class="nav-prev icon fl">Older posts</span> <div class="fix"></div>
</div>
<!-- /#main -->
<div id="sidebar">
<div class="widget widget_woo_search" id="woo_search-3"><div class="search_main">
<form action="http://testbuzz.illinimedia.com/" class="searchform" method="get">
<input type="text" onblur="if (this.value == '') {this.value = 'Search...';}" onfocus="if (this.value == 'Search...') {this.value = '';}" value="Search..." name="s" class="field s">
<input type="image" name="submit" class="submit" alt="Search" src="http://testbuzz.illinimedia.com/wp-content/themes/canvas/images/ico-search.png">
</form>
<div class="fix"></div>
</div>
.... the rest of the content stuck .....
</span></td></tr></tbody></table>
Below is the code I stuck into our magazine template within our child theme, template-magazine.php, which is causing the problem:
<?php
$page = ( !empty($_REQUEST['page']) && is_numeric($_REQUEST['page']) )? $_REQUEST['page'] : 1;
$args = array(
'limit' => 10,
'scope' => 'today',
'pagination' => 1
);
if (isset($_REQUEST['c'])) {
$args['category'] = $_REQUEST['c'];
$args['scope'] = 'future';
}
$args['offset'] = $args['limit'] * ($page-1);
$events = em_get_events_array($args);
$size = sizeof($events)-1;
echo "<h2 id='headding'class='title'>Today's Events</h2>";
echo "<table cellpadding='0' cellspacing='0' id='current-events' >";
for ($i=0;$i<$size;$i++) {
echo "<tr><td class='startdate_cost_td'>
<span class='start_time'>" . $events[$i][0] .
"</span><br/><span class='cost'>" . $events[$i][7] .
"</span></td><td class='event_image'>";
if ($events[$i][8] == '') {
echo "<img src='../../../wp-includes/images/default_img.jpg' />";
}
else {
echo $events[$i][9];
}
echo "</td><td class='event_title'>" . $events[$i][10] .
"<br/><i><span class='location_name'>#". $events[$i][11] .
"</span></i><br />" .$events[$i][12] .
"<div id='read_more_wrapper'>" .
"<img src='../../../wp-includes/images/little_pink_arrow.jpg' /><a href='" . $events[$i][13] . "' class='pink_link'>READ MORE</a></td></tr>" .
"</div>";
}
echo "</table>";
$last_index = count($events) - 1;
echo $events[$last_index][0];
?>
I made sure the PHP code above was the cause of the main problems, by removing the code itself. That seems to do the trick! When I tried placing this code in different parts of the template-magazine.php file, it repeated the same results. When I also moved this to the functions.php file in our child theme, it simply made everything else go further down and expand to the whole width of the browser. How do I get rid of the looping in the code, having all of the side content being sucked into the table being created?
UPDATE: I commented out the above PHP code using HTML tags, duplicating the events and commenting out the table elements, which we would like to have show on the page. Below is the screenshot of what it looks like: https://docs.google.com/open?id=0B2UC67NQiW2yalhMSGxra0FFWGM and the code that's showing up in the picture below (I didn't want to paste in the code because it would've been very long:
It ended up being that it seemed to be reading in the Event Preview settings, which I created within the plugin. All I needed to do was simply create a for loop, reading in each of the events for that specific page, blocking out the rest of the PHP code above. The following is the code used to read in the events:
for ($i=0;$i<$size;$i++) {
echo $events[$i][0];
}
Hopefully this helps someone using the Events Manager plugin.

Resources