Use Composer Package inside class - Namespacing error

I have my theme, with a folder called classes, with a single class:

wp-content
|- themes
   |- mycustomtheme
      |- classes
         |- FooBar.php

In FooBar, I would like to use this package here: Soundasleep / HTML2Text.

And use it like this:

In FooBar.php

  doSomethingBadass( $some_html ){
    wp_mail( '[email protected]', 'test', \Soundasleep\Html2Text::convert( $some_html ) );
  }

But whatever I do, I keep getting this error:

Fatal error: Uncaught Error: Class 'Soundasleep\Html2Text' not found in /app/public/wp-content/themes/mycustomtheme/classes/FooBar.php:60


My attempt

I assume that it's something to do with the namespacing.

I'm including FooBar.php by requiring the class in the functions.php.

How do I get past this? And if possible, then please link to a documentation or a place where I could have figured this out. I've been on this for 2 hours now.


Update

I'm including Composer with these lines inside functions.php:

if( file_exists( get_template_directory() . '/vendor/autoload.php' ) ){
    require get_template_directory() . '/vendor/autoload.php';
}

Topic namespace composer functions Wordpress

Category Web


Your code uses the conditional:

if( file_exists())

So are you sure you are using the correct path to autoload.php as it will stop if not found and you won't get the usual error thrown by require?

Personally I'd be expecting to see your vendor dir outside of the wordpress install.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.