I have a Web Page that contains 1 to many tabs. Each tab has a dynamically created Form. So if there are 4 tabs, I generate 4 forms dynamically in my controller in a foreach loop where I pass the $Id like so:
foreach($ids as $id) {
$html .= $this->createHtml($id);
}
To the following function
private function createHtml($Id)
{
$html .= '<div id="product_'.$Id.'">';
$html .= ' <form id="form_'.$Id.'" name="form_'.$Id.'" class="formatted" method="POST">
<input type="hidden" name="_method" value="PUT">
<fieldset class="border">
{{ form_row(product_form_'.$Id.'.product, {
\'note\' : \'Enter Something\'
}) }}
</fieldset>
</form>';
$html .= ' <br/>';
$html .= '
<div>
Some text
</div>';
$html .= '</div>';
return $html;
}
So that function is called 4 times and keeps adding to $html.
I then pass $html and the 4 product_form->createView() to the twig and output the html like so:
{{html | raw}}
All the html div and form tags are rendered correctly but instead of rendering an input field for product, this gets displayed:
{{ form_row(product_form_'.$Id.'.product, { \'note\' : \'Enter Something\' }) }}
You see? It doesn't recognize that as a symfony input field to render. It thinks it's regular text.
I thought the | raw might have something to do with it so I adjusted my code to display {{form_row... }} without the | raw but that had no affect.
Related
I'm using a contact form 7 for the contact forms. I have a page for a doctor for example, and it has it's own form on the same page. How can I display the title of that page into the input field or select field on the contact form 7?
I did some research for dynamic data on contact form 7 and I found this https://wordpress.org/plugins/contact-form-7-dynamic-text-extension/
I put the following code into the form but it's not retrieving the page title:
[dynamictext dynamicname “CF7_get_post_var key=’title'”]
Any help is very much appreciated.
To use shortcode inside the wpcf7 forms, an easy way is "Berenis" solution, but with an important change.
Instead of add_shortcode( 'cf7_extra_fields', 'cf7_extra_fields_func' ); should be wpcf7_add_shortcode( 'cf7_extra_fields', 'cf7_extra_fields_func', true );.
Remaining code can be same, on functions.php:
wpcf7_add_shortcode( 'cf7_extra_fields', 'cf7_extra_fields_func', true );
function cf7_extra_fields_func( $atts ) {
$html = '';
$html .= '<input type="hidden" name="page-title" value="'.get_the_title().'" />';
$html .= '<input type="hidden" name="page-url" value="'.get_the_permalink().'" />';
return $html;
}
On contact form, use: [cf7_extra_fields], on email fields use: [page-title] and [page-url].
This worked for me, without any issues.
a year late, but I just saw it. I suppose you have already solved it. The error is because the shortcode has wrong characters. The correct thing is like this:
[dynamictext dynamicname "CF7_get_post_var key='title'"]
Regards!
Put this to functions.php
add_shortcode( 'cf7_extra_fields', 'cf7_extra_fields_func' );
function cf7_extra_fields_func( $atts ) {
$html = '';
$html .= '<input type="hidden" name="page-title" value="<'.get_the_title().'">';
$html .= '<input type="hidden" name="page-url" value="<'.get_the_permalink().'">';
return $html;
}
Then when editing contact form add this shortcode inside [cf7_extra_fields]
When passing form fields to email use [page-title] and [page-url]
No plugin needed
Did you try with jquery/js ? like jQuery("#field_id").val("<?php echo get_the_title(); ?>"); . you can try this code in the page where the form is using.
I created custom post type named Emails, and added a custom field using advanced custom fields plugin to one post inside the custom post type called email footer, the field is image field that is supposed to show at the bottom of each automatic email going out of the website.
the current code I'm using
function wpcf7ev_verify_email_address2( $wpcf7_form ){
$email_footer = '<html>
<body style="color:#000000;">
<div style="font-size:16px;font-weight:bold;margin-top:20px;">
Regards,
<br/>
$email_footer .= '<img src="http://mysite.col/footer_image.jpg" width="100%" alt=""/>
</div>';
$email_footer .='<div style="display:none;">'.generateRandomString().
'</div></body>
</html>
';
the code is working, it displays the image with this url at the bottom: http://mysite.col/footer_image.jpg
but I don't want hardcoded, I want to be able to modify it with the custom field I created
I looked at ACF documentation and found this, but I don't know how to use it to still show that exact field on custom post type I created:
<?php
$image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
The code you've outlined from the ACF documentation tells you how to get the image from an ACF field using Image (with type array).
If we were to implement this into your function, we'd have to reference the image from the page somewhere. Without knowing how you're calling this there are a couple of ways you could embed it.
The first way, we pass it through to the function called on the page, like so...
wpcf7ev_verify_email_address2(get_field('image'));
and then update your function like so...
function wpcf7ev_verify_email_address2($image, $wpcf7_form)
{
$email_footer = '<div style="font-size:16px;font-weight:bold;margin-top:20px;">Regards,<br/>';
// get the image from the passed in image function.
$email_footer .= '<img src="' . $image['url'] . '" width="100%" alt="' . $image['alt'] . '"/></div>';
$email_footer .='<div style="display:none;">' . generateRandomString() . '</div>';
}
Or, the second way, if you are calling the function to modify an action or something, you'd have to get the image from whichever page ID / options page it is assigned to in your ACVF settings. This would make your function look a little like this:
function wpcf7ev_verify_email_address2($wpcf7_form)
{
// get image acf field from page with id 1
$image = get_field('image', 1);
// or get image from acf field on options page
// $image = get_field('image', 'options');
$email_footer = '<div style="font-size:16px;font-weight:bold;margin-top:20px;">Regards,<br/>';
$email_footer .= '<img src="' . $image['url'] . '" width="100%" alt="' . $image['alt'] . '"/></div>';
$email_footer .='<div style="display:none;">' . generateRandomString() . '</div>';
}
All of the above is presuming that your function is working as intended, with you needing help grabbing the ACF field, and the image is uploaded. You can wrap your declarations of get_field in if statements if required.
Right now I have a front end form that lists 4 separate taxonomies in checkbox inputs that a user selects. I can list all of the terms one after the other but I can't figure out out to list them with proper hierarchy like so:
-parent
-child
-child
-child
-parent
-child
-child
etc.
Right now my code to output to the form is
$closed_schools = get_terms('closed_schools', 'orderby=id&order=ASC&hide_empty=0&get=all');
$counter = 0;
foreach ($closed_schools as $close) {
$counter++;
$option = '<fieldset id="'.$close->slug.'"><label for="'.$close->slug.'">'.$close->name.'</label>';
$option .= '<input type="checkbox" name="terms[]" id="'.$close->slug.'" value="'.$close->slug.'">';
$option .= '</fieldset>';
echo $option;
}
I have figured out a solution to my problem. It may not be the most elegant solution but I am able to achieve the layout I was looking for. Instead of get=all I replace that with the parent categories id so it only lists the children of that specific id. Since they are in a checkbox array I then perform several loops under each parent to separate the checkbox's each time just adding a new number $closed_schools_1, $closed_schools_2 and so on. would love to here from anyone who may have an easier less repetitive solution but for time being this will work.
<?php
$closed_schools = get_terms('closed_schools', 'orderby=id&order=ASC&hide_empty=0&child_of=35');
$counter = 0;
foreach ($closed_schools as $closed_school) {
$counter++;
$option = '<fieldset id="'.$closed_school->slug.'">
<label for="'.$closed_school->slug.'">'.$closed_school->name.'</label>';
$option .= '<input type="checkbox" name="closed_school_terms[]" id="'.$closed_school->slug.'" value="'.$closed_school->slug.'">';
$option .= '</fieldset>';
echo $option;
}
?>
<?php
$closed_schools_2 = get_terms('closed_schools', 'orderby=id&order=ASC&hide_empty=0&child_of=55');
$counter = 0;
foreach ($closed_schools_2 as $closed_school_2) {
$counter++;
$option_2 = '<fieldset id="'.$closed_school_2->slug.'">
<label for="'.$closed_school_2->slug.'">'.$closed_school_#->name.'</label>';
$option_2 .= '<input type="checkbox" name="closed_school_terms[]" id="'.$closed_school_2->slug.'" value="'.$closed_school_2->slug.'">';
$option_2 .= '</fieldset>';
echo $option_2;
}
?>
I have Wordpress installed and I have this form on each category page sidebar:
<form name="tags" onChange="document.forms.tags.submit();">
<input type="checkbox" name="tag" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>>tag1<br>
<input type="checkbox" name="tag" value="tag2" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag2") { echo "checked";}?>>tag2
</form>
The goal is to create an url like /?tag=tag1+tag2 and so on.
With the above code I get the url like this: /?tag=tag1&tag=tag2.
I've searched for two weeks and tried a lot, but nothing works for me. I've tried for example
<input type="checkbox" name="tag[]" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>>tag1
but then i get ?tag%5B%5D=tag1 and Wordpress don't find any results.
The form submit each time a checkbox is checked.
If I use radio input fields, then it works great, because then there's one value each time, but I want to pass multiple values with the same name and render a url like /?tag=tag1+tag2+tag3+tag4 etc.
Can anybody help me with this problem, because I don't know how to get this work for me. Thanks!
this is what you want:
// This would output '/client/?s=word&foo=bar'
echo add_query_arg( 'foo', 'bar' );
take a look on add_query_arg() and get_query_var()
Consider on this example http://codepad.org/s08Jmseg
$url_string = $_SERVER['QUERY_STRING'];
// reuturn: tag=tag1+tag2+tag3
parse_str($url_string);
// $tag holds the value tag1+tag2+tag3
$string = urldecode( $tag );
$arr = explode(" ", $string);
$my_tag = "tag2";
if( in_array( $my_tag, $arr ) ) {
// $key
echo $my_tag . ' found!';
}
else {
echo "Tag Not Found!";
}
//print_r($arr);
I am trying to get field_1 value into the input text and I tried this code but it doesn't work.
function bbg_add_reg_field() {
$field_1= $_POST["signup_username"];
echo '<input name="field_1" id="field_1" value="$field_1" type="text">';
}
add_action( 'bp_before_registration_submit_buttons', 'bbg_add_reg_field' );
The echo value shown inside the input text is $field_1
The value should be a name, say for example peter.
In PHP you can interpolate a string (add variables to a string with $var) only when it is a double quoted string.
From the PHP Documentation
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
Instead you could do this
echo '<input name="field_1" id="field_1" value="' . $field_1 . '" type="text">';
oh and don't forget htmlspecialchars
echo '<input name="field_1" id="field_1" value="' . htmlspecialchars($field_1) . '" type="text">';
You could also achieve the same using PHP Heredoc. Which goes like this
echo <<<EOF
<input name="field_1" id="field_1" value="$field_1" type="text">
EOF;