I know there's a few solutions out there for this problem, but none of them seem to fix my code.
I've been following a lynda.com tutorial on creating plugins. However I believe they are using an older version of wordpress, which is why I think I'm running into trouble.
I'm trying to add an options page, but everytime I "save" on my options page it gives me "not found" error for the options.php page.
Tried linking to options.php directly (with full URL), no dice.
Tried changing register_setting to both equal the same thing, as stated in Wordpress Codex, but that didn't work.
Here's my code:
function cc_init(){
register_setting('cc_options,','cc_cc_email');
}add_action('admin_init','cc_init');
function cc_option_page(){
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>CC Comments Options</h2>
<p>Welcome to the CC comments plugin. here you can edit the email(s) to CC your comments to.</p>
<form action="options.php" method="post" id="cc-comments-email-options-form">
<?php settings_fields('cc_options'); ?>
<h3><label for="cc_cc_email">Eamil to send CC to:</label>
<input type="text" id="cc_cc_email" name="cc_cc_email"
value="<?php echo esc_attr(get_option('cc_cc_email')); ?>" /></h3>
<p><input type="submit" name="submit" value="Save Email" /></p>
</form>
</div>
<?php
}
function cc_plugin_menu(){
add_options_page('CC Comments Settings','CC Comments','manage_options','cc-comments-plugin','cc_option_page');
}add_action('admin_menu','cc_plugin_menu');
I think I had my add_action('admin_menu', 'cc_plugin_menu'); in the wrong spot. I moved it into the cc_plugin_menu function and it seems to save OK now.
Here's the updated code:
add_action('admin_menu', 'cc_plugin_menu');
function register_mysettings() {
register_setting( 'cc_options', 'cc_cc_email' );
}
function cc_option_page() {
?>
<div class="wrap">
<h2>CC Comments Options</h2>
<p>Welcome to the CC comments plugin. here you can edit the email(s) to CC your comments to.</p>
<form method="post" action="options.php" id="cc-comments-email-options-form">
<?php settings_fields( 'cc_options' ); ?>
<?php do_settings_sections( 'cc_options' ); ?>
<h3><label for="cc_cc_email">Eamil to send CC to:</label>
<input type="text" id="cc_cc_email" name="cc_cc_email"
value="<?php echo esc_attr(get_option('cc_cc_email')); ?>" /></h3>
<p><input type="submit" name="submit" value="Save Email" /></p>
</form>
</div>
<?php
}
function cc_plugin_menu(){
add_options_page('CC Comments Settings','CC Comments','manage_options','cc-comments-plugin','cc_option_page');
add_action( 'admin_init', 'register_mysettings' );
}
Related
I built a simple php contact form using mail(). This form has worked on my website for years.
However, I recently built a new website on wordpress. I pretty much copied and pasted the same form, however, I do not receive any email. When I hit submit, the form simply redirects to the homepage/index layout, but has the contact form url.
On the original site, however, a message is printed on a blank screen confirming the email was sent. I also tested the original code on the original site and it works.
contact_form.php:
<?php
/*
Template Name: Contact Form
`*/
include( 'header.php');
?>
<?php
if(have_posts()): while (have_posts()): the_post();
?>
<div class="wrapper">
<div class="message">
<div class="text">
<?php the_content(); ?>
<form action="contact_form_script.php" method="post">
<p>Your email address:</p> <input type="text" name="emailAddress">
<p>Subject:</p> <input type="text" name="subject">
<p>Message:</p> <textarea name="message"></textarea>
<input type="text" name="honeyPot" style="display: none;">
<br />
<input type="submit" name="email_form" value="SUBMIT"/>
</form>
<br />
<br />
</div>
</div>
</div>
<?php
endwhile;
endif;
?>
<?php
include('footer.php');
?>
contact_form_script.php
<?php
$to= 'myEmailAddress#email.com';
$from = "FROM: Email#myDomain.com";
$replyTo=$_POST['emailAddress']; /*whatever the user input*/
$headers= $from ."\r\n" .'Reply-To: '. $replyTo;
//Have also tried just $_POST['subject']; Still doesn't work
$subj="Subject: "+$_POST['subject'];
$msg=$_POST['message'];
$spam=$_POST['honeyPot'];
if (empty($spam)){
mail($to, $subj, $msg, $headers);
};
Print "Thank you for email! <br /> <br /> <a href='/'><--BACK</a>"
?>
Also, is there a better way to test a form like this to see errors or troubleshoot in some way other than just checking to see if I got the email?
There are many ways in WordPress to accomplish what you are trying to do.
Currently your form action is pointing at contact_form_script.php which assumes that this script is in the current directory. But that current directory is not relative to your contact_form.php. It is relative to the users current path. eg www.mysite.com/contact/.
One quick (but lazy) way of solving your problem would be to move contact_form_script.php into the root WordPress directory of your site. Then change the form action="/contact_form_script.php" (Note the /) causing it to post to a file called contact_form_script.php in the root site dir.
A better method would be to keep contact_form_script.php in your template directory and include it in your functions.php.
If we use the email_form submit input to let WordPress know the contact_form_script.php script needs to be loaded, this should allow you to load the whole of wordpress core and still have your custom form handling script deal with the data:
functions.php:
if (!empty($_POST['email_form'])) {
require_once(__DIR__.'/contact_form_script.php');
// You could put a die() here if you wanted the script to stop executing.
}
contact_form.php:
include( 'header.php');
if(have_posts()): while (have_posts()): the_post();
<div class="wrapper">
<div class="message">
<div class="text">
<?php the_content(); ?>
<form action="" method="post"> <!-- Notice we are now submitting our data to wordpress and not directly to our form script -->
<p>Your email address:</p> <input type="text" name="emailAddress">
<p>Subject:</p> <input type="text" name="subject">
<p>Message:</p> <textarea name="message"></textarea>
<input type="text" name="honeyPot" style="display: none;">
<br />
<input type="submit" name="email_form" value="SUBMIT"/>
</form>
<br />
<br />
</div>
</div>
</div>
Again this isn't ideal, but I suspect is what you are looking for at this point in time, and it is a step in the right direction. Keeping your code within your template.
Change the following
mail($to, $subj, $msg, $headers);
Into
wp_mail($to, $subj, $msg, $headers);
This will let WordPress properly route your mail.
Also as a whole... you should use wp_ajax to do this you can use it without writing any javascript... create a file that you include in functions with the following.
function process_contact_form() {
$to= 'myEmailAddress#email.com';
$from = "FROM: Email#myDomain.com";
$replyTo=$_POST['emailAddress']; /*whatever the user input*/
$headers= $from ."\r\n" .'Reply-To: '. $replyTo;
//Have also tried just $_POST['subject']; Still doesn't work
$subj="Subject: "+$_POST['subject'];
$msg=$_POST['message'];
$spam=$_POST['honeyPot'];
if (empty($spam)){
wp_mail($to, $subj, $msg, $headers);
};
header('Location:'.$_REQUEST['_wp_http_referer']);
wp_die();
}
add_action( 'wp_ajax_process_contact_form', 'process_contact_form' );
add_action( 'wp_ajax_nopriv_process_contact_form', 'process_contact_form' );
Change your template file to
<?php
/*
Template Name: Contact Form
`*/
include( 'header.php');
?>
<?php
if(have_posts()): while (have_posts()): the_post();
?>
<div class="wrapper">
<div class="message">
<div class="text">
<?php the_content(); ?>
<form action="<?php echo admin_url( 'admin-ajax.php' ); ?>" method="post">
<p>Your email address:</p> <input type="text" name="emailAddress">
<p>Subject:</p> <input type="text" name="subject">
<p>Message:</p> <textarea name="message"></textarea>
<?php wp_referer_field(true); ?>
<input type="hidden" name="action" value="process_contact_form">
<input type="text" name="honeyPot" style="display: none;">
<br />
<input type="submit" name="email_form" value="SUBMIT"/>
</form>
<br />
<br />
</div>
</div>
</div>
<?php
endwhile;
endif;
?>
<?php
include('footer.php');
?>
We recently faced a similar problem with our contact form. Apparently, the problem was not a result of a problem with our PHP, but rather a change in shared-hosting policies. Naturally, if you are using a VPS this would not be a problem.
However, if you have any form of shared hosting using cPanel and any form of script based email (including Contact Form 7) this can be a potential solution.
Using the "paper lantern" theme, chose the option "Registered Mail IDs" as below:
Then you should get an option to add additional email IDs. Just enter your email ID that you use in the script. In roughly around 8 hours the ID should have been propagated and you will be able to use your script.
Hope this helps! Took us a really long time to figure out.
I have a searchform.php file which contains the following code:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<div><label class="screen-reader-text" for="s">Search for</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsumit" value="Search" />
</div>
Also in my index.php I've inserted this code:
<div class="search">
<?php get_search_form(); ?>
</div>
Now When I search for something I get no results. Therefore I added a search.php file to show the results in it but I still don't get the results. What kind of changes should be made? Or what piece of code is lacking?
Note that I want to show the results in a separate page which must be search.php in Wordpress.
You need to add search.php file inside the theme and in that you will need to add the view for the search result page and the result can be obtained by looping for e.g
while(have_posts() ) : the_post();
//here is your data
endwhile;
Hey i am trying to retrive data from a basic form .. but when i am using $_POST['field name'] then it gives me nothing .
here is my basic code
form page is:
<?php
/**
Template Name: galaxy
*/
get_header(); ?>
<div id="main-content" class="main-content">
<form action="<?php echo site_url();?>?page_id=8" method="post">
<input type="text" name="name" /> <input type="submit" value="Send" />
</form>
</div><!-- #main-content -->
<?php
get_footer();
when i click submit it redirects to next page but display nothing with this code
<?php
/**
Template Name: get_value_galaxy
*/
$name=$_POST['name'];
echo $name;
print_r($_POST);
?>
Try using a different name for the variable. I know that Wordpress uses "name" as a public query var, and perhaps that's why it's not working. So rather than using name="name", try this:
Form:
<input type="text" name="unique_name" />
Post Page:
$name=$_POST['unique_name'];
echo $name;
See this list for all query vars:
http://codex.wordpress.org/WordPress_Query_Vars#Query_variables
I am new to code igniter. I've created a form but it is not display properly.
When I put
<?php echo form_open('sms'); ?> instead of <form action="">tag
here in my form and controller, I can't understand why it is not displayed.
<?php echo form_open('sms'); ?>
<p>
<label><strong>Username</strong>
<input type="text" name="textfield" class="inputText" id="textfield" />
</label>
</p>
<p>
<label><strong>Password</strong>
<input type="password" name="textfield2" class="inputText" id="textfield2" />
</label>
</p>
<input type="submit" value="Authentification" name="auth" />
<label>
<input type="checkbox" name="checkbox" id="checkbox" />
Remember me</label>
</form>
and my controller is
<?php
class sms extends CI_Controller{
function school(){
$this->load->view('school/index.php');
if($this->input->post('auth',TRUE)){
$this->load->view('school/dashboard.php');
}
else{
$this->load->view('school/index.php');
}
}
}
?>
If this is your entire script for the most part, it looks like you need to load the helper first from the CodeIgniter Form Helper Page.
If you don't have this line, try adding it before the form_open() function:
<?php $this->load->helper('form'); ?>
While I have used CodeIgniter, it's been a while. Let me know if that changes the result.
Edit: Since you've chosen my answer I'll include this one, credits go out to devo:
You could change </form> to: <?php echo form_close(); ?>. There are pros and cons for this method though, and without using arguments you might be better off sticking with </form>.
I'll explain further:
<div class="registration">
<div class="form-box">
<?php $this->load->helper( 'form' ); ?>
<?php $end = '</div></div>'; ?>
<?php echo form_open( 'register' ); ?>
<!-- Form Inputs Here -->
<?php echo form_close( $end ); ?>
<!-- Echos '</form></div></div>' -->
So for closing the form without arguments, the </form> tag works best, both by performance and simplicity. The example used above is a rather simplistic view of what you can do with it, since what I wrote is not very efficient either.
However, this is still php we're talking about, so perhaps the craftier among us could put it to better use.
End Edit
Have you loaded the form helper? You can use $this->load->helper('form'); in your controller action, her inside function school(). You can then use form helper in the view pages.
Load form helper,
$this->load->helper('form');
And use,
echo form_close()
Instead of,
</form>
First in your Controller put in:
$this->load->helper('form');
And Change </form> to :
<?php echo form_close(); ?>
I am getting the following error:
Parse error: syntax error, unexpected '<' in /home/u7mtb69/public_html/wpsitehrhhw/wp-content/themes/hrhhw.1.1/functions.php on line 1201
I am trying to add meta boxes to a custom post section in my function.php file.
The file works great, until I add the <div> container, then I get an error - but the container looks correct.
Can anyone tell me what the issue is?
// Add WHEELS meta boxes
add_action( 'add_meta_boxes' , 'wheel_meta_boxes' );
function wheel_meta_boxes() {
add_meta_box(
'wheel_info',
__( 'Wheel Info'),
'wheel_info_div',
'wheels'
);
}
function wheel_info_div( $post ) {
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'wheel_noncename' );
}
// WHEELS fields for data entry
<div>
<label for="tire_code">
<?php _e("Tire Code");?>
</label>
<input type="text" name="tire_code" value="<?php echo get_post_meta($post->ID, 'tire_code', true);?>" />
<br>
<label for="tire_name">
<?php _e("Tire Name");?>
</label>
<input type="text" name="tire_name" value="<?php echo get_post_meta($post->ID, 'tire_name', true);?>" />
<br>
<label for="tire_bname">
<?php _e("Tire Brand");?>
</label>
<input type="text" name="tire_bname" value="<?php echo get_post_meta($post->ID, 'tire_bname', true);?>" />
<br>
<input type="button" value="Create" id="create" />
<input type="button" value="Replace" id="replace" />
</div>
// NEXT
// END
?>
The problem is that you put HTML inside a PHP block. Don't do that.
You must remember that PHP and HTML are completely distinct. There is no relationship whatsoever, other than that the HTML your browser sees is the result of a PHP interpreter running your PHP script (which may include some hard-coded HTML to keep in the output, like your <div>). So when you see a PHP error, it's not going to be HTML at fault.
Wrong:
<?php
$somePHPCodeHere = 3;
<p>Some HTML here.</p>
?>
Right:
<?php
$somePHPCodeHere = 3;
?>
<p>Some HTML here.</p>
In other words: You just need to add the closing php tag ?> right after this line:
// WHEELS fields for data entry
?>