[Solved] update_option doesn’t work in single php file


As pointed out in the comments your file is outside wordpress environment, so it doesn’t recognize the update_option function. Even knowing the ajax api was mentioned in the comments, I’m putting here what you should do:

  1. Require the activate.php file in the main plugin file if you aren’t doing this yet. Something simple like require_once('admin/activate.php'); should do it.

  2. Hook an action into wordpress ajax using the wp_ajax hooks. You can place this code in your main plugin file or in activate.php (since it’s required by the former).

    add_action( 'wp_ajax_my_plugin_activate', 'my_plugin_activate' );
    add_action( 'wp_ajax_nopriv_my_plugin_activate', 'my_plugin_activate' );
    
  3. Surround your activate.php code with the function named above, like this:

    function my_plugin_activate() {
        if(isset($_REQUEST['txtAC']) && isset($_REQUEST['txtKey'])) {
            $ac = $_REQUEST['txtAC'];
            $key = $_REQUEST['txtKey'];
    
            // the code...
    
        }
        wp_die();
    }
    

    Note that you don’t have to test against $_GET['activate'] anymore.

  4. Change the url of the ajax post to wp-admin/admin-ajax.php and pass the action as the data attribute. This should be done localizing your script (as seen in the docs). For simplifing purposes I’m putting it here directly:

    jQuery.ajax({
        url: "../wp-admin/admin-ajax.php", // you should use wp_localize_script in your PHP code and the defined variable here
        method: "POST",
        data: { action: 'my_plugin_activate', txtAC : txtAC, txtKey: txtKey }
    })
    

Excuse my English, it’s not my mother language. Hope it helps!

1

solved update_option doesn’t work in single php file