It's a limitation of the WordPress hooks. If your method is inside a class, you need to pass an array with the instance of that class as a first parameter. Otherwise, WordPress expects a string that will use to call the `call_user_func` method of PHP.
Goal of the video (OOP)0:20 Review of previous video 0:28 Start of lesson 1:13 How to define/declare a class 1:15 _class ClassName_ _{_ _//methods_ _}_ How to create a class instance (the object) 3:20 _className= new ClassName();_ Before you create a class instance it is important to check if the class exists 4:35 _if (class_exists('ClassName')) {_ _$className= new ClassName();_ _}_ Explain the purpose of a constructor 5:45 Syntax of creating a php constructor 7:45 _function __construct(){_ _}_ 10:20 WordPress has inbuilt methods that are triggered during different parts of the lifecycle of a plugin ie : On activation On deactivation On uninstall 11:38 We will create our own user defined functions that will be called when the above methods are triggered _function activate(){_ _}_ _function deactivate(){_ _}_ _function uninstall(){_ _}_ 12:43 We will then link each of our user defined methods to the respective inbuilt method using the syntax shown below _register_activation_hook(filename, array($class_instance_name,’activation_function_name’));_ _register_deactivation_hook(filename, array($class_instance_name,’deactivation_function_name’));_ For the filename argument, we will use __FILE__ Magic Constant _register_activation_hook(__FILE__, array($class_instance_name,’activation_function_name’));_ _register_deactivation_hook(__FILE__, array($class_instance_name,’ ’deactivation_function_name’));_
The plugin generated 24 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.
At 15:55 I don't completely understand why the echo you put there will and actually does create the error. Since when would echoing from a php file not be possible? You seem to say it has something to do with (at which moment) the file header is ... read/sent? (if sent, then sent to where?) and the moment at which the activate function is called upon. That would seem to me like the function is called upon *before* the file is read by the wp system, but if that's the case then why the hell would wp call upon the function without first having read what is written inside the it?! And why don't the CPT creation and rewrite flush that you code in the following lesson generate errors? They're called upon in the same timespan (which is *within the activate function* ) as the echo here is ... ?:-/ Most likely my understanding is completely wrong, but please help me understand the order of things and the cause of the error correctly then. From what you tell about it in this vid I get no clue :'-(( Apart from that, I love this tutorial! I can code a bit but am used to coding at the back-end. I learn a lot from this channel :))
Hi Ale! What's going on ? Sorry, I really didn't understand what are the activation and deactivation hook and how they can be usefull for the building of my plugin! Can you kindly answer me ? PS Vedo adesso che sei italiano. Magari se rispondi in inglese, credo che qualcun altro possa beneficiarne. Grazie mille :)
hi, if you make the variable $alecadddPlugin inside the "if" statement above the register_activation_hook, how are you accessing it, shouldnt it only exist inside the if statement?
Recoverable fatal error: Object of class PluginName could not be converted to string in C:\xampp\htdocs\wordpress-plugin\wp-includes\functions.php on line 1909 in register_activation_hook(__FILE__, array($plugin_name, 'activate'));
*ALL your content in FIRE* - *_BRILLIANT!!_* I see this was years and years ago... what are you up to these days?! I can imagine you probably work for _Elon,_ _Mark...,_ _Bill..._ *_all three?!_* - Adam M.
Great tutorial. All of this is very new to me, so I am glad with what you teach. (Just one very minor, totally unimportant thing..... You talk about "uni-stall"m but it is actual "un-install". :-) )
All examples in the codex show opening curly braces inline. Never on a line by itself. -- make.wordpress.org/core/handbook/best-practices/coding-standards/php/#brace-style
Maybe better to wrap your class within a negate class_exist condition checking other plugins haven't initialized or declared similar name within scope? if(!class_exists('AlecadddPlugin')){ class AlecadddPlugin{
Hi! Thank you for your series . I am working in PHP for the last 1 year . From today i have started learning wordpress from your series of wordpress .Hopefully it will be helpful .
//How about this class SomePlugin { public function __construct() { register_activation_hook(__FILE__, array($this, 'activate')) ; // some others stuff ie: // Add Javascript and CSS for front-end display add_action('wp_enqueue_scripts', array($this, 'enqueue')); } public function enqueue() { wp_enqueue_script('js-name', plugins_url('js/somefile.js', __FILE__), array('jquery'), '1.0', true); wp_enqueue_style('style-name', plugins_url('css/somefile.css', __FILE__), null, '1.0'); } public function activate(){ } } //outside class you can instantiate this //you can also declare as global $skeleton whatever suit your need $skeleton = new SomePlugin();
hey,Alessand.you are so amazing ,so nice and patient to share the knowledge of WP.I was previous a c and c++programmer for 3years .But now I am loving web dev and started WordPress two month ago ,from building up wp website to child theme and theme and plugins.Logical and structured programming is ok for me but lacking so basic ideas of the wp.i have created several plugins which can functional perform well but I think they don't fit well with wp rules .your beginner's course is so helpful for me.if you don't mind ,I can get contact with you of some questions.Anyway please continue to do more.
Hi, thanks for watching. I'm glad my videos are helpful also for other developers that already know how to code these stuff :D If you want, you can come to the forum and write your questions there: forum.alecaddd.com Cheers
I see people complaining about AC talking about php in these videos. If you do not know basic php then these videos are not for you. AC pretty much states this in his introduction video as well as the beginning of this video that you will have a hard time following the series if you do not understand php. From the WP developer site "A WordPress Plugin is a program or a set of one or more functions written in the PHP scripting language." These videos are extremely helpful to us who know php and want a fast track to build WP plugins. Thank you for these videos.
Hello Ale ! great tutorial as ALWAYS!!! i have simple question : when you use register_activation_hook(__FILE__, array($alecadddPLugin, 'activate')); and you call the methos in the class using the array, could be doing using the PHP : register_activation_hook(__FILE__, $alecadddPlugin->activate()) ? thanks so much romy
Mh, I'm not sure it'll work. The second parameter needs to be a function name written as a string, or a class passed as an array. That's the way WordPress recognizes what you need to trigger. In your example, you're trying to pass the method from within the class, but not as a string. I don't think WordPress will be able to handle that.
oh wow ... quindi posso creare in contemporanea con l'attivazione/disattivazione del plugin ulteriori elementi (pagine, cpt, ecc) questo è fantastico ...
Nice tutorial, very well explained :D I have some experience with java in android studio, and so far, php seems quite similar to java. following this tutorial I ran into an error, and spent probably double the length of the video trying to troubleshoot it, then when I read the error message for the 10th time, and looked closer at my code, I relized I forgot one of the most important rules of code: Don't Forget The Semicolon; the error was wordpress didn't recognize }, because I forgot to put a ";" after the command "echo $string" once I fixed it to "echo $string;" it worked as expected :D
Im just a beginner and wonderign what software you're using to code? It would really help me a lot with the software you using since it has autocomplete feature.
I wish this covered more about plugins and not PHP. If people don't know basic PHP or object oriented concepts, they should go to a PHP tutorial first. A video on WordPress plugins should be only about WordPress plugins. I understand some off-the-cuff things, but there is too much irrelevant information. Either way, good video. Just find it a tad frustrating. I am specifically referring to the explanation of things such as code-convention such as the talking of camel case, and passing arguments, etc.
Yeah, I see your point. I'm sorry for the frustration, but I decided to add this portion to the initial videos because lots of users asked for it and were confused by the OOP structure. You can watch the videos at 2x speed and quickly reach the part where we start coding the real plugin. Happy Coding
So I already have a PHP script that I want to turn into a Wordpress Plugin. I didn't use OOP when I wrote the code for my script so it would be nice to see how you set up the activate(), deactivate(), and uninstall() in a procedural way. Thanks for making these videos. EDIT: It turns out since I'm using AJAX in my script I have learn a lot more about how wordpress handles ajax requests
What use is the` if ( class_exists...` part? It's defined just above so it definitely does exist, no? It would make more sense to only create an instance if the class DOESN'T already exist, wouldn't it?
This is awesome. I'm curious though. There is a boilerplate plugin generator by Devin Vinson and Tom McFarlin (wppb.me and wppb.io for boilerplate)... these guys do not use OOP for activate/deactivate by the looks of it. Those guys are pretty heavyweight so I was curious what your thoughts are on their way of doing things. Ta.
wpbb uses OOP for activate, the method is static. I went through all the different options and possibilities you can use in future videos. Personally, I think you can go the way you feel comfortable and how you think the code looks cleaner and easier to read. Happy Coding
haha! that makes sense. I'm on part 10 now and saw your bit on static methods. makes sense. thanks for the awesome tutorials. i watched your premium wp theme one quite a while ago and it was also brilliant. for me you are teaching at the perfect level of detail.
Hi Alec, I just tried the code but I cannot see the post type name showing in the dashboard menu. I click on activate but the Books does not show in the dashboard.
You have to change the unique name of the folder, as well as the file, name of the plugin, and if you have some unique functions with a specific prefix name, also those.
but want about composer namespace ? Do I need to change composer ( Inc ) to any other name . Bkz I have created a customised plugin seed and your seed is in the same site but when I activate my plugin it just repeats your seed 2 times . Have a look . Create another plying with same directory structure in your root plugin dir and active booth see what happens . If you want I can create a short video regarding to this issue and post you on fb or some other social site
function activate() called BEFORE we send the headers with WP redirecting by clicking the Activate button in the Console, not AFTER the header was sent, man. If the function with 'echo' was called AFTER so there would be no mistake at all. For example: echo "Something"; // echo was called before header(); // the mistake in this line!
The actual recommendation of the codex: "Class names should use capitalized words separated by underscores. Any acronyms should be all upper case." -- make.wordpress.org/core/handbook/best-practices/coding-standards/php/#naming-conventions
Hi Alex! It's a thrill to see your new tutorial series! I'm sorry to catch up so late! ANYWAY! Would you plz, tell us how to develop a plug-in to organize Wordpress uploads into different folders, based on file extension, size, etc. ?
Hi, thanks for watching. I wouldn't recommend trying to achieve something like that. WordPress organises the files in year/month based folders, and you shouldn't hack the default folder structure or you risk to break your installation.
I am at the biginning of tut, I see two issues, you should wrap the class definition and instatiate the class inside the if class_exists otherwise there is risk of collition with other pluging, and you dont pass undefined $vars to the $hooks.. I love this tuts.
The WordPress hooks and actions don't like calling OOP classes directly as an attribute, but it requires an array with the class and method as separated parameters. Not sure if it was changed recently.
Part 3: Basic PHP object oriented programming - creating and initializing classes and passing parameters with construct method - built in wordpress functions and hooks - activation, deactivation.
I am enjoying the tutorial very much but just as a tip, it would have been much more useful if you didn't show us 3 ways of doing something that will cause an error first before just showing us what the correct way is. You do a very good job IMO of explaining OOP overall however.
That's kinda my way of teaching. I like to show the most common mistakes an inexperienced developer could make and the issues he could stumble upon. Personally, I find it very insightful and useful, I'm sorry if it doesn't resonate with you or makes the tutorial a bit boring :D Thanks for watching anyway, and always Happy Coding
@@alecaddd although i understand Carl's point, i really appreciate your way of teaching, alessandro, because such a huge proportion of coding is trying to figure out what the heck you have done wrong and why things won't work. the more things you know in advance about why things fail, the better and faster one can code.
@@mithoomiyaan2534 the problem is that you have a typo, it's "register_activation_hook" and not "register_activtion_hook" you're missing the letter a in activation
if you install a plugin on multiple sites and we release an update for the plugin, how would multiple websites get an update in the wordpress admin to update the plugin?
If the plugin it's installed via the official plugin repository of WP, you just need to update the version number and the user will get the notification. If the plugin it's private and not released in the WP repository, you can handle this problem in a couple of ways. Code a built-in trigger to check once a week the version of the plugin against a remote repository, or add a button in the admin area to "Check for Updates"