I created a custom php file, put it in my folder. The files does insert some data to the database to a custom table i created.
require wp_path() . "/wp-load.php";
global $wpdb;
$favorite_table = $wpdb->prefix . "fav";
$wpdb->insert( $favorite_table, array( 'link' => $_POST['fav_link'], 'title'=> $_POST['title']) );
$lastid = $wpdb->insert_id;
echo $lastid;
function wp_path() {
if (strstr($_SERVER["SCRIPT_FILENAME"], "/wp-content/")) {
return preg_replace("/\/wp-content\/.*/", "", $_SERVER["SCRIPT_FILENAME"]);
}
return preg_replace("/\/[^\/]+?\/themes\/.*/", "", $_SERVER["SCRIPT_FILENAME"]);
}
what is calling this php file is a jquery ajax function from within my theme:
$.post(WPURLS.theme_directory_uri+'/favorite/fav.php', data).done(function(data ) {
alert(data);
}).fail(function() {
});
This insert into the database but doesn't return anything in the response, data is blank. while if i manually pasted the URL in browser (GET) i can see the $lastid displayed.
What am I doing wrong? I need to retrieve the response data $lastid.