wordpress meta box\custom fields validation - php

I am trying to validate custom meta box fields. On the server side I tried to use "return false" on the "save_post" action, but WP disregards it (apparently the save_post is called after the post is already inserted to the DB).
I then resorted to JQuery on the client-side, but using "return false" froze the page, and it stopped responding, as if I was using an infinite loop.
the code is plain and simple:
$("#post").submit(function(){
if ($("input[name='post_title']").val()==='') {return false;};
});
Any ideas? Maybe built-in WP functionality?

The WordPress SE site has some good stuff on this topic..
Server side validation is obviously preferable.. can't rely on javascript.
This topic has a good discussion on how WP lets you handle errors.
I haven't tried implementing anything like this on meta boxes, so I can't say for sure it'll solve your problem, but hopefully it will put you on the right track.

Related

Use formvalidation on entire form after page reload from submit

I'm coding some basic validation (mainly for aesthetic purposes, will do the main stuff in PHP) with formvalidation, and I've noticed a big problem in that once I hit submit, if it goes back to the same page, all the fields are no longer validated. I've tried a couple of lines of code from googling the problem, but none seemed to work.
Here is one line that seems to run on the datepicker alright when you use it, but if I just stick it out in the option for the document ready function (I just use trial and error with jquery), it does nothing.
$('#main_form').formValidation('revalidateField', 'pcn');
From here I tried this:
$('#main_form').validate().form();
But for some reason the entire validation breaks when I use that.
Here is a jsfiddle using the jquery code I'm using, what I basically want is it to instantly say that the field is required when you hit run.
Alternatively, can I set it to default to a certain message? That way I have PHP validate it on page load.
Finally got it working after a lot of trial and error. It turned out to be quite a simple fix, but needed to be in the correct place.
Add this line below all the validation code:
$('#form_id').formValidation('validate');
I'm hiding it behind if(isset($_POST['submit'])), so it'll revalidate the form on submit, but not validate when you first load.

How do I make a custom <form> element be used in place of a <form> in a pre-existing plugin?

I am customizing a theme and a plugin in Wordpress. The plugin has a button where a user can click to bring up a form where they can ask a question. I want users to use post questions through this button on this plugin, but I don't fancy the visuals on it. So I thought I'd make a form on my landing page that will post into the plugin's form. If this is a terrible idea in the long run, please feel free to let me know because I am still new to web dev.
So the goal is to create a new form whose aesthetics are to my liking but still use the plugin's built in posting capability. How do I make my form paste into the plugin's form and then submit it? You'll notice that my form has 1 additional field: the add money field. I also need to integrate this into the plugin. If anyone know how to begin modifying the code for this I'm all ears to anything I need to start reading or a general way of how to approach this.
My form
Plugin's form
Summary:
Need to have my form paste to plugn form and submit
Need to add a field to the plugin code that my form can post to similar to how the other fields will do do
You can post from a different form if you get the destination URL or javascript that is handling the original form post. You can use the debugger in Firefox or Chrome to inspect the page contents to see how the form is handled.
Adding the money field will be more complicated because you will need to update the server side handling to accept the additional parameter so that it is not simply ignored or causing errors. How much work this is depends on what the server has to do. It may involve adding a column to the database or creating a new table which you will likely want to do the wordpress way if you want to have something that is maintainable.
Lastly keep in mind, that every time the original plugin is updated, it may bust your add on code unless you create your own plugin from the original.

How can I capture a form textarea value in WordPress and insert into mysql?

I see all sorts of people asking this but no solid answers. I want to take text from a textarea form where users can enter their bio and then insert it into the bio field in mysql. This might be fairly easy normally, but alas, it seems nearly impossible with WordPress. I have to use the "Php for posts and pages" plugin and for some reason, the php script I have cannot be found (404 error) no matter where I place it. Then I thought I would try to just make the form and php all on one WordPress page, but it apparently can't be done like on a normal php web page. Anyone have any experience doing this? Any approaches that are known to work?
You can make form using normal php and insert it into mysql.
You can do this by making page template in wordpress.
http://codex.wordpress.org/Page_Templates
I've went through the same process! :)
My solution was:
Installing a PHP insertor plugin (specifically this). It's lot easier than creating a template.
I used it like this (inserted into the post's source):
[insert_php] include('myphpfile.php'); [/insert_php]
Then the included PHP's contents will be processed and printed out inside the page's body (the article's main part), so you don't have to create the html, head and body tags.
Then came the problem with posting form data to a page like this. This was because some of my form's fields kinda conflicted with WP's variables.
Some of the conflicting field names I've noticed: info, name
Just have a look at your form' field names and replace them to something else, and everything will be fine! :)
PS.: It's possible to put the form and the form data "receiver" in the same PHP script. I've done so and it worked in this scenario.
PS. 2: Optionally, you can disable the annoying auto-paragraph-insertor wpautop stock WP plugin, as it's affecting the included PHP's output. There is a per-post wpautop disabler plugin: Toggle wpautop

Is it possible to have a contact form on a pop up box without using ajax?

I am sorry for the stupid question, I realize that what I want to do must be extremely simple, but I can't find my way to do it.
I learned basic programing, mostly front end, and enough php to interact with databases and other basics. I can use jquery, javascrip, ajax, no problem in normal situations. But I am not fluent at all in using object-oriented php and I work on a Yii environment.
I have a contact form on the website I'm working on and it works perfectly sending the data to the database. Now I have to re-create the same form, and use it on a pop up box, but I have no idea how to do the php validation and saving to the database in that situation. I've always used Ajax and I tried it, but apparently you need special synthax with ajax on Yii that I am not familiar with.
So is there any way to have the php validation and saving being performed without ajax, (and without the form submitting that refreshes the page and closes the lightbox)?
"Popups": http://jqueryui.com/dialog/
ClientSide validation: http://jqueryvalidation.org/
PHP-Validation: Easiest Form validation library for PHP?
After everything I tried, because I don't know the Yii framework enough to use ajax in their synthax, I found a way to make my form work with simple jQ.
I have an empty onsubmit attribute on my form, that will change to "return false" if there are errors in the js validation, keeping the page from refresh and showing the errors. And if there aren't errors the form will submit the info to php validation then save to the database and all the php will be executed and the lightbox will re-open with the success message.

WordPress $_POST variables disappear completely on form submission

I have created a page template in WordPress to display a form with action="". Very simply, it will not post any of the form data upon submission. I have tried var_dump($_POST) but it just gives me array(0) {} every single time. I've tried examples provided by some websites for creating a form, but I can't reproduce the same results.
I have thoroughly search for answers to this problem but nothing seems to work. It must be something internal or very advanced. I just want to know why my form data won't post and where it might be going.
Check your data using a non-WP page, meaning put a php file in web-root, and have the form action point to it, such as ...action="/formtest.php", and on that page:
<?php
var_dump($_POST);
?>
This will test if you really aren't getting the POST vars. Then, since you'll 99% likely find they ARE being posted, the likely trouble is that your page IS being redirected somewhere by the theme as mentioned elsewhere (e.g. my theme includes a hook on 'template_redirect'...).
If you are theme savvy and just looking for a quick fix- at the expense of losing it on theme upgrade- you can create an exception just for your temp.php page, but the way that many developers handle it is via Plugins, or handle your form via Ajax. Many others have encountered this same issue, and so, either tacking on code to a Plugin, or performing in-page Ajax (since you are submitting to SELF anyway...).
This page will give you a leg-up on WP Ajax, it's a reputable and well written doc.

Categories