Claudio
Claudio

Reputation: 75

wp_enqueue_scripts not loading (class based plugin)

For some reasons, i don't know why, i don't know how.. this is driving me nuts. What am i doing wrong here, i've searched for 3 hours straight for a solution

Here's the of the thesis.php file which is the "starter" file:

<?php

class Thesis
{
    public function __construct()
    {
        $this->thesis_hooks();
        $this->thesis_widgets();
        $this->thesis_api_endpoints();
    }

    public function thesis_hooks()
    {
        // Inizializza pagina admin
        add_action('admin_menu', array($this, 'thesis_admin_page'));
        add_action('wp_enqueue_scripts', array($this, 'thesis_scripts'));
    }

    public function thesis_scripts()
    {
        wp_enqueue_style( 'thesis-admin', THESIS_DIR . "app/assets/css/thesis.admin.css", array(), null );
        // This should load the css in the app/assets/css folder
        // I've tried with a js file using enqueue_scripts but it is still not working
        // The only way enqueuing works is when i put $this->thesis_scripts() in the constructor
        // but wordpress throws a warning saying "wp_enqueue_script was called incorrectly"
    }

    public function thesis_widgets()
    {
    }

    public function thesis_api_endpoints()
    {
    }

    public function thesis_admin_page()
    {
        add_menu_page(
            __('Thesis', 'textdomain'),
            'Thesis',
            'manage_options',
            'thesis',
            array($this, 'thesis_render_admin_page'),
            'dashicons-book-alt',
            10
        );
    }
    public function thesis_render_admin_page()
    {
        include_once(THESIS_DIR . 'app/views/admin/admin.php');
    }
}

The constants like THESIS_DIR are defined in the first file:

define('THESIS_DIR', plugin_dir_path(__FILE__));
define('THESIS_URL', plugin_dir_url(__FILE__));

// loading vendor **COMPOSER**
require_once(THESIS_DIR . 'app/vendor/autoload.php');
require_once(THESIS_DIR . 'app/includes/Thesis.php');

$init = new Thesis();

If you see function that are empty, they're in WIP..

EDIT: Actually i solved the problem using the URL constant instead of the DIR

Upvotes: 1

Views: 168

Answers (1)

PS Media
PS Media

Reputation: 46

Seems your code $init = new Thesis(); is called before plugins are loaded, so the action hooks could not be registered. Try to surround it with

add_action( 'plugins_loaded', function() {
    $init = new Thesis();
});

Upvotes: 2

Related Questions