ERROR : adding <div> container to function.php - php

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
?>

Related

PHP Contact Form Not Working on Wordpress

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.

Search page does not show the search results in WordPress

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;

Wordpress - Error: Options Page Not Found

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' );
}

error if "else" begins with a new php block

Just curious to know why the code below gives "unexpected T_ELSE" syntax error:
<?php if (isset($_SESSION["user_id"])) { ?>
<h2>Welcome, <?php echo $_SESSION["user_id"]; ?></h2>
<?php } ?>
<?php else { ?>
<form action="" method="post">
<label for="user">User ID</label>
<input type="text" id="user" />
<label for="password">Password</label>
<input type="password" id="password" />
<input type="submit" value="Login" />
</form>
<?php } ?>
While I keep the } else { on same line, it works fine. I mean the code below just works fine:
<?php if (isset($_SESSION["user_id"])) { ?>
<h2>Welcome, <?php echo $_SESSION["user_id"]; ?></h2>
<?php } else { ?>
<form action="" method="post">
<label for="user">User ID</label>
<input type="text" id="user" />
<label for="password">Password</label>
<input type="password" id="password" />
<input type="submit" value="Login" />
</form>
<?php } ?>
thinking a bit about this, I've come to the realization that this has to be the intended behavior.
consider the following (syntactical wrong ) example:
<?php if ($condition == true) { ?>
<div id="first">Yey</div>
<?php } ?>
<span id="second?">where am I?</span>
<?php else { ?>
<div id="first">Ney</div>
<?php } ?>
the span element would be in an undefined state
It seems to me, that you can't start a new code block with an else statement without a preceding if.
You could…
A) write your code in one block, e.g.
<?php }
else { ?>
B) or use the alternative syntax, if you are working with multiple code-blocks:
<?php if (isset($_SESSION["user_id"])): ?>
/* … */
<?php else: ?>
/* … */
<?php endif; ?>
There's nothing weird per se, it's because you're in a separate code block, that's the simplest way to put it. Nothing is open at the time of you "Leaving PHP", so when you go back into it there is no context.
Consider your code like this (of course consider it as pseudo-code just to emphasise the point):
if (isset($_SESSION["user_id"])) {
// ....
}; else {
// ....
}
Breaking in/out of PHP can be tricky at times, and managing it like you want to in your first example doesn't really make very much sense.
You might want to consider using this, which would put your transition to the else block on a single line anyway:
<?php if (isset($_SESSION["user_id"])): ?>
<h2>Welcome, <?php echo $_SESSION["user_id"]; ?></h2>
<?php else: ?>
<form action="" method="post">
<label for="user">User ID</label>
<input type="text" id="user" />
<label for="password">Password</label>
<input type="password" id="password" />
<input type="submit" value="Login" />
</form>
<?php endif ?>
At the end of the day whilst PHP is pretty flexible I wouldn't expect it to allow you to do what you're wanting. That would allow for an else block to be added miles away which may not be the intention at all.
<?php } ?>
^^
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
When the PHP parser comes to this line it executes the if block only. After that PHP parser tried to parse the next block of code(else part) but here it start with else { and because of that else is separated from if and produces error.

Colorbox doesn't execute PHP code in AJAX call

I have a PHP file and want it load in a colorbox modal popup via AJAX but the PHP code inside of the file doesn't get executed (normal HTML tags like divs are visible only if I remove all PHP stuff). I have no idea why this is happening.
I can see e.g. the constants' names (like EP_AVATAR_RESTRICTIONS) but not their content (in this case it's just text).
My colorbox code:
$(".edit_avatar_link").colorbox({
initialWidth:'386',
initialHeight:'528',
innerWidth:'386',
innerHeight:'528',
href: "<?php echo $setting['site_url'];?>/includes/forms/avatar_form2.php",
fixed:true,
scrolling:false,
transition:'none',
onComplete: function(){
$("#cboxLoadedContent").appendTo("#cboxContent");
var title = 'Edit Avatar';
$('#cboxTitle').text(title);
}
});
My PHP-file (avatar_form2.php):
<?php defined( 'AVARCADE_' ) or die( '' ); // Security ?>
<div id="edit_avatar_content">
<div class="edit_profile_header"><?php echo EP_EDIT_AVATAR;?></div>
<div class="edit_avatar_container">
<div class="edit_profile_lable"><img src="<?php echo $user['avatar'];?>" width="75" height="75"></div>
<div class="edit_avatar_element">
<form enctype="multipart/form-data" id="form1" method="post" action="?task=edit_profile&done=avatar">
<input name="new_id" type="hidden" id="new_id" value="<?php echo $new_id;?>" />
<span class="style1"><?php echo EP_AVATAR_UP;?></span>
<input name="img_file" type="file" id="img_file" size="50" /> <input type="Submit" name="Submit" value="<?php echo EP_AVATAR_BUTTON;?>"/>
<?php echo EP_AVATAR_RESTRICTIONS;?>
</form>
</div>
</div>
</div>
It is normal that you don't get anything. Your colorbox calls a programatically protected php file.
This line of code is causing it:
<?php defined( 'AVARCADE_' ) or die( '' ); // Security ?>
This is a direct access protection so that you can not directly access this file. One makes that to only make it includable from other php files that do a DEFINE('AVARCADE_','something');
either remove that line of code or make another php file that defines the AVARCADE_ constant and then include the vatar_form2.php in that file

Categories