2 days ago I asked this then I tried to work on my code to be able to understand how ajax and php work and now I have a new issue that, probably would be easily solved.

These are my codes:

Template Name: Nuovo form
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 

        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'myForm' and provide a simple callback function 
            $('#myForm').ajaxForm(function() { 
                alert("Thank you for your comment!"); 
<?php wp_head();?>
<form id="myForm" action="<?php echo get_template_directory_uri();?>/nuovoform/comment.php" method="post"> 
    Name: <input type="text" name="name" /> 
    Comment: <textarea name="comment"></textarea> 
    <input type="submit" value="Submit Comment" /> 

Then this is my Comment.php

// This is comment.php content
$nuovoform = 0;
$nuovocommento = 1;
if ( isset( $_POST['name'] )){
$nuovoform = $_POST['name'];}
if ( isset( $_POST['comment'] )){
$nuovocommento = $_POST['comment'];}
$controlliamo = get_post_meta($post->ID, 'varcontrollo', true);

'varcontrollo' is a custom meta field created using Custom Field Template plugin.

What I'm trying to do is use the form I created to store the result inside my custom field. If I try to browse to comment.php it says this:

Fatal error: Call to undefined function update_post_meta() in D:\Locali\xampp\htdocs\wp-content\themes\slotlandia\nuovoform\comment.php on line 8

Probably i have to 'import' WordPress 'global' variables but I don't know what have I to write.

Is there anyone who can help me? Thank you very much.

  • You need to get an idea how to code a wordpress theme. Do a google search and follow a tutorial. I think then you will know most of the basics about how create and wordpress template files. Fatal error says you don't have any function named update_meta_data() – Sisir Jul 27 '12 at 16:55
  • I have never had this kind of issues and I am usually able to code a custom template. Only this time I'm having problems so a good answer would be more helpfull instead of an useless comment... I now what Fatal Error means but I can't understand why update_meta_data() is not recognized. I tought that update_meta_data() was a 'core' function of Wordpress with no need to redeclare it each time. – maisdesign Jul 27 '12 at 16:57
  • 1
    There is no function update_meta_data() in WordPress. – fuxia Jul 27 '12 at 17:11
  • @toscho yes you are right, I was wrong the correct function is: update_post_meta Anyway I have the same problem even after using: update_post_meta – maisdesign Jul 27 '12 at 17:37
  • this question is a similar issue, answer is the same. – Milo Jul 27 '12 at 17:51

That will not work because you are referring to a single php file, instead the the whole WordPress environment.

There's a easier way to use ajax in WordPress.

Use admin_url('admin-ajax') as the form action, then put the function that will answer the ajax call in the theme's functions.php.

More detail here in this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.