Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I initialize inside wordpress a class. I would like to send requests via ajax to plugins class. First I have the entry point.

class EntryClass{

    public function __construct()
    {
            add_action( 'wp_enqueue_scripts', array($this , 'wptuts_scripts_basic' ) );
        add_shortcode( 'podukt_konfigurator', array ($this, 'getProd_Config') );
    }

    function getProd_Config() { 
        ob_start();
        include( 'html/templ_config.php');
        $content = ob_get_contents();
        ob_end_clean();
        return $content;        
    }

    function wptuts_scripts_basic()
{
    // Register the script like this for a plugin:
    wp_register_script( 'konf-js', plugins_url( 'config.js', __FILE__ ) );
}

}

$init= new EntryClass();

The templ_config.php contains the Javascript which on event would load a php file inside of my plugin folder like

jQuery('#id').load(
            '/wordpress/wp-content/plugins/konfigurator/html/class.konfigurator.php',
            {
                prod_detail: jQuery(this).attr('rel')},
                function(msg){

                    show_driver(msg);
                }
                );

then in class.konfigurator.php I would like to use wordpress globals.

share|improve this question
    
Firstly, you cannot just write javascript inside a PHP file. Secondly, to use wordpress globals, you just have to write global $WP_VAR_NAME; then you can work with it. Or use the 'shorthand' like $GLOBALS['VAR_NAME']. –  t f Mar 7 '13 at 17:14
    
I did not let me show you how. The problem is than I can not use for example $wpdb –  fefe Mar 7 '13 at 17:22
    
Yes, you CAN use global WordPress variables. In your case it'd be global $wpdb; $wpdb->WHATEVER;. But I don't get the context of this and instantiating a class and all... –  t f Mar 7 '13 at 17:30
add comment

1 Answer 1

  1. You don't appear to have actually enqueued your script anywhere. You registered it, but don't enqueue it, unless that code is elsewhere.

  2. When you load a file directly like you are-- aka., browsing straight to '/wordpress/wp-content/plugins/konfigurator/html/class.konfigurator.php'-- you can't use any of WordPress' functions because you have bypassed WordPress' boot process.

To use WordPress functions you need to load WordPress. In your case, to do that you should be using the AJAX API, or if you need the performance, implement a solution like suggested as an answer to AJAX API performance question.

share|improve this answer
    
thank you for your feedback!! –  fefe Mar 7 '13 at 18:00
    
I don't know what the question has to do with initializing a class in a plugin though. –  s_ha_dum Mar 7 '13 at 18:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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