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>';
}
Related
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;
}
Update: I am creating long running background task in Symfony framework using Process functionality. As part of that:
- User will complete the form and click submit button
- This will start the background process using below script
$rootDir = $this->get('kernel')->getRootDir();
$adk_process = new Process('php ../bin/console app:adkaction ' . $numcampaigns . ', ' . $timezone . ',' . $depdate);
$adk_process->setWorkingDirectory($rootDir);
$adk_process->setTimeout(null);
$adk_process->start();
In order for user to be able to go do other things, we close current session and want to render specific twig tempalate and pass specific variable to it using code below
$ignore_user_abort(true);
$strURL = $rootDir;
header("Location: " . $strURL, false);
header("Connection: close", true);
header("Content-Encoding: none");
header("Content-Length: 0", true);
flush();
ob_flush();
session_write_close();
This works fine however I cannot seem to find the way how to pass a variable this way. My Twig Template is like this
{% if currprogress is defined %}
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="x_panel">
<div class="x_title">
<div class="progress">
<div class="progress-bar progress-bar-striped active"
role="progressbar" aria-valuenow={{currprogress}}
aria-valuemin="0" aria-valuemax="100" style="width:{{currprogress}}%">
Campaign Generation - {{currprogress}}%
</div>
</div>
</div>
</div>
</div>
{% endif %}
So once currprogress variable value is passed to Twig, it would slightly change. Any idea on how to do it using header + link.
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
I am tryign to make my theme translatable so I am using the following command to output text. But its not outputting a default value i thought the __ did that automartically.
<?php __('PLAYER POINTS AT A GLANCE.','gogreensoccer');?>
I am using the above to display a translatable string to wordpress buts its empty.
<div class="skill-title">
<h3><?php __('PLAYER POINTS AT A GLANCE.','gogreensoccer');?></h3>
</div>
<div class="col-md-5 col-sm-12">
<div class="kids-dashboard-skill">
<div class="skill-show">
<div class="points"><h3><span><?php echo $player->display( 'points' ); ?></span>POINTS</h3></div>
<div class="circle-skill"><div id="circle" data-size="<?php echo $player->display( 'points' ); ?>" data-thickness="35"></div></div>
</div>
<div class="skill-button">
<center>
<button><?php __('VIEW MY TEAM MATES.','gogreensoccer');?></button>
<button><?php __('Player ID','gogreensoccer');?><?php echo $playerId;?></button>
</center>
</div>
</div>
But im not getting any text outputed obv I want a default value here if no translation exists i though __(string,themename) would achieve this.
You don't use the right function: __() returns the text or the translation if it exists. If you want to display it, you must use echo like this: echo __('some text', 'textdomain').
As an alternative, if you just want to print the text, you can use the _e() function which will echo the text (the one in parentheses or the translation if it exists). You can use it like this for example: _('some text', 'textdomain').
Use esc_html_e() function to escape your translatable text and output it.
_e() is a shorthand notation for echo __(), while __() just returns the translated string (not echoing it out).
Anyways, the best practice is to escape all things.
I wanted to create specially styled columns, but make it easy for the client to still edit the content of the column.. so I created my own shortcodes to setup the row and columns inside. (This is a custom wordpress template I created for a specific client).
Here is the code in my function.php for the two shortcodes 'member-row' & 'member':
add_shortcode('member-row', function ($content = null) {
return '<div class="row">
<br />'.do_shortcode($content).'</div>';
});
add_shortcode('member', function ($atts, $content = null) {
extract(shortcode_atts(array(
'color' => 'white',
), $atts));
return '<div class="col-md-6 col-sm-6 col-xs-12 md-margin-bottom-40">
<div class="member-col funny-boxes funny-boxes-top-'.$color.'">'.do_shortcode($content).'</div>
</div>';
});
I've also added the .do_shortcode($content). on the inner-nested 'member' column shortcode - just in case the client wants to add other shortcodes inside of each column. (Note: The problem still occurs even if I change the 'member' shortcode to just use .$content.)
Here is the code I entered in the text side of the wordpress page editor
[member-row]
[member color="gold"]
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo1.jpg" alt="logo" />
<strong>Company 1</strong>
Address
Telephone
www.example1.com
[/member][member color="yellow"]
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo2.jpg" alt="logo" />
<strong>Company 2</strong>
Address
Telephone
www.example2.com
[/member]
[/member-row]
When I save, and click on 'view page', the section where this has been added doesn't display anything.
This is the html code that is produced:
<div class="row">
<br /></div>
So it is only executing the 'member-row' shortcode, and not the nested 'member' shortcode columns.
This is the correct html code I was expecting:
<div class="row">
<br />
<div class="col-md-6 col-sm-6 col-xs-12 md-margin-bottom-40">
<div class="member-col funny-boxes funny-boxes-top-gold">
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo1.jpg" alt="logo" />
<strong>Company 1</strong>
Address
Telephone
www.example1.com
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 md-margin-bottom-40">
<div class="member-col funny-boxes funny-boxes-top-yellow">
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo2.jpg" alt="logo" />
<strong>Company 2</strong>
Address
Telephone
www.example2.com
</div>
</div>
</div>
When I just remove the surrounding [member-row] and [/member-row] shortcodes from the editor window, then the 'member' column shortcode does show up properly on the webpage.. so it appears it is just being inside of the 'member-row' shortcode that causes the problem.
Note: I did try adding the add_filter('the_content', 'do_shortcode', 10); line to the bottom of my function.php file, but it didn't seem to make any difference, so I removed it.
Hopefully I have just made some typo error.. Any help will be greatly appreciated!
Thanks.
I figured out the problem.
I had to add $atts to the function for the member-row shortcode as follows:
add_shortcode('member-row', function ($atts, $content = null) {
return '<div class="row">
<br />'.do_shortcode($content).'</div>';
});
It now works as expected!!
I am not sure why I needed to add $atts, as this shortcode is not passing attributes or setting default ones, but it appears you need it if you are going to use enclosing shortcode with $content. (Maybe it is needed because $content is defined as the 'second' parameter???)
I just read in the WP Codex for shortcode api that
"Three parameters {$atts, $content, $tag} are passed to the shortcode
callback function. You can choose to use any number of them including
none of them."
So that seems to me that you could use the $content by itself... but as I just proved that $content can't be by itself - I am still unsure of the reason that my solution worked for me. (Just glad it did!)
If anyone knows the rule or reason for why the $atts is needed to make this work, I would appreciate the comment - to clarify this for myself, and anyone else that has the same problem.
Thanks!