php – Ajax call not working in wordpress through a plugin

Question

I have 2 Ajax call the first is working but when it send the data to other the file send_data.php and when I try to run any wordpress function which is add_post_meta it doesn’t work!

This’s the first ajax call ok now the data sent to send_data.php file when I try to run any wordpress function like this add_post_meta(2, 'dropbox', 'Senddataphp', false); it prints this error what I want to do is pretty simple just get the data from ajax insert into wp database but I’ve no idea everytime I work with js with php problems like this happens. WHen I include require_once(ABSPATH . 'wp-config.php'); require_once(ABSPATH . 'wp-load.php'); the ajax call doesn’t work as well

caught Error: Call to undefined function add_post_meta() in C:\xampp\htdocs\wordpress\wp-content\plugins\Dropboxapi\includes\send_data.php:17
        Stack trace:

First Ajax code

$.ajax({
        
                type: "POST",
                url: 'wp-content/plugins/Dropboxapi/includes/send_data.php',
                data: {
                    req: JSON.stringify(dropbox_links)
                },
        
                cache: false,
                success: function(responseData) {
                    // consider using console.log for these kind of things.
                    console.log("Data recived: " + responseData);
                    console.log("First Ajax" + dropbox_links);
        
        
        
                }
            });

Second Ajax code here I tried to execute a function that take the sent data and insert into database but it doesn’t work, dropbox.php file is the main file of the plugin

$.ajax({
            type: "POST",
            url: 'wp-content/plugins/Dropboxapi/dropbox.php',

            data: {
                functionname: 'printdata2'
            },

            success: function(obj, textstatus) {
                if (!('error' in obj)) {
                    var yourVariable = obj.result;
                    console.log("SEc Ajax" + dropbox_links + yourVariable);

                } else {
                    console.log(obj.error);
                    console.log("SEc1 errr Ajax" + dropbox_links +
                        yourVariable);
                }
            },
            error: function(msg) {
                console.log(
                    " errrnot sure what to ask for here to check issue" +
                    msg);
            }
        });

0
Steve 2 months 2022-06-25T10:46:28-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse