I'm trying to use Composer with WordPress plugin

My source file starts like this:

<?php
/**
 * @package JpLists
 */
namespace Inc\Pages;

use WP_List_Table;

class ListTable extends WP_List_Table {

I end up with the following error in the browser when trying to extend a standard WordPress class:

Fatal error: Class 'WP_List_Table' not found in /srv/www/wordpress/wp-content/plugins/jpLists/inc/Pages/ListTable.php on line 9

This post suggests Composer + WordPress plugins is a bad idea

I'm just about to throw in the towel and go back to hard-coded require_once() calls, but the idea of namespaces & autoloading appeals to me.

my composer.json file looks like:

{
    "name": "jp/jp-lists",
    "description": "WordPress plugin",
        "type": "wordpress-plugin",
    "license": "GPL",
    "authors": [
        {
            "name": "JP",
            "email": "jp@example.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4":{"Inc\\": "./inc"}
    }
}

Composer's autoloader will take care of autoloading classes in your namespace (Inc). You have a problem with loading WP_List_Table, but this is not your class - it is WordPress core class and autolader from your project will not even try to load it (since it does not have any definition where to search it).

Also this class is marked as internal and WordPress documentation discourage from using it in plugins or themes. This is probably the reason why it is not available by default. If you really need it, you should require it manually:

if (!class_exists('WP_List_Table')) {
    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}

Or just copy it to your project under different name and use it, just like documentation recommends:

This class's access is marked as private. That means it is not intended for use by plugin and theme developers as it is subject to change without warning in any future WordPress release. If you would still like to make use of the class, you should make a copy to use and distribute with your own project, or else use it at your own risk.

https://codex.wordpress.org/Class_Reference/WP_List_Table

Kendrick Lamar - i (Audio), I definition, the ninth letter of the English alphabet, a vowel. See more. I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I?

It turns out if you just add a '\' before the name, PHP knows that you are referring to a class in global namespace and not the namespace specified at the beginning fo the file.

use \WP_List_Table;

I had abandoned my effort to use namespaces and composer for a couple of months, but after some diligent studying on namespaces, I decided to tackle the problem again and it was a simple fix.

I (Kendrick Lamar song), The ninth letter of the basic modern Latin alphabet. I (lower case ı). The letter i without a dot above, in both the upper case and the lower case versions. Translingual: ·The letter i with an acute accent.··water Definition from Wiktionary, the free dictionary

Please add the following to composer.json.

[example case: wp-content/plugins/example/src/composer.json]

"autoload": {
    "files": ["../../../../wp-admin/includes/class-wp-list-table.php"]
}

and Generating autoload files

composer dumpautoload

write into class file

use WP_List_Table;
class Test_WP_List_table extentds WP_List_Table {
.....

but i don't know use ABSPATH method


I am using Google Translate because I can not understand English very well.

I, I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I? Welcome back to Instagram. Sign in to check out what your friends, family & interests have been capturing & sharing around the world.

I, Nor am I talking about waking up one morning 18 years from now and ending my life through euthanasia or suicide. Since the 1990s, I have actively opposed  I have a PHP source file where  characters automatically got added in! I don't know from where they have come. I'm not getting any parse errors but it results in weird behavior in the execution

I, Log in to i-Ready®, online assessment and instruction that helps teachers provide all students a path to proficiency and growth in reading and mathematics. Log in to i-Ready®, online assessment and instruction that helps teachers provide all students a path to proficiency and growth in reading and mathematics.

Why I Hope to Die at 75, I-94 is a place for U.S. visitors to find travel records. Employment Eligibility Verification Department of Homeland Security U.S. Citizenship and Immigration Services Form I-9 10/21/2019 Page 1 of 3 START HERE: Read instructions carefully before completing this form. The instructions must be available, either in paper or electronically, during completion of this form.

Comments
  • Thanks for your suggestion. I had tried the code fix you suggested previously but ended up with the same error. As far as the class being private, right beyond the warning is this statement: Nevertheless, the WP_List_Table class has become widely used by plugins and WordPress developers as it provides a reliable, consistent, and semantic way to create custom list tables in WordPress. To date, no major changes have occurred in this class or are scheduled to occur, so testing your plugin with beta/RC phases of WordPress development should be more than enough to avoid any major issues.