0

I am building a custom plugin for my WordPress site.

I made a php file called "checkrank" that obtains data from a custom table named "cranking".

$uid = $GET['id']; 

function checkExists($id){
    global $wpdb;
    $table_name = $wpdb->prefix . 'cranking';
    $exists = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d" ) );

    $error = $wpdb->print_error();

    if(count($exists) > 0){
       echo 'Exists!';
    } else {
       echo 'Does not exist!';
    }

 return $error;
}

checkExists($uid);

The code is just to check whether the cranking table has the user with the provided ID.

The problem is, whenever i run this php file, i get this error :

PHP Fatal error: Call to a member function get_row() on a non-object

Where am I at fault here?
I think $wbdp is not initiated.

  • What do you mean by "whenever i run this php file"? Earlier, you mention that you are "building a custom plugin". Is this a plugin or a standalone file? – Johannes Pille Jul 27 '13 at 10:23
  • Hi,what i meant by running is going to the url (mysite.com/wp-content/plugins/myplugin/checkrank.php?id=1 – ETAN Jul 27 '13 at 10:25
  • this is a php file inside my plugin. – ETAN Jul 27 '13 at 10:26
1

In a comment you clarify:

what i meant by running is going to the url
(mysite.com/wp-content/plugins/myplugin/checkrank.php?id=1)

In that case, you are running a standalone file.

Yes, one that resides inside WordPress' plugins folder, but you don't access it in the context of WordPress (i.e. from within it). Here, none of the WordPress core functions/methods/classes will be available, because they are simply never loaded.

  • thanks for replying, oh i see, I did ran using jquery ajax call, which also returns the same error. so how do I load the functions? or how should i actually test it? – ETAN Jul 27 '13 at 11:02
  • thanks Johannes, i did some quick research, found my solution here:kynatro.com/blog/2012/12/11/… – ETAN Jul 27 '13 at 11:10
  • Yup, that is one of the approaches I was about to edit into the answer. Good find. As an aside, there is a multitude of answers on this stack detailing the same. – Johannes Pille Jul 27 '13 at 11:11
  • Here's a good one. – Johannes Pille Jul 27 '13 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.