Adding class to the comment form in Wordpress - wordpress

I want to add a class to the form, not form items. I've looked on http://codex.wordpress.org/Function_Reference/comment_form but there is no mention of adding a class to the form.

UPDATE:
Wordpress finally supports the possibility to add classes to the comments form. See Nabil Kadimi's answer to get an example.
My outdated answer:
Because Wordpress still hasn't supported this option, I've worked out the following workaround:
<?php
ob_start();
comment_form();
echo str_replace('class="comment-form"','class="comment-form your-custom-class"',ob_get_clean());
?>
Now the standard class comment-form will be replaced by itself plus the custom class.

In the documentation for the comment_form() function:
WordPress 4.4.0 Introduced the 'class_form' [...] arguments.
So you would do:
// Output the comment form with a custom class:
comment_form ( array( 'class_form' => 'my_custom_class' ) );
One a second thought
I prefer using hooks:
/**
* Callback function for the `comment_form_defaults` filter hook
*
* #param Array $defaults Defaults.
* #return Array Defaults modified.
*/
function se_8476425_modify_comment_form_defaults( $defaults ) {
$defaults[ 'class_form' ] = 'class1 class2 class3';
return $defaults;
};
add_filter( 'comment_form_defaults', 'se_8476425_modify_comment_form_defaults' );
This solution is more generic as you can use it to modify the default function behavior and themes you don't "own".

Since wordpress versión 4.1 (Dec, 2014) the comment_form function allows to specify a class attribute for the submit button.
Php Code:
$comments_args = array('class_submit' => 'btn btn-default');
comment_form($comments_args);
Resultant HTML Button code:
<input name="submit" type="submit" id="submit" class="btn btn-default" value="Submit" />
For reference see the related ticket: https://core.trac.wordpress.org/ticket/20446

You can easily modify the code of the submit button of the coment form with this filter :
function custom_submit_comment_form( $submit_button ) {
return '<input name="submit" type="submit" id="submit" class="btn btn_2" value="Laisser un commentaire" />';
}
add_filter( 'comment_form_submit_button', 'custom_submit_comment_form' );

You can just edit your single.php and wrap the :
<?php comments_template(); ?>
In a class. Something like:
<div class="myClass">
<?php comments_template(); ?>
</div>

Related

Form Block - add help text to form input

I'm trying to add a 'help text' to the questions added by the Form block.
Something like this but on the front-end part of the form.
For some reason its not saving the value or getting the value into the edit form. Even when I add it manually to the row it is not in the frontend output.
I made a custom form block / template with the additional changes to:
An extra table column 'helptext' in the db.xml. And updated the block.
<table name="btFormQuestions">
...
<field name="helptext" type="text">
<default value=""/>
</field>
...
</table>
An extra field on the editing form (normal and edit):
<div class="form-group">
<?php echo $form->label('helptext', t('Help Text'))?>
<?php echo $form->text('helptext', array('maxlength' => '255'))?>
</div>
Output it In the view.php
use \Application\Block\Form\MiniSurvey;
...
<?php echo $question['helptext']; ?>
Added the extra code to auto.js,
addQuestion:
postStr += '&helptext=' + encodeURIComponent($('#helptext' + mode).val());
...
reloadQuestion:
$('#helptextEdit').val(jsonObj.helptext);
...
resetQuestion:
$('#helptext').val('');
mini-survey.php,
$dataValues = array(
intval($values['qsID']),
trim($values['question']),
trim($values['helptext']),
$values['inputType'],
$values['options'],
intval($values['position']),
$width,
$height,
intval($values['required']),
$values['defaultDate'],
intval($values['msqID']),
);
$sql = 'UPDATE btFormQuestions SET questionSetId=?, question=?, helptext=?, inputType=?, options=?, position=?, width=?, height=?, required=?, defaultDate=? WHERE msqID=? AND bID=0';
} else {
if (!isset($values['position'])) {
$values['position'] = 1000;
}
if (!intval($values['msqID'])) {
$values['msqID'] = intval($this->db->fetchColumn("SELECT MAX(msqID) FROM btFormQuestions") + 1);
}
$dataValues = array(
$values['msqID'],
intval($values['qsID']),
trim($values['question']),
trim($values['helptext']),
$values['inputType'],
$values['options'],
intval($values['position']),
intval($values['width']),
intval($values['height']),
intval($values['required']),
$values['defaultDate'],
);
$sql = 'INSERT INTO btFormQuestions (msqID,questionSetId,question,helptext,inputType,options,position,width,height,required,defaultDate) VALUES (?,?,?,?,?,?,?,?,?,?,?)';
}
$result = $this->db->executeQuery($sql, $dataValues);
Cleared cache and updated the block.
Forum Post on Concrete5 with the changes
Solved:
Got this working on a clean install of C5.
Small changes:
Added the original files form the concrete/blocks/form to the application folder (and changed the namespace)
db.xml (longtext was unnecessary)
<field name="helptext" type="text" size="65535"></field>
auto.js (Semicolon ; missing in the original concrete/block/form/auto.js (and thus the copy in application))
reloadQuestion:
$('#editQuestionForm').css('display', 'block');

Wordpress Contact form 7, fields in template.php - not validating correctly

I need to generate CF7 fields from php to make the form dynamic.
So instead of writing <div>[text* your-name]</div> and so on directly in wordpress, I have a template file that does that instead. The problem is that when doing so, the validation is not working. The fields are displayed correctly, but no fields are checked when submitting. If submitting, the data is however saved correctly.
WP ADMIN form tab:
[my_cf_template]
template.php
<?php echo do_shortcode(apply_filters("the_content", '[contact-form-7 id="115" title="Ruumide rent - booking"]')); ?>
functions.php
function cf_template_func(){
$email = wpcf7_do_shortcode('[email* your-email]');
$submit = wpcf7_do_shortcode( '[submit "Send"]' );
$str = <<<HTML
<div class="detailed-info">
<label> Your E-mail*
$email </label>
</div>
$submit
HTML;
return $str;
}
add_action( 'wpcf7_init', 'custom_add_shortcode');
function custom_add_shortcode() {
wpcf7_add_shortcode( 'my_cf_template', 'cf_template_func');
}

I am developing plugin give following error while activation gives

The plugin generated 2 characters of unexpected output during activation. If you notice “headers already sent” messages, my code is below
<?php
/*
Plugin Name: configuration
Description: example plugin to demonstrate wordpress capatabilities
Author: kailash
License: Public Domain
*/
// run the install scripts upon plugin activation
function test_init() {
if(isset($_POST['save'])) {
$setting=$_POST['setting'];
global $wpdb;
$table_name ="setting";
$wpdb->query("UPDATE $table_name SET setting='$setting' WHERE id=1");
//echo"Setting Saved";
}
?>
<form name="form" id="myform" method="POST">
<input type="radio" name="setting" value="2" <?php if($setting==2){echo 'checked="checked"';}?> id="term"/>newconfiguration<br>
<input type="radio" name="setting" value="1" <?php if($setting==1){echo 'checked="checked"';}?> id="term"/>oldconfiguration<br>
<input type="submit" name="save" value="save">
<?php
}
add_action('admin_menu', 'config_plugin_setup_menu');
function config_plugin_setup_menu(){
add_menu_page( 'Test Plugin Page', 'configuration', 'manage_options', 'config-plugin', 'test_init' );
}
Headers already sent
It is usually because there are spaces, new lines, or other garbage before an opening <?php tag or after a closing ?>
check and remove all unnecessary space. example check before
<form> tag. means after ?> tag there is extra space in your code. so check other too and remove all space..!
Check that the very first characters are
<?php
Check that the very last characters are
?>
for details check this
https://codex.wordpress.org/Answers-Troubleshooting

I would like to diplay the output(Mickey) in a wordpress page, instead of php page

The following code is in a wordpress page.
<form action="action_page.php" method="post">
Name:<br>
<input type="text" name="name" value="Mickey">
<input type="submit" value="Submit">
</form>
action_page.php
<?php $name=$_POST['name']; echo $name;?>
I would like to diplay the output(Mickey) in a wordpress page, instead of php page. Please help me.
You can achieve it via AJAX, see the codex
Add this to functions.php
add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );
add_action( 'wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar' );
function prefix_ajax_add_foobar() {
// Handle request then generate response using WP_Ajax_Response
// In a real world scenario, apply all the validation,
// sanitization and black magic to POSTed variables before using them
$name=$_POST['name'];
echo $name;
}
Now the following could go in footer.php inside of <script> tags or in a separate JS file.
jQuery.post(
ajaxurl,
{
'action': 'add_foobar',
'data': jQuery("form").sanitize()
},
function(response){
alert('The server responded: ' + response);
}
);
All things being equal you should see an alert with the value that was posted.

Override form using PHPTemplate

I was wondering how I should override the way a form looks in Drupal 7 using PHPTemplate. I have some difficulties displaying all the information on my page.
I have a page with a form and some miscellaneous information.
I have tried:
foo.module
function foo_add_form($form, &$form_state, $foo) {
...
$form['#theme'] = 'foo_add';
return $form;
}
function foo_theme($existing, $type, $theme, $path) {
return array(
'foo_add' => array(
'template' => 'foo-add',
'render element' => 'form',
),
);
}
foo-add.tpl.php
<?php
// First form
print drupal_render_children($form);
?>
<!-- Miscellaneous information -->
<div id="links">
<ul>...</ul>
<form action="#" method="post" id="second-form">
<fieldset id="i-want">
...
</fieldset>
...
</form>
</div>
At the moment the form and miscellaneous information are being displayed. However, the 2nd form (id="second-form") is being removed somehow. I can see it in the source, but when I inspect the element with chrome/firefox, I can no longer see the form element. I can see the div, ul and fieldset tags though.
Has anybody done this before?
Typically, you wouldn't add to the form at the theme layer. Instead, you'd use a custom module, implementing hook_form_alter to modify the form.

Resources