Inside my active theme there is a user.php which serves this kind of url http://mysite.com/user/username. Inside user.php I echo a script tag with the following content
$.ajax({ url: "' . get_theme_root_uri() . '/fray/userslogan.php",
data: {"id": ' . $profile['id'] . ', "slogan": el.innerHTML},
type: "post",
success: function(status) { alert(status); }
});
I created a file userslogan.php and added it at the same level as user.php. Inside this file now all I want to do is
<?php
update_user_meta( $_POST['id'], 'slogan', $_POST['slogan'] );
echo 1;
?>
but I get errors that functions that I call are undefined. So if I include some file that defines the update_user_meta function, then I will get another similar error and so on. What is the right way of executing code like this?
You need to include
wp-load.php
to get access to the wordpress function in custom files.Suggestion: Donât include wp-load, please. Use ajax in wordpress in proper way. You can refer this article.
From above article
Try
WP AJAX
1) http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)
2) http://codex.wordpress.org/AJAX_in_Plugins
You need to have the entire WordPress codebase in there. Your best bet would be to make an actual WordPress plugin, that’s going to be a lot easier than this.
http://codex.wordpress.org/Writing_a_Plugin