2

This is tricky, as people usually mix wp-load with AJAX and I can't really find a proper answer.

Case 1

I have a dynamic style.php file that looks like this:

<?php header("Content-type: text/css");
require_once('../../../../wp-load.php'); ?>

body {
   background: <?php echo get_option('background_color'); ?>;
}

Case 2

I'm using AJAX call:

  jQuery.post('<?php bloginfo('template_directory');  ?>/framework/foo.php',{ foo: smth, bar: else });

Then I'm running some WordPress functions within foo.php (it's not JS as in other examples):

$foo = $_POST['foo'];
$bar = $_POST['bar'];

$theme_options = get_option('theme_options');
$theme_options[$smth] =  $foo; 
update_option('theme_options', $theme_options);

How to avoid wp-loads in both of these cases?

Thanks a lot :)

1 Answer 1

1

Here is a basic setup that I use for AJAX with Wordpress. instead of loading wp-load.php; just use Wordpress default method for AJAX calls. This allows you to also filter function calls from Javascript through a switch. I also added a quick example for wp_localize_script.

<?php

add_action('wp_enqueue_scripts', 'YOUR_NAME_scripts'); //back end

function YOUR_NAME_scripts( $hook_suffix ) {

            wp_enqueue_script('YOUR_NAME-js');

            global $blog_id;
            $params = array(
                'site_url' => site_url(),
                'admin_ajax_url' => site_url() . '/wp-admin/admin-ajax.php'
            );

            wp_localize_script( 'jquery', 'YOUR_NAME', $params );

}

add_action('wp_ajax_nopriv_YOUR_NAME_ajax', 'YOUR_NAME_ajax_function');
add_action('wp_ajax_YOUR_NAME_ajax', 'YOUR_NAME_ajax_function');
function YOUR_NAME_ajax_function(){

    fobu_load_classes();

    switch($_REQUEST['fn']):
        case 'test_ajax':
            $output = $_REQUEST['data'];
        break;


        default:
            $output = 'No function specified, check your jQuery.ajax() call';
        break;
    endswitch;

    ob_clean();
    $output=json_encode($output);
    if(  is_array( $output )  ):

        print_r( $output );
    else:

        echo $output;
    endif;
    die();

}





?>

<script>
//in YOUR_NAME.js or whatever
jQuery(document).ready(function() { 
    (function ($) { 

                jQuery.ajax({
                    url: YOUR_NAME.admin_ajax_url,
                    dataType: 'json',
                    //type:'POST',
                    data: {
                       'action':'YOUR_NAME_ajax',
                       'fn': 'test_ajax',
                       'data': data
                       },
                    success: function(results){
                        //console.log(results);
                        if( results ){

                        }

                    },// end of success
                    error: function(errorThrown){console.log(errorThrown);}
                });// end of ajax   

    })(jQuery);
});

</script>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.