WordPress Plugin Development - Part 10 - Namespaces and Composer Autoload

preview_player
Показать описание
:: Become a Patreon ::

:: Join the Forum ::

:: Support Me ::

:: Tutorial Series ::

:: My Website ::

:: Follow me on ::
Рекомендации по теме
Комментарии
Автор

I watched a 30 minutes tutorial for the terminal before going back to this tutorial to install composer. Don't lose hope guys. it's easy as long as we try to learn it, ain't that why we're all here?

KaminariGame
Автор

When passing static function in hook, using the 'use Inc\ClassName' then passing array( 'ClassName', 'function_name' ) is not work.

However, array( 'Inc\ClassName', 'function_name' ) is working.

It seems that the 'use' keyword is not passed along in callable.

christopherjaya
Автор

Your tutorial is already 6 years old and still, all the procedures are working as if WordPress and PHP are not updating. That is the advantage of PHP and WordPress CMS compared to other software. Overall your tutorial is amazing. Just Part 10 of the video and I am already drowning with new techniques in WordPress plugin development. Thank You!!😅

deogenesabejuelajr
Автор

10:34 - I am able to do every step UNTIL I get to this part. Whenever I add 'namespace Inc', to the activate.php file, I get the following error.

'Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, class "TemplatePluginActivate" not found in Stack trace: #0 WP_Hook->apply_filters('', Array) #1 WP_Hook->do_action(Array) #2 #3 {main} thrown in on line 292'

I don't know how to fix it at this point, so i'll just skip using namespaces and come back to it later.

I was able to figure out that using 'namespace Inc' works only in the original php file. I am just not able to use it in any other file.

farringtonworks
Автор

Very good. I hah implemented the autoloader mannually, but with your approach it is much better, and now we can use other packages and libs. Thanks.
PS: Maybe the type should be `wordpress-plugin` to follow wp conventions.

Magenteiro
Автор

Great stuff, second time through and it's getting clearer. PHP is so different than Java, but thanks to your tutorials I'm figuring it out! Thanks!

bitsinmyblood
Автор

Hello Alessandro,

first I have to say I love your video tutorials and the autoloader is a great tool. I love it.
But I maybe have a luttle tiny thing to point out to you and your followers.

Don't stop what you are doing! :)

BlankWolf
Автор

Alessandro, your videos are very good and they've helped me a lot in the past. Thank you. Unfortunately, I don't I agree with this one. Using composer inside plugins is a very bad idea as a lot of plugins can have several same dependencies which will be downloaded for each of them. Also, the should not be committed to git. The production build will also include each plugin's dev dependencies. Using Namespaces such as "Inc" is very bad too. Also these are not global namespaces but simply available to a request which has been passed through the autoload file in advance of it being used. Composer is a great tool, every PHP project I work from starts with composer init but, this is Wordpress and dependencies should also be plugins. There's never nothing stopping you from building a vanilla PHP (for everyone to use everywhere) composer package with the desired logic and then build a WordPress plugin around it. Regarding autoloading your source code, you can easily build a small function to autoload and even cache the namespaces. Composer does a lot more than just autoloading the source directory and none of these can be used correctly in a wordpress plugin with the context of an entire WordPress project. I.e. more than one plugin, a custom theme and dependency management in between them. Sorry for the long note. I wanted to be as constructive as possible. PS: Looking forward to your new Gutenberg tuts :)

keithmifsud
Автор

Thanks for sharing this great knowledge! Can I use composer on my production websites? Thanks

jvitorcarvalhomg
Автор

Thanx very much for this ... but this works great on my local dev env but gets a fatal error when I push this to remote hosting ... any idea why?

htmlfivedev
Автор

I love all your tuts, but I have to say introducing composer at this stage is not ideal for me, I had issues installing it and I felt like it was over complicating the process. I am just starting to learn about plugins and wanted to keep this a purely organic experience without using other software. I am more interested in doing it the standard way before improving the plugin.

draganovich
Автор

amazing! these are really high-quality videos! thx man!

thesloveniaboy
Автор

Interestingly it gives following error IF Activate class is directly called inside of register_activation_hook. Gives this: The plugin generated 214 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin. HOWEVER if I use register_activation_hook as you described it doesnt give error!

sevdamurat
Автор

Important note: Namespaces work with PHP 5.3.0 and above. If you are planning to release your plugin to the public, you will have problems with namespaces. I know - from experience - there are a lot of websites that use really old versions of PHP.
Just keep that in mind.

divikingdom
Автор

Hello Alessandro. Your videos are amazing. I am enjoying learning your videos. I am facing a problem Please help me out in 1:54 track. I've installed composer but unable to open that black window file. Please guide me. without this info i cannot proceed this lecture. Thanks in advance

mithoomiyaan
Автор

can i copy the autoload code into a file and use it without composer ?
or do i have to modify the autoload code for that?

nekopath
Автор

Very very good tut even it's from 2017. One of the best out there. Anyways until this part 10 - using phpcomposer in this early development state is a bit confusing - wouldn't it be more educational if you just continued with the native wp code? Of course I think I understood the namespace topic and that it really helps you to make your code more readable. Maybe this would be a nice second edition to clean your code with phpcomposer.

aarensan
Автор

This autoload method is working in windows but when I uploaded to production server which is linux I am getting class not found error. Can you please help me solve the issue

abhilasha
Автор

Can you just use Inc for the namespace when the folder is named inc in lowercase?

tomdevisser
Автор

What if I don't want to use composer for plugin development?Bro it's very confusing & I don't know about how I write next tutorial's code lines without using namespace/composer.It's difficult to going to next parts.

secrettalent
join shbcf.ru