0

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.

3
  • 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'].
    – tfrommen
    Commented Mar 7, 2013 at 17:14
  • I did not let me show you how. The problem is than I can not use for example $wpdb
    – fefe
    Commented Mar 7, 2013 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...
    – tfrommen
    Commented Mar 7, 2013 at 17:30

1 Answer 1

0
  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.

1
  • I don't know what the question has to do with initializing a class in a plugin though.
    – s_ha_dum
    Commented Mar 7, 2013 at 18:04

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.