0

Im creating a plugin with the following structure:

plugins (folder)
- myplugin (folder)
 - myplugin_index.php (adminpanel file)
 - myplugin_ajax.php (loading data when user enters a form on the "start page")

themes (folder)
 - mytheme (folder)
  - template_my_own.php (this file have the form which calls the myplugin_ajax.php)

The plugin works just fine from the admin panel point of view. I have added data through the plugin from the admin panel.

But when I try to access data through the form in the file template_my_own.php I get the following error:

PHP Fatal error:  Call to undefined function get_option() 
in /var/www/example.com/wwwroot/wp-content/plugins/myplugin/myplugin_ajax.php 
on line 14, referer: http://example.com/

I have tried the solutions from both this site.

None of them work though. My code lookes like this:

<?php
/*
Plugin Name: Myplugin
Plugin URI: http://example.com
*/
?>
<?php
    $square = $_POST['square'];

    //Get arrays and remove empty array values
    $arrayFrom =            array_filter(get_option('from'), 'strlen');
    $arrayTo =          array_filter(get_option('to'), 'strlen');
    $arrayPrice =   array_filter(get_option('price'), 'strlen');


    //Sort arrays
    asort($arrayFrom);
    asort($arrayTo);
    asort($arrayPrice);


    //Go through values until we find the one
    foreach($arrayFrom as $index => $from)
    {
        if($from < $square && $arrayto[$index] > $square)
        {
            echo $from;
            echo '<br>TESTTESTTEST';
            echo $to;
        }
    }   
?>

How should I solve this and what am I doing wrong?

3 Answers 3

3

How plugins loading works

WordPress has, as any other software, a specific order in which files get loaded. During loading WPs core files, there're specific points where you can either hook into do_action() or alter data during apply_filters() calls. Those functions always get called with a minimum of one argument: The name. Sometimes there're more arguments, which then are the callbacks.

Point is, that you have to wait until certain hooks to have some parts of the public API available.

The rule...

Never ever just execute code without wrapping it into a function or method (the mentioned callback) which you then hook. For more information about the order in which those hooks appear, take a look at the Codex and the Plugin API & Action Reference.

1

You should hook your function to the WordPress AJAX Api.

function your_ajax_function() {
    // your Script here
}

add_action( 'wp_ajax_your_ajax_function', 'your_ajax_function' );
add_action( 'wp_ajax_nopriv_your_ajax_function', 'your_ajax_function' ); // Skip this line if you want the AJAX just for logged in users

Now you can easily call your function via Javascript:

var data =  {
    action: 'your_ajax_function',
    your_data: 'datatosubmit'
};
$.post(ajaxurl, data, function(response) {
    // what to do with your response
});

Just be sure ajaxurl is defined. Usually it is http://www.yoursite.com/wp-admin/admin-ajax.php, but that depends on your WordPress setup.

If you use this method, you have all the WordPress functions available on your AJAX call.

0

You need to have WordPress loaded. Something along this lines:

<?php include '../../../wp-load.php'; ?>

but, this not advisable. I assume you want to do some ajax? If that's the case, do it like this outlined here: http://ottopress.com/2010/dont-include-wp-load-please/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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