WordPress Plugin Development - Part 8 - Static Methods and Multiple Files

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2025

ความคิดเห็น • 109

  • @HamzaSaleem-x2o
    @HamzaSaleem-x2o ปีที่แล้ว +2

    I have been a developer for more than 5 years , havent seen any tutorials better than these.
    Thanks so much

  • @delower186
    @delower186 7 ปีที่แล้ว +24

    You are simply the best of I have ever watched tutorials of wordpress development and eagerly wait and check when your next video will be uploaded :) Carry on man :)

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว +3

      Thank you so much man, I really appreciate it.
      You can activate the notifications to never miss a video.
      Cheers

    • @delower186
      @delower186 7 ปีที่แล้ว

      Thanks man, I turned it on :)

    • @rominhalltari3785
      @rominhalltari3785 6 ปีที่แล้ว

      man

  • @timwitort5724
    @timwitort5724 6 ปีที่แล้ว +10

    Great videos with good Wordpress and PHP info. Thanks for sharing these. Just one correction. I'm sure it's because English is not your native language, but when you create an instance of a class, it is not called "initialization" as you refer to it. It is called "instantiation". There is a difference and I don't want viewers to get confused. Keep it up!

    • @alecaddd
      @alecaddd  6 ปีที่แล้ว +9

      Thank you so much for the clarification, I pinned this comment at the top so other users will see it first.
      "instantiation", I learned something new today 😄

  • @klaasdeeindbaas
    @klaasdeeindbaas 5 ปีที่แล้ว +7

    Ever since you remove the SecondClass at 0:49, your custom post type 'Books' doesn't get created anymore. It never appears back in the menu upon activation of you plugin during this lesson. However, in the following lessons it does appear. Where in your toturial do you restate the call upon *add_action('init', array($this, 'custom_post_type');* which is currently hiding in the uncalled *function create_post_type()* ?

    • @artems3566
      @artems3566 4 ปีที่แล้ว

      I also lost 2 hours because of this problem :-(

    • @KaminariGame
      @KaminariGame 2 ปีที่แล้ว

      not sure but I think you want to make sure that the function custom_post_type() is called within the activate function
      function activate() {
      // generate CPT
      $this -> custom_post_type();
      require_once plugin_dir_path( __FILE__ ) . 'inc/lwdsplugin-activate.php';
      // flush rewrite rules
      flush_rewrite_rules();
      }
      don't quote me on that though. I'm still learning some of the stuff. hahahaha

    • @StefanSchmalhaus
      @StefanSchmalhaus 2 ปีที่แล้ว +1

      Just add this constructor again:
      function __construct() {
      $this->create_post_type();
      }

  • @mortezaaboudi2642
    @mortezaaboudi2642 6 ปีที่แล้ว +1

    you are one of my best teacher that i had.

    • @alecaddd
      @alecaddd  6 ปีที่แล้ว

      Thank you so much :D

  • @MrForund
    @MrForund 7 ปีที่แล้ว +2

    Thank you for the amazing tutorial. Waiting for series to continue!

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว +1

      +Ilya Nice thanks for watching! I'm working a lot on an app and it's sucking all my free time. I should be able to publish a new video soon

  • @nurakhtar8332
    @nurakhtar8332 3 ปีที่แล้ว

    Feeling Great!! Learning really interestingly.

  • @bdkoder
    @bdkoder 4 ปีที่แล้ว

    Skills growing bit by bit.
    Thank you.

  • @soultouchingsongs
    @soultouchingsongs 5 ปีที่แล้ว +2

    Thank you for this amazing tutorial. It's so valuable.

  • @luiggidevelop9945
    @luiggidevelop9945 7 ปีที่แล้ว +1

    Thanks man! your tutorials are the best! I can't wait for the next one!!!

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      Thank you so much. I should be able to get back with this series from next week, sorry for the long wait.

  • @PhungkhaBrahma
    @PhungkhaBrahma 4 ปีที่แล้ว

    Thank you Alessandro. The Best tutorial.

  • @ChrisVomRhein
    @ChrisVomRhein 6 ปีที่แล้ว +4

    If you encounter problems with concatenating plugin_dir_path with the subsequent path- and filenames due to different usage of / and \ under windows, try this:
    $path = untrailingslashit(plugin_dir_path(__FILE__));
    require_once join(DIRECTORY_SEPARATOR, array($path, 'inc', 'cvr-tutorial-plugin-activate.php'));

    • @j8ahmed
      @j8ahmed ปีที่แล้ว

      Apparently plugin_dir_name() is a wrapper for trailingslashit( dirname( $file ) );. So we can just use dirname(__FILE__) as well

  • @DanielStopka
    @DanielStopka 7 ปีที่แล้ว +1

    Hi, thank you for showing how will oop work in the plugin, otherwise I would be lost in future parts of this tutorial... good job

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      Thanks for this. Many others complained about this little digression into OOP, but I find it necessary in order to continue the work on plugins and fully understand their structure.
      Happy Coding!

  • @michelegiordano4337
    @michelegiordano4337 7 ปีที่แล้ว +1

    You are great! Great tutorial! I saw more of your video and I learn more. Can i say what font you use in your editor? I like it.

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      Thanks for watching, I'm glad you like my tutorials.
      I'm using Fira Code.

  • @AbdusSalam-tx3fq
    @AbdusSalam-tx3fq 5 ปีที่แล้ว +1

    Really your a great teacher.

  • @8BlankWolf8
    @8BlankWolf8 7 ปีที่แล้ว

    Hey Alessandro and Viewers,
    in a previous comment I already showed a way to use the hooks without an array. I know that this video came out earlier then my comment, but I want to point at it in a directer way.
    What I showed you was the usage inside the constructor, but it also works outside a class. You also can use the static method of a class.
    register_activation_hook(__FILE__, $alecadddPlugin->activate);
    register_activation_hook(__FILE__, AlecadddPluginActivate::activate);
    Notice that I used the function without ().
    When you use the () the function would be called and what ever returns will be injected into the register hook. But without () you'll refer to the function directly and that is what the register hook actually needs to call the function.
    Sure. The method provides a way to use an array but he will then use some magic functions from PHP (yes there a called magic functions) and that would always be slower then to call a function directly.
    But as always: Use the way you personally prefer.
    Greetings BlankWolf

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      Sorry for the lack of answer, I totally missed this comment.
      I tried your suggestions and I got errors in both cases.
      register_activation_hook(__FILE__, $alecadddPlugin->activate); returns
      Notice: Undefined property: AlecadddPlugin::$activate
      register_activation_hook(__FILE__, AlecadddPluginActivate::activate); returns
      Fatal error: Uncaught Error: Undefined class constant 'activate'

    • @8BlankWolf8
      @8BlankWolf8 7 ปีที่แล้ว +1

      You're right ... I mixed that up with js ... :O

  • @sudhakarsudha9655
    @sudhakarsudha9655 7 ปีที่แล้ว +2

    its super explain about oops and step into plugin development its more helpful for my career

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      Really happy to hear that, thank you so much :D

  • @sagarwalzade5617
    @sagarwalzade5617 2 ปีที่แล้ว

    Good stuff man!
    But I dont understand why that header not sent error comes and how to get rid of it???

  • @scyre3190
    @scyre3190 4 ปีที่แล้ว

    Thank you for your good work! Keep it up :)

  • @prahasasa
    @prahasasa 7 ปีที่แล้ว +1

    Great tutorial like always...Please, is it necessary to write WP plugins in OOP, or does wp codex alow plugins without OOP? Thanks..

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว +1

      Yes, it is. Due to the peculiar structure of a plugin, WordPress recommends to use OOP to avoid conflicts with other plugins and theme options.
      Don't worry tho, if you keep watching my videos, you will grasp the basic concepts of OOP and you'll learn how to build anything.

  • @zakariabokhtiar4157
    @zakariabokhtiar4157 4 ปีที่แล้ว

    Gd tutorials man. Like u and ur tutorials also

  • @delower186
    @delower186 7 ปีที่แล้ว

    Hi, we are eagerly waiting for your next video of this series ):

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว +1

      I should be able to get back with this series from next week, sorry for the long wait.

  • @RohitSaini-qg2xh
    @RohitSaini-qg2xh 7 ปีที่แล้ว

    hi alessandro how you doin. i would really appreciate if you make a plugin with max possible add_filter hook used to change things, like in some cases woocommerce or may things which are hard to look out and possibly we can use filter to reset according to our wish..basically understanding the hook properly

  • @xphis0528
    @xphis0528 ปีที่แล้ว

    Thanks great video. I do not know if this is a PHP thing, but it feels strange to see a function "activate" being invoked from another function called "activate".

  • @akobey
    @akobey 7 ปีที่แล้ว +1

    Nice tutorials thank you.

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      You're welcome

  • @kevin_neugebauer
    @kevin_neugebauer 2 ปีที่แล้ว +1

    great tutorial:)

  • @santa6379
    @santa6379 2 ปีที่แล้ว

    really helpfull tutorial!!

  • @antoninog4976
    @antoninog4976 5 ปีที่แล้ว

    Allessandro, are you willing to teach how to create a plugin like VideoWhisper? That would be great. :)

  • @sudhakarsudha9655
    @sudhakarsudha9655 7 ปีที่แล้ว +5

    Hi waiting for the next videos master

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      Yeah, you're right. Sorry for the delay, it's a pretty busy period and I'm trying to record new video this week.
      Thanks for your patience.

  • @rolenlambertcordero9717
    @rolenlambertcordero9717 5 ปีที่แล้ว

    Really Helpful! BIg Thanks.

  • @joseimaz1
    @joseimaz1 4 ปีที่แล้ว

    Hi, thank you so much by the tutorials. Is it not neccesary include
    if(!defined( 'ABSPATH' )) {
    die;
    }
    in AlecadddPluginActivate and AlecadddPluginDeactivate?
    Thanks in advance

    • @TheClaumigue
      @TheClaumigue ปีที่แล้ว

      Non è necessario includere quel codice in ogni file del plugin, solo nel file principale che contiene i dati del plugin e l'esecuzione principale.

  • @writingerrands5676
    @writingerrands5676 4 ปีที่แล้ว

    Can you use 'self ' to call static method inside a class? I mean just substituting $this with self

  • @pyrotechnick420
    @pyrotechnick420 6 ปีที่แล้ว

    15:29 The custom post type stopped worked working

    • @alecaddd
      @alecaddd  6 ปีที่แล้ว

      This part is just an intro showing different approaches and methods. You're gonna start coding the rela plugin later in few videos.

  • @manualvarado2212
    @manualvarado2212 6 ปีที่แล้ว

    When calling the activation hook outside of the class, i am getting a headers already sent of 260 characters ):
    However, by wrapping the requiring and invocation of the activate method inside of the plugin's class, as you explained:
    class CutiePlugin
    {
    public function register()
    {
    add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
    }
    public function activate()
    {
    require_once plugin_dir_path( __FILE__ ) . 'inc/cutie-plugin-activate.php';
    $this->custom_post_type();
    CutiePluginActivate::activate();
    }
    public function deactivate()
    {
    require_once plugin_dir_path( __FILE__ ) . 'inc/cutie-plugin-deactivate.php';
    CutiePluginDeactivate::deactivate();
    }
    }
    It does work properly!

  • @devilreaper8543
    @devilreaper8543 7 ปีที่แล้ว

    will there be tutorial for theme option like adding phone number, emails, logo etc, i have one but thats in php 5 which obviously doesn't wok with php 7, so need theme option that work with php 7

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      Did you check my Premium Theme Development series?
      I tackle everything you listed

  • @freebreakful
    @freebreakful 7 ปีที่แล้ว +1

    Is this tutorial series completed thanks

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      No is not, I'm releasing a new video later today.
      Cheers

  • @juliekikass
    @juliekikass 6 ปีที่แล้ว +1

    Merci Beaucoup !

  • @eng.alitaha
    @eng.alitaha 2 ปีที่แล้ว

    Hay, Keep it up!

  • @MohammadAbdulHyeShaon
    @MohammadAbdulHyeShaon 7 ปีที่แล้ว +1

    Alessandro Castellani >>>>>> great , great & great

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      Thank you so much :D

    • @ricardoambdev
      @ricardoambdev 6 ปีที่แล้ว

      Alessandro Castellani hi and tnx for all u have made for us! Is amazing this series and all stuff that u posted!
      I have a question for u... In my case the hook for activate the code is not working, for some reason all stuff inside the method that is called by the register_activation_hook don't work... I seek along the net by the answer without success... Do u know why is this happening!? Is something I overpasses?

  • @christopherjaya342
    @christopherjaya342 ปีที่แล้ว

    By the way, start from PHP 8, calling non-static function statically is not allowed anymore.

  • @olegkapkaev6911
    @olegkapkaev6911 5 ปีที่แล้ว +2

    post_type 'books' is disappeared! :D

  • @firededgeconsulting9317
    @firededgeconsulting9317 6 ปีที่แล้ว

    I am getting "The plugin generated 228 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." after the changes. I went to git repo and compared my code and still no go . i copied all the code from the repo and just changed filenames and still get error. any ideas?

  • @vigilancer
    @vigilancer 7 ปีที่แล้ว

    Okaaay.. Got quite confused, was wondering why the [$this->custom_post_type();] line of code doesn't go to activate method on the other file.

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      What do you mean by "the other file"?
      In the video, I'm calling that method in the parent extended class.

    • @vigilancer
      @vigilancer 7 ปีที่แล้ว +1

      Just realized the method [custom_post_type] / [create_post_type] was purposely not called in this video, double checked the results in the end that there is no [Book] link.

  • @dyanbonke
    @dyanbonke 6 ปีที่แล้ว

    Hi Alessandro. i got this ...but am sure i can fix it. _func_array() expects parameter 1 to be a valid callback, non-static method ::enqueue() should not be called statically
    class-wp-hook.php on line 286

    • @dyanbonke
      @dyanbonke 6 ปีที่แล้ว +1

      Foud the problem. i forgot to add action in function register method

    • @alecaddd
      @alecaddd  6 ปีที่แล้ว

      I'm glad you solved it

  • @sohanwp
    @sohanwp 3 ปีที่แล้ว

    best one

  • @nobir98
    @nobir98 7 ปีที่แล้ว +1

    another tut :D

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      And many more to come :D

    • @nobir98
      @nobir98 7 ปีที่แล้ว +1

      I know that ........ :D ...
      do you planing to do jQuery tuts and also javascript , php, mysql ???
      Thanks in advance :D

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว +1

      Yes to all of that :D

    • @nobir98
      @nobir98 7 ปีที่แล้ว

      cant wait to see these tutorial .

  • @sesvasiledaniel8523
    @sesvasiledaniel8523 4 ปีที่แล้ว +1

    Please add a Bitcoin address on support page. Thank you!

  • @alexkievforex
    @alexkievforex ปีที่แล้ว

    Thanks at least I understand my issues.

  • @gabrielpoupou1479
    @gabrielpoupou1479 6 ปีที่แล้ว

    Why are you showing stuff and then erasing it? This is super confusing!

    • @alecaddd
      @alecaddd  6 ปีที่แล้ว

      I did it to show different approaches and teach the how and why behind specific decisions.

  • @k3on
    @k3on ปีที่แล้ว

    WHY ARE YOU KEEP SAYING “METHOD” TO THE FUNCTIONS? NO GOOD. :)

  • @animeshsahu9937
    @animeshsahu9937 7 ปีที่แล้ว

    its really going to be too much lenthy due to oop cource in between, make another oop cource playlist instead. Show how to make plugin fully...........

    • @sudhakarsudha9655
      @sudhakarsudha9655 7 ปีที่แล้ว

      No its fault he is going in correct way its so helpful

    • @animeshsahu9937
      @animeshsahu9937 7 ปีที่แล้ว

      I know but for good developer who not know wp plugin development then for them its more lenthy

    • @sudhakarsudha9655
      @sudhakarsudha9655 7 ปีที่แล้ว +1

      No he trying to make beginners also to become a good developer

    • @animeshsahu9937
      @animeshsahu9937 7 ปีที่แล้ว

      What i said in main comment... to make another playlist for oop cource. not to involve everything at same cource. I'm not saying to remove this...

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว

      As I said in the previous video, I need to do a quick overview about OOP in order to continue the series. Fully understanding the basics of OOP is absolutely necessary to build a good plugin, and furthermore, follow along and understand this series.

  • @pyrotechnick420
    @pyrotechnick420 6 ปีที่แล้ว

    You should be more confident in your coding if you're going to be making these videos, it almost felt like you were trying to convince me to stop watching them lol

    • @alecaddd
      @alecaddd  6 ปีที่แล้ว

      What do you mean?

  • @JamesJohnAgar
    @JamesJohnAgar 7 ปีที่แล้ว

    It's turning into a OOP tutorial rather than a Wordpress Plugin tutorial, get back to concentrating on creating the plugin...

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว +1

      As I said in the previous video, I need to do a quick overview about OOP in order to continue the series. Fully understanding the basics of OOP is absolutely necessary to build a good plugin, and furthermore, follow along and understand this series.

    • @rickstewart3267
      @rickstewart3267 7 ปีที่แล้ว +1

      Great series - thanks for doing it. In my opinion add whatever you like to make everything make good sense, its better to over explain rather than going too fast and letting some people fall behind.
      BTW uninstall is pronounced un-install and not uni-stall... lol - just poking a little fun... Looking forward to the next installment!

    • @alecaddd
      @alecaddd  7 ปีที่แล้ว +1

      Thank you so much. And thank for the pronunciation tip, I'm still learning :P

  • @janszkylaszlo
    @janszkylaszlo 10 หลายเดือนก่อน

    A much cleaner approach is using spl_autoload_register and writing a little code for autoloading plugin files. Better to have at least a namespace for the plugin to avoid class naming collisions.
    spl_autoload_register(function ($className) {
    $prefix = 'Castellani1\\';
    if (substr($className, 0, strlen($prefix)) !== $prefix)
    return;
    $classNameWithoutPrefix = substr($className, strlen($prefix));
    $fileNameWithoutPrefix = str_replace('\\', DIRECTORY_SEPARATOR, $classNameWithoutPrefix).'.php';
    $fileName = plugin_dir_path( __FILE__ ) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $fileNameWithoutPrefix;
    require_once($fileName);
    });