Made Wordpress Plugin - Shows up at top of entire site - php

*EXAM AT HIVOLDA, IN CASE EXTERNAL EXAMINER SEES THIS*
I have just made some Wordpress Plugin for school.
My PHP-files works just fine, but when I upload and activates them as plugin, they only shows up at top of the page.
As you understand, I am kinda noob...
Screenshot: s27.postimg.org/53w3bmyw3/131128oav_plugin.jpg
Here is the shortest code:
<html>
<head>
<meta charset="utf-8">
<title>Registrering av åpne bakker</title>
</head>
<body>
<?PHP
/**
* Plugin Name: Haugsdalen Skisenter Bakkeregistrering
* Description: Registrering av åpne/stengte bakker for Haugsdalen Skisenter.
*/
function visSkjema(){
echo('
<form id="bakkerreg" name="bakkerreg" method="post" action="bakkerreg.php">
<label for="hoved">Er hovedbakken åpen?</label><br/>
<input type="radio" name="hoved" value="Åpen">Ja
<input type="radio" name="hoved" value="Stengt">Nei<br/>
<label for="skog1">Er skogsløypen vest åpen?</label><br/>
<input type="radio" name="skog1" value="Åpen">Ja
<input type="radio" name="skog1" value="Stengt">Nei<br/>
<label for="skog2">Er skogsløypen øst åpen?</label><br/>
<input type="radio" name="skog2" value="Åpen">Ja
<input type="radio" name="skog2" value="Stengt">Nei<br/>
<input type="submit" name="lagre" id="lagre" value="Send inn">
</form>');
}
if( isset($_POST['lagre'])) {
if( $_POST['hoved']!="" &&
$_POST['skog1']!="" AND
$_POST['skog2']!="" ){
/* Her lagrer den til databasen*/
$db = mysql_connect('localhost','USERNAME','PASSWORD');
/* Velge database */
mysql_select_db('USERNAME',$db);
/* Definer spørringen */
$sql = "INSERT INTO bakker(hoved,skog1,skog2)
VALUES('".$_POST['hoved']."',
'".$_POST['skog1']."',
'".$_POST['skog2']."') ";
/* Kjør spørring */
$resultat = mysql_query($sql);
if($resultat) {
echo ("<p>Grattis, du har nå registrert informasjonen!</p>");
}else {
echo ("<p>Du failet!</p>");
echo ("<p>".mysql_error()."</p>");
}
}else{ // Manglande obligatoriske felt
visSkjema();
}
}else {
/*Vis skjema, ingen har trykt på lagre knappen (enda) */
visSkjema();
}
?>
</body>
</html>
Suggestions? :)
The intention is to easy update information on the frontpage. It will be a admin-poll-system, where the company can update the information by pressing checkboxes. I have made 3 plugins that has this function. This one has the function that the company can update which of the ski hills are open. I have also made a plugin which has a registration form, and sends to database. And a file that recieve information from the database, and admin can delete any rows.
*EXAM AT HIVOLDA, IN CASE EXTERNAL EXAMINER SEES THIS*

It sounds like you're a bit confused on how WordPress functions. Plugins are all included into Wordpress at the very beginning of the page load, right after WordPress gets all its files loaded, but before it actually does anything. This allows plugins to modify any functionality from that point further, but means you can't actually write out any code yet, because then it'd end up at the top of the page.
Wordpress combats this issue by what is called the hook system. Hooks (or action hooks) are setup throughout WordPress and are called at certain times, and you can register your functions with them, so they are called when that hook is run. I know that's a bit abstract, so here is an example:
add_action( 'get_footer', 'customplugin_footer_hook' );
function customplugin_footer_hook ($name)
{
echo 'Some code here.';
echo 'This will show up right before the footer, because it's registered to the get_footer action hook';
}
There are actions and filters, filters are designed to modify data passed to them, and return it, actions (or hooks) are designed to be given data and print something to the screen (but that isn't always what they're used for).
All this being said, it doesn't look like it'd help much with what you're actually trying to do, as processing post data in WordPress can be a bit confusing, and I can't tell if that is suppose to be a poll, or admin settings, or something else; I don't speak Norwegian :)
Give us a bit more information on what you're trying to achieve and we may be able to give you a bit more detailed information on how to do it.
Some helpful links in the meantime:
Wordpress action hooks
Wordpress filter hooks
Decent guide over hooks

Related

how to write effective wordpress plugin to search using user input via postcode

I am trying to create my own word-press plugin which will allow a user to submit their postcode on a form, this will search a list of postcodes and select a correct area and then divert the user to the webpage with the appropriate area in the URL.
I am struggling to get this to work, and am also worried I may be taking an altogether ridiculous approach, and would appreciate (constructive) advice from somebody with more experience.
I have managed to get all of the postcode data corresponding to area in a table. This is roughly 2 million rows for reference. eg. WD4 5SH - Hartfordshire
The current approach I have tried is to create a plugin which is essentially the one function, it creates the postcode data into an array. It then takes the user input and finds the corresponding area. Finally it uses the header function to redirect to the appropriate URL.
I have tried to test this on instant wordpress, but it has come up with an error of the memory limit being reached. I could possibly try to increase the memory, but I am now worried that this is going to make the website really heavy, and maybe I should be using some sort of database.
To give you a general idea of my level I am moderately comfortable with scripting mostly in Perl and R but new to Wordpress.
My code is essentially:
Plugin:
<?php
/**
* Plugin Name: wordpress plugin
**/
function Search_Area_function($post_code) {
$Postcodes = array( "AL1 1AG" => "1", "AL1 1AJ" => "2" );
$AreaKey = array( "1" => "Hertfordshire", "2" => "Bedfordshire");
/* Search array for the core post info, assign number simultaneously */
$currentAreaCode = array_search($post_code, $Postcodes);
$areafound = array_search($currentAreaCode, $AreaKey);
/* redirect to county page */
header('Location: examplewebsite.com/'.$areafound);
}
?>
Form:
<form id="search" action="wordpress_plugin.php" method="get">
<div><input id="s" name="s" type="text" value="Postcode" /> <input id="searchsubmit" type="submit" value="search" /></div>
</form>

using get_post_meta() In wordpress plugin after form submit

I have a simple wordpress plugin that will take one or two inputs and display a chunk of HTML using a few pieces of meta data from custom posts in Wordpress (within the plugin admin menu).
I have it displaying the HTML output just fine on a new blank page, as well as displaying the input from $_POST when I tested it, although when I try and use get_post_meta() function using my $_post variables within that html output it draws a blank.
I'm using one PHP file, and the code is:
<?php
if (!isset($_POST['submit'])) {
?>
<h1>My Plugin</h1>
<form action="<?php echo plugin_dir_url( __FILE__ ).'myplugin.php'?>" method="post" >
Enter input:<input type="text" name="info" size="30">
<p><input type="submit" name="submit" value="Go">
</form>
<?php
}
else {
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<?php
$info = $_POST['info'];
?>
Information based on your input is:<?php echo get_post_meta($info, '_info', true); ?>
</body>
</html>
<?php } ?>
I'm a beginner at PHP and plugin development, so apologies for all the silly mistakes that are sprinkled in their most likely.
From my scouring on SO and other wordpress forums, I've got a hint that I might be taking the output out of the wordpress 'Sandbox' and therefore direct wordpress functions wouldn't work. So my question would be, what is the best way to generate my chunk of html with dynamic php on a separate blank page that still works with get_post_meta()?
I should mention that the code works correctly if I put it all on the first plugin page with no forms, and just force a few test variables to pull from the DB.

Updating html after php call to a stand-alone php file

I am trying to implement a user login system for my website. I have a newMember.html file that has a form like this:
<form method="post" action="nameValidation.php">
<p>Username:</p>
<input type="text" name"user" required>
<input name="submitButton" type="submit" value="Create account"/>
</form>
inside nameValidation, I check if the username is available. If it's not, i want some way to show a red text next to my input field where you type in your username. I tried something like this, but got a server error:
(inside nameValidation.php)
<?php
//connect to database, check the value of the username to see if it exists
if (username already exists) {
//keep the page that i had the way it was,
//just add a red sentence saying "username is already taken"
}
else {
//tell the user he was successful in making an account,
//and redirect him to my login page (already implemented)
}
?>
I didn't want to use inline php for two reasons.
1) it looks cluttered and confusing (at least to me)
2) I read in other stackoverflow posts that it's not good practice.
I will use inline php if i have to, but any and all help will be greatly appreciated!
Use a web framework like CakePHP, Laravel, Symfony or any other framework that suits you.
A PHP framework uses templates to accommodate dynamic pages. They usually allow special shortcuts in your code, so that you do not have to write any 'inline PHP'.
This is the MVC (Model View Controller) mentality: the controllers control the actual data being used in your view, and not the view.
Enough about web frameworks.
To answer your question, first rename your file to end in .php, to show that the file contains PHP.
Next, edit the code such that it checks if a user exists. The most beautiful inline PHP code gets created by doing all the work at the top of the page, and using the generated data in the page itself.
So, for your HTML:
<?php
/* For example: */
$username = $_POST['username'];
$db = new DB(/* ... */);
$userNameExists = $db -> userNameExists ($username);
?>
<!-- The rest of the code of the page -->
<form method="post" action="nameValidation.php">
<p>Username:</p>
<input type="text" name"user" required value="<?php echo $username; ?>"><?php echo $userNameExists ? '<span class="error">Username already exists</span>' : '' ?>
<input name="submitButton" type="submit" value="Create account"/>
</form>

Can't save theme options, following a tutorial from tutsplus

I know that the question has been asked before, why isn't my code saving, but this tutorial was recently recommended by a user here, and I have been following it from tuts plus, (obviously changing variables and options to fit my needs), but for some reason the little bar that says settings saved will come up, and the save button comes up, the form itself, it all looks right, but when I hit save, it is gone from the form. Keep in mind that I have only tried saving the settings for the first options, site logo url. Here is my code, it is properly included in the functions.php file.
PS: Sorry for it all being together, PHP and HTML, but that was the only way to get it all together in one post, plus, I like to put things in separate files after I finish.
<?php
if (!current_user_can('manage_options')) {
wp_die('You do not have permission to view this page, if you believe that this is a mistake, please contact your system administrator, or try closing the tab and come back.');
}
add_action("admin_menu", "setup_where_now_admin_menus");
function setup_where_now_admin_menus() {
add_menu_page('Front Door Theme Options', 'Front Door Theme Options', 'manage_options', 'where_now_elements', 'where_now_options') ;
}
?>
<?php
function where_now_options() { ?>
<div class="wrap">
<h2>Front Door Web Design Custom Theme Options</h2>
<form method="post" action="">
<h4>Site Logo URL</h4>
<p>Remember, this is the image used in places like your header, or anywhere else you want your logo to appear, and if you are having trouble remembering how to use this feature, you can always upload your new logo to the media section in Wordpress, get the URL from the specific images page, and then paste it here!</p>
<input type="text" name="site_logo" value="<?php echo $site_logo;?>" size="25">
<h4>Analytics Tracking Code</h4>
<p>Any code used for tracking purposes that is placed in the header goes here</p>
<input type="text" name="analytics_code">
<?php
$site_logo = get_option("where_now_site_logo");
if (isset($_POST["update_settings"])) {
$site_logo = esc_attr($_POST["site_logo"]);
update_option("where_now_site_logo", $site_logo);
?>
<div id="message" class="updated">Your Changes Have Been Saved</div>
<?php
}
?>
<input type="hidden" name="update_settings" value="Y" />
<input type="submit" value="Save settings" class="button-primary"/>
</form>
</div>
<?php
}
?>
Any ideas?
I recommend you to use this Plugin: Option Tree
It is very simple and doesn't need any coding.Just install it on your website that your theme is running,then you can manage your options easily.

custom wordpress comment form html

I would like to modify the actual HTML of the comment forms in WordPress. I know of the comment_form() function that WordPress provides, but it doesn't actually let me modify the html of the form. What I'm specifically trying to do is make a bootstrap responsive form, which is not possible with the options provided with the comment_form() function. I've tried looking all over the internet for a way, but to no avail. Can anyone point me in the right direction?
Let me clarify this:
I am looking to modify the actual FORM and containing DIV elements of the comment form, not just the fields.
The uncustomizable parts of the function comment_form() should (idealistically) be limited purely to code that is essential for security and proper form handling by the WordPress core. However, that is not the case. To explore the issue, I used filters (could have used arguments) to completely remove all the code that is customizable. I set all available variables to empty strings—that is, all the stuff mentioned on the codex page for comment_form().
Here is the left-over, non-customisable code from comment_form():
<div id="respond" class="comment-respond">
<h3 id="reply-title" class="comment-reply-title"> <small><a rel="nofollow" id="cancel-comment-reply-link" href="/1#respond" style="display:none;">Click here to cancel reply.</a></small></h3>
<form action="http://www.[yourdomain].com/wp-comments-post.php" method="post" id="" class="comment-form">
<p class="form-submit">
<input name="submit" type="submit" id="" value="" />
<input type='hidden' name='comment_post_ID' value='1' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</p>
</form>
</div><!-- #respond -->
Where does all this come from? At the bottom of the codex page for comment_form() it says…
Source Code
comment_form() is located in wp-includes/comment-template.php.
comment-template.php is linked and you can view the full code in the browser. comment_form() starts on line 1960. In fact, it’s the last function in that file. There are no arguments or filters that let you modify the residual code above. These lines of code are all moreorless “hard-coded”.
The text Click here to cancel reply. is the only text that survived my filter genocide. Strangely, comment_form() has back-up text for cancel_reply_link hard-coded into the function, in case it is passed to the function as an empty string. None of the other filterable items have hard-coded back-ups.
It is easy to see which bits of code are essential and which bits are non-essential for a HTML form. A little more difficult to judge is which bits are essential for a WordPress form. Some of the bits above were dynamically output (note that this is the first comment on a development blog with no other replies, no parent/child comments).
From the comment_form() function in comment-template.php you can draw out the code needed to produce the dynamic parts of the WordPress form. Then, with default arguments taken from the codex page for comment_form(), you could piece together a barebones form, hard-coding the desired fields, labels and wrapping HTML. I’m doing that now and putting my custom form function in my theme’s comments.php template file, which I call using comments_template() only in single.php (for this particular theme).
The result would be a full and proper, lean and mean WordPress comment form. But… it would be a form that could not be customized anymore using comment_form() arguments or related filters unless you went ahead and included the filter code in your own comment form function. Since you’re customizing the heck out it already, that’s probably not a problem. Similarly, all the WordPress actions would also be unavailable to you or any other plugins unless you also triggered those action functions in your own comment form function. Again, probably not an issue for you at this point.
But most importantly, the resulting form might break your theme if future WordPress updates change the way the core handles forms.
If you’re aware of those risks, you can probably rebuild a hand-coded comment form just from copying code on the codex page for comment_form() and in wp-includes/comment-template.php. I don’t have the finished code, otherwise I’d post it. But I will post when/if I succeed.
Right, that’s all from me for now. Bear in mind (all readers) that despite appearances, I am an amateur WordPress theme developer and my proficiency with PHP and WordPress is very rudimentary. Just writing up this post I learned a lot.
I’m also worried that a full and proper solution is already out there somewhere but I haven’t found it in my searches.
Just Create a New PHP file with the name of comment_form.php then paste all of your code. And your comment form is ready.
you shouldn't edit the wp-includes/comment-template.php file.
Keep this for your ref for more styling your existing comment form
http://codex.wordpress.org/Template_Tags/comment_form
Or this one
http://wordpress.org/support/topic/where-to-find-the-comment_form-function-in-wp3
Form code and action
<div class="comment-form">
<h2 class="comments-wrapper-heading"> Leave a comment </h2>
<form id="commentform" action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<div class="commentform-element">
<label class="hide" for="author">Full Name</label>
<input class="input-fields" id="author" name="author" type="text" placeholder="Full Name" value=""/>
</div>
<div class="commentform-element">
<label class="hide" for="author">Email</label>
<input class="input-fields" id="email" name="email" type="text" placeholder="Email" value=""/>
</div>
<div class="commentform-element">
<label class="hide" for="comment">Message</label>
<textarea id="comment" class="input-fields" placeholder="Message" name="comment" cols="40" rows="200"></textarea>
</div>
<input name="submit" class="form-submit-button" type="submit" id="submit-comment" value="Post comment">
<input type="hidden" name="comment_post_ID" value="22" id="comment_post_ID">
<input type="hidden" name="comment_parent" id="comment_parent" value="0">
</form>
</div>
WP does not offer a way to create and use a comment form template. i.e.: as of 4.1.1, there's still no way to control all of the HTML markup that makes up the form. There are only ways to edit parts of it and that's by supplying arguments to comment_form().
So, if you would like to edit the whole layout (e.g.: place some of the content side to side instead of on top of one another; replace some of the <p>with <div>), you're out of luck.
What can we do?
An option would be to borrow the whole comment_form() function from the WordPress codebase (like this guy did): make a copy of the function in your theme, rename it, customize it and then call it in place of the vanilla function. That will work but there's a chance it might break on a future WordPress update if some of the inner workings of WP change.
Did you look for the comment_form() function in Wordpress code to see if you can edit it ?
You'll also find some informations here :
http://wordpress.org/support/topic/editing-the-comment-form
This function uses the default form. You need to make a comment-template.php file in your theme and in that file you can make your form.
Now when you will call comments_template() you will get your made form instead of default form.
I don't think you can change the containing div (<div id="respond" class="comment-respond">). Have a look at the comment-template.php
The only thing you can change is the form ID. 'id_form' => 'commentform'
You can make a responsive form without changing these elements.
I've recently answered something similiar on WPSE. You can go and check it out here

Categories