Basheer Kharoti
Basheer Kharoti

Reputation: 4302

Wordpress admin_init in custom plugin on a custom function throws fatal error

A bit weird but really is ask-able question. I am creating a custom QuickBook Oauth plugin. I activated the plugin via admin dashboard but the functions defined in the plugin are not being loaded on do_action function but rather throws a fatal error

Fatal error: Call to undefined function is_rtl() in D:\xamp\htdocs\projectmanager\wp-includes\general-template.php on line 2616

Here's my plugin code

<?php
/**
 * Plugin Name: Oauth Quick Book
 * Plugin URI: https://www.test.com
 * Description: Authenticate to QuickBook and send/receives data
 * Version: 0.1.0
 * Author: Myname here
 * Author Uri: https://www.mysitehere.com
 * License: GPL-2.0+
 */
do_action('admin_init', 'authenticate');
function authenticate() {
            var_dump($_GET);exit;
        if(isset($_GET['app_token']))
        {
            if(current_user_can('cpm_super_admin'))
            {
                $app_token = $_GET['app_token'];
                $oauth_consumer_key = $_GET['consumer_key'];
                $oauth_consumer_secret = $_GET['consumer_secret'];
                $token = $_GET['app_token'];

                if(isset($oauth_consumer_secret) && $oauth_consumer_secret != null && $oauth_consumer_key != null && $app_token != null) 
                {

                    if ( ! add_post_meta( 1000011, 'oauth_request_token', $oauth_consumer_key, true ) ) { 
                       update_post_meta ( 1000011, 'oauth_request_token', $oauth_consumer_key );
                    }

                    if ( ! add_post_meta( 1000012, 'app_token', $token , true ) ) { 
                       update_post_meta ( 1000012, 'app_token', $token  );
                    }

                    if ( ! add_post_meta( 1000013, 'oauth_request_token_secret', $oauth_consumer_secret, true ) ) { 
                       update_post_meta ( 1000013, 'oauth_request_token_secret', $oauth_consumer_secret );
                    }

                    require_once (plugin_dir_path(__FILE__).'quickbooks-php/docs/partner_platform/example_app_ipp_v3/index.php');
                } else
                {
                    // require_once (plugin_dir_path(__FILE__).'quickbooks-php/docs/partner_platform/example_app_ipp_v3/index.php');
                }
            }
        }
}

When I remove the do_action function then it does not throw any error. Any help would be appreciated.

Upvotes: 0

Views: 1544

Answers (1)

vard
vard

Reputation: 4156

You shouldn't use do_action as it execute the action. Use instead add_action to attach your function to the admin_init action:

add_action('admin_init', 'authenticate');

That way it will be called at the right time, on admin initialization, so this hook (admin_init) will be triggered only when an user access admin area. If you want your function to execute on every page load, use the init hook or if you want it to execute only when an user log in, use the wp_login hook.

Upvotes: 2

Related Questions