One note: While debugging I discovered the root problem to the 'Books' Label not appearing in the left collum. At 5:36 when you fixed/added the 'label' parameter it appears you corrected a syntax error and removed the single quotes around 'true'. INCORRECT: register_post_type('book', ['public' => 'true', 'label' =>'Books'] ); CORRECT: register_post_type('book', ['public' => true, 'label' =>'Books'] ); Your training video has been a delight on a cold Saturday afternoon. I've always wanted to program a plugin for Wordpress and your video has made the process so approachable. I'm feeling much gratitude in being released from using other plugins to accomplish what I want.
Thanks John! I wasn't sure why it wasn't working for me. I was searching online and triple checking my syntax trying to determine what was wrong. Then it dawned on me to check the comments of the video and there was your answer. Exactly what I needed. Thanks again
*Video Summary* Review of previous video 0:20 Start Lesson 0:55 *Main goal:* Create CPTs and Flush rewrite rules What is a Custom Post Type (CPT): Simply put a CPT is a user defined WordPress blogpost Unlike ordinary WordPress blogposts, CPTs allow the developer to add his/her own custom fields thus making them more versatile. Good examples of CPTs are products and appointments. Flushing rewrite rules is used to reset the WP environment after making major changes that the new settings can be reflected in the WP environment *How to generate a CPT **1:50* Add a new method to your class that will handle all the user defined code for adding your CPT. In this case we have named it custom_post_type(). In the method make a call to the register_post_type() which as the name suggests will register the new CPT The register_post_type() function will take in 2 main arguments ->CPT Slug ->An array of additional features that further define your CPT e.g. its label etc. _function custom_post_type(){_ _register_post_type(‘post_type_slug’,array());_ _}_ *Hook our method to init* 3:00 We then need to ensure that our new function/method is executed when the plugin is being initialized i.e. when we are instantiating our class, this is done by calling our new function inside the constructor of our class since the constructor is always executed when a class is instantiated *Syntax* 4:10 _function __construct(){_ _add_action(‘init’, array($this,’function_name’))_ _}_ *Actual code* _function __construct(){_ _add_action('init', array($this,'custom_post_type'));_ _}_ *refresh the rewrite rule* 6:33 _flush_rewrite_rules();_ This can be executed on activate plugin function and the deactivate plugin function
It's amazing! When I first came across this series I had no idea what OOP PHP was or why I would be using it and stopped at part three. Now I'm building a more modular OOP theme and want my custom post type in a plugin and... BAM! This is _exactly_ what I'm looking for! Solid content as always, champ!
You can’t send classname in a static way, because it is not going to be same instance, so it’ll never be able to read same data, $this means current instance of object/class (I know video author know, but whoever didn’t get it this is the reason) and neither methods are defined as static. This video was autoplayed and decided to watch, I like how explain it, great job.
9:25 This method will trigger a fatal error in PHP 7.4, the recommended version of PHP that the current version of WordPress (5.6) is using. If you haven't run into this issue yet, you will once you update your version of PHP. This function is correct. custom_post_type needs to be called each time the plugin is initiated, not just when activated. function __construct() { add_action( 'init ', array( $this, 'custom_post_type ') ); } This next function, as written, is what will throw the error: function activate() { $this->custom_post_type(); flush_rewrite_rules(); } $this can no longer be used when not in object context. In addition, the action has already been called in the __construct function. This is all that is needed so far: function activate() { flush_rewrite_rules(); } Many thanks to the uploader for posting these clips. They are appreciated.
Hi, thx for the great tutorials.. pls grant me a question: at minute 9:40 you are calling the method custom_post_type() directly from AlecadddPlugin::activate() and propagading, that the registration of the CPT will now only be done, when the plugin will be activated and the callback function is called. But since you left the add_action hook in the constructor of the class nothing really changes, but to call the AlecadddPlugin::custom_post_type() twice. Can you pls explain why you are following this approach?
Whoop, yeah, you're right. I forgot about that hook. Don't worry too much about it anyway, because these lessons, up until 11 or 12, are made to give a quick overview of OOP, with different approaches and methods. After these videos, we're gonna start building the actual plugin with a proper structure. Cheers
Alessandro, you called 2 times register_post_type(). Both at 'init' and at ACTIVATE. You told that it is because it may not register at wordpress initialization. Did I understand it right? Could you explain it a little more. If registering at wordpress "init" then why register? If it works on "init" then why call again at activation?
OK, suuuuper bit noob mistake. I've been coding for years, but new to WP and PHP... The plugin was just showing as text on the dash board because... I had
Can you please answer the question to why you are running custom_post_type() in both the constructor and in the activate methods? It just looks like a bad practice.
Firstable, thanks for sharing your knowledge, I truly appreciate it. Aren't we calling the custom_post_type function twice in this example? once while the initiation through the init hook and again through the activation function?
The first 10-11 episodes of the series are just an overview of different methods and how to setup a plugin in OOP. We're gonna start coding the real plugin around video n.12, so yeah, some duplication or other stupid things can happen :D
@@yanafit376 I had the same issue. For me it was register_post_type('book', ['public' => true, 'label' => 'Books']);. I had single quotes around 'true' and there should not be any as it is a boolean datatype. Hope that helps
In my enveronment show this messege "The plugin generated 23 characters of unexpected output during activation. If you notice "headers already sent" messages, problems with feeds or other issues, try disabling or removing this plugin." how can I do fix this ?
after declaring add_action in constructor and custom_post_type in activate method. I went to my new generated plugin. I click on add new post put the data. after that i went to database to check the data that i have given in that new post form. guess what i did not find the data in post_type column in post table. so my question is where is my data in table? as you have describe in this video that should placed in database table.
i have created a cpt but i cannout get the front end to show the contents of the post i have initialized the has_archive argument to true but i cannot seem to show my custom cpt recent posts or my post content to be shown
While debugging I discovered the root problem to the 'Books' Label not appearing in the left collum. At 5:36 when he fixed/added the 'label' parameter it appears he corrected a syntax error and removed the single quotes around 'true'. INCORRECT: register_post_type('book', ['public' => 'true', 'label' =>'Books'] ); CORRECT: register_post_type('book', ['public' => true, 'label' =>'Books'] );
I want to ask if the custom post type is created twice with examples you used. i.e 1. first time the constructor is called during the instantiation of the class? 2. during the activation of the plugin class AlecaddPlugin { public function __construct(){ add_action( 'init',[$this,'custom_post_type']); } public function activate(){ $this->custom_post_type(); flush_rewrite_rules(); } //... public function custom_post_type(){ register_post_type( 'book', ['label' => 'Books','public' => true] ); } } if(class_exists('AlecaddPlugin')): $alecaddPlugin = new AlecaddPlugin(); endif; //activation register_activation_hook( __FILE__ , array($alecaddPlugin, 'activate'));
In this case yes, but WordPress doesn't create 2 post types, simply overrides the old one. Anyway, keep watching the series as these initial videos are just examples tho show some methodologies and different approaches. We won't keep this code
Hello, I'm trying to follow your tutos to create a simple plugin,but so far I haven't been able to achieve what I want, I'm trying to connect the front of my site with my plugin. Basically I have a button on the page and I'm trying to make that when you click it a simple function in the plugin is called, but so far It hasn't worked, do you have any resources that I could consult? THX
function custom_post_type() { register_post_type('book', ['public'=>true, 'label'=>'Books']); } function __construct() { add_action('init', array($this, 'custom-post-type')); } CPT doesnt appear to the left, anyone know why? Been trying to fix this for a week now, but can't wrap my head as to why it doesnt, does it affect the coding that I am using a webhotel?
how did you access the database at 10:19? Is it a special software you have to download? I've looked around and they direct towards cpannel, but I unfortunately don't have that
That's called Sequel Pro, it's MacOS SQL client to access local and remote DBs. There are various alternatives for Windows and Linux, or even online, like PhpMyAdmin.
Ciao Alessandro. grazie per il tuo lavoro. Does Wordpress delete the custom post type information in the database once the plugin is deleted/uninstalled as default? Thanks
why did you use both $this->create_custom_post_type(); --> in activate function and add_action('init',[$this, 'create_custom_post_type']); --> in construct function
This question is kind of off topic. There is this cool plugin which is made for the admin dashboard only. Do you know what kind of hook I can use to make the plugin appear for all users/public. As in add the plugin to the menu for all to see and access.
Hello Alex, I have a question. When I use register_post_type ('book', array('lable' => 'Books', 'public' => 'true')), it seems not be working over here. However, when I assign the array to $args, and use register_post_type('book', $args), it works well. So what is the different?
I do not get it cause at the end we code 2 times the activation of posttype... one in constructor the other one is on activation ? so we are making a double CaLL ?
Alessandro, what function would someone use if the plugin they are creating was for accessing the files in the media library? I'm trying to create a visual effect on the thumbnails.
Hey Alessandro... I m learning with your tutorial. But this tutorial can't work in my case... the Book (Custom_post_type) do not show in my dashboard. How can I resolve this. Or how can I check the error. please help me.
This is great tutorial indeed, I rarely found a youtube videos creating plugin tutorial with OOP approach. Hope you can make a tutorial videos on how to create an extension/plugin for woocommerce using OOP. I hardly found on youtube a video like that. Anyway, this videos is what i'm looking for, Thank you very much.
I thought that $this refers to the object instantiated by the class, but the presenter says that it refers to the class. Please help me understand. Thanks
The official statement says that "it's a reference to the current object", and since the object it's the class, it's a reference of the class. Different ways to say the same thing :D
Hi Sir, I have created the simple plugin using your part 2 & part 3 video and while clicking of the Activate button getting the below error. "Plugin could not be activated because it triggered a fatal error. " Even enabled the debug mode getting the same error even after increase the memory limit. Can you please help me on this.
This is confusing.. You put the register post type function inside the constructor. Does'nt this calls this function every time its instantiated , regardless of activation/deactivation?
Are you sure that we need to call this->custom_post_type() inside 'activate' method? Each time we activate our plugin we get a redirection to a new page, which means that our plugin will be executed and custom_post_type() will be called on 'init' hook. So, your advice doesn't make sense.
This is just an example of how you can achieve something, it's not the golden standard, and if you think there's a better approach, go for it. Anyway, until episode 12, these videos are just for introduction and generic overview of how WordPress manages plugins. The real structure and actual plugin will be build later.
Hey! Great Series! Super happy to learn this :) But when i try to create the custom_post_type it wont create it, and i can see the code on the top in wp. The showin code starts from when we set the public => true. Any clue? :)
He changes the type of public from a string to boolean during a camera cut around 5:45. He changed 'public'->'true' to 'public'=>true by removing the single quotes. He doesn't mention this in the video. If you go forward a bit, you'll see he made the correction at some point.
good job Alessandro, can you share the code because i write the same code but nothing new when activating the plugin, and i get an 2834 error note in plugins page.
I didnt watched whole video but have question. There are deact act unins methods. What's they function ? When we are deactivating or activating plugin they are already doing by wordpress itself am i wrong ? (when we deactivating or uninstalling this plugin CPT already deleting by wordpress)
complimenti per la splendida serie di tutorial, personalmente ho un problema con la visualizzazione del cpt, che non mi compare nel backend nonostante abbia ricontrollato piu volte il tuo listato e ricopiato in maniera pedissequa, non visualizzo errori ne il custom post type, magari è cambiata la sintassi con le nuove versioni di wp? utilizzo wp 5.9 installato in locale con wplocal, qualche suggerimento... Grazie in anticipo congratulations for the wonderful series of tutorials, personally I have a problem with the display of the cpt, which does not appear in the backend despite having rechecked several times your listing and copied slavishly, I do not see errors nor the custom post type, maybe the syntax has changed with the new versions of wp? I use wp 5.9 installed locally with wplocal, any suggestions ... Thanks in advance
i have an error even i have exactly same code written as your tutorial , i have error saying when i activate the plug in Plugin could not be activated because it triggered a fatal error. could you please advise ?
Is there anything changed on WordPress now? because your tutorial is fine, but register_post_type( 'Book', ['public'=>true] ); Here the book is not showing when I activate the plugin. Instead of that another custom post is created which is also named "posts" , In easy words this is creating a new custom post with the default name "posts"
Hi Alessandro, On the same note of custom post type, I added metaboxes with rating and want to allow comments on the post type. So I want to make the site allow users to comment(review) and edit via front end without accessing the dashboard. In case it is too much for a tutorial, can you explain how to make that happen?
if(class_exists('Alecadddplugin')){ : Only works here when plugin is activated. shouldn't this condition be true as far as class Alecadddplugin is defined? regardless of Plugin Activated / Deactivated. Can anyone please explain this to me?
Nor good or bad, it all depends on what you want to use and how you want to. I find Singletons a bit limiting and somewhat complicated, but nonetheless are super helpful if you want to avoid unwanted mutations and keeping some logic under a class that can't be init more than once.
when you write $this inside a class you a referring to the current instance of the class, when you write $this->method(), you are calling the method on the class instance and returns the result. If in this case your method doesn't return anything (what's known as a void method) then you would actually be passing null to the add action argument. however when you pass and array and say $this as one argument, you a passing the instance fo the class as the first argument and then a string which is the name of the method as the second argument. So in short you want to tell the add_action method the name of the method it should call, you don't actually want to call it yourself.
When created the function of custom post type and adding the action, it is giving this error, and thats why the label books is not showing in side bar: Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'ahmedplugin' does not have a method 'custom_post_type' in C:\xampp\htdocs\plugintutorial\wordpress-5.2\wordpress\wp-includes\class-wp-hook.php on line 286
please if the book post didn't appear for you maybe you should use this method it worked for me class TestPlugin { function __construct() { add_action('init', 'custom_post_type'); } function activate() { //flush rewrite rules flush_rewrite_rules(); } function deacivate() { } function uninstall() { } function custom_post_type() { register_post_type( 'book', ['Public' => true, 'label' => 'books'] ); } }
Your code is wrong. I showed in the tutorial that you need to pass the array with the instance of the class in the add_action() method. Also, 'public' should be written lowercase.
First of all, I wanna thank you for all your videos, I have learned (And i'm still learning) a lot about wordpress thanks to you. I looked for the function "flush_rewrite_rules" in codex and I found that it takes an expensive operation so it should only be used when absolutely necessary (codex) My question is : Should we do the same thing that we have in the example in this link : codex.wordpress.org/Function_Reference/flush_rewrite_rules I mean, create a function that register the CPT and flush rewrite rules then call it in deactivation and activation Is it OK to call the registation of CPT in deactivation ? I Hope my question is clear, and sorry for my english.
Hi, thank you so much for watching my videos. Yes, the flush_rewrite_rules() should be used carefully. If you keep following my videos, you'll see that I actually transfer it from the simple activation of the plugin to only being used when creating or deleting a custom post type from within the plugin. You don't need to call again the registration of the CPT on deactivation, just have the flush_rewrite_rules() there to deal with it. Cheers
thank you very much - i love it. 'It would be a great pleasure if you create more vids. you have helped me alot. if you come up with more vids - you probably would attract many many ppl - wordpress user and fans from all over the globe
Thanks for the videos, it helps a lot, but when using OOP the methods (functions) in the class shouldn't have the accessor keyword (public/private) it will be more correct.
I'm truly surprised to see there are 2 thumbs down on this video ... this baffles me! What kind of people would thumbs down this kinda awesome training ... my guess, they must be mentally challenged! In that case, my apologies ...
I'm getting a syntax error warning for the custom_post_type function in Dreamweaver. I'm not getting any warnings or errors when I test it though. Has anyone else had this happen? Maybe because it's CS5 they haven't updated their code to include wordpress stuff? or maybe I've just typed something wrong and can't see it haha Here's the code, within the class. I've named it ACOrder instead of Books. function custom_post_type(){ register_post_type('acorder', ['public' => true, 'label' => 'ACOrder']); } Thank you :)
your videos are dope!! one of the best in the world at learning to be a wordpress developer, as i'm on my way to that!! Thanks a lot for this excellent material dear Master :) The only critic is that you don't enlarge your screen enough while you are coding, it is short and not that easy to see when i wish to code live while u do!! Pliz could you do a special tuto on how to build a plugin with the kind of dashboard woocommerce has, especially at the level of the Orders or Commands of products: because i wish to know how you can add each product with its quantity, in the same command... i don't know if they do that with jquery or vanillajs... it will be dope you have time for this big challenge for me (Wordpress + jquery or vanillajs) May Jesus bless you for your gift :)
Thanks for watching my videos. I thought my resolution was big enough, sorry for the issue, I'll try to increase it a bit more. Keep watching the plugin series as I'm building a complex administration area with repeater fields, custom sections, and adavanced options. Cheers
"Keep watching the plugin series as I'm building a complex administration area with repeater fields, custom sections, and adavanced options." "sorry for the issue, I'll try to increase it a bit more." Thanks in advance for answering my queries... Cheers man
Hey, if anyone wants to know a little more about rewrite rules in wordpress, check out this blog post www.pmg.com/blog/a-mostly-complete-guide-to-the-wordpress-rewrite-api/ More wordpress posts from this site www.pmg.com/blog/tag/wordpress/
i did that. well nevermind i love your teachings keep the good work, i want to write a plugin about tracking , where i can create a tracking id with map and a short-code to display a tracking form so that when a user enters a tracking id in the front end it will display the tracking details with the location under it.
Keep watching this series and you'll learn everything you need. I tackled the creation of shortcodes, storing dynamic data, and more good stuff. Cheers
One note: While debugging I discovered the root problem to the 'Books' Label not appearing in the left collum. At 5:36 when you fixed/added the 'label' parameter it appears you corrected a syntax error and removed the single quotes around 'true'.
INCORRECT: register_post_type('book', ['public' => 'true', 'label' =>'Books'] );
CORRECT: register_post_type('book', ['public' => true, 'label' =>'Books'] );
Your training video has been a delight on a cold Saturday afternoon. I've always wanted to program a plugin for Wordpress and your video has made the process so approachable. I'm feeling much gratitude in being released from using other plugins to accomplish what I want.
Thank you so much
Thanks John! I wasn't sure why it wasn't working for me. I was searching online and triple checking my syntax trying to determine what was wrong. Then it dawned on me to check the comments of the video and there was your answer. Exactly what I needed. Thanks again
thanks
@@DanielTriesLife thanks to all
Thanks for that one!
*Video Summary*
Review of previous video 0:20
Start Lesson 0:55
*Main goal:* Create CPTs and Flush rewrite rules
What is a Custom Post Type (CPT): Simply put a CPT is a user defined WordPress blogpost
Unlike ordinary WordPress blogposts, CPTs allow the developer to add his/her own custom fields thus making them more versatile. Good examples of CPTs are products and appointments.
Flushing rewrite rules is used to reset the WP environment after making major changes that the new settings can be reflected in the WP environment
*How to generate a CPT **1:50*
Add a new method to your class that will handle all the user defined code for adding your CPT. In this case we have named it custom_post_type(). In the method make a call to the register_post_type() which as the name suggests will register the new CPT
The register_post_type() function will take in 2 main arguments
->CPT Slug
->An array of additional features that further define your CPT e.g. its label etc.
_function custom_post_type(){_
_register_post_type(‘post_type_slug’,array());_
_}_
*Hook our method to init* 3:00
We then need to ensure that our new function/method is executed when the plugin is being initialized i.e. when we are instantiating our class, this is done by calling our new function inside the constructor of our class since the constructor is always executed when a class is instantiated
*Syntax* 4:10
_function __construct(){_
_add_action(‘init’, array($this,’function_name’))_
_}_
*Actual code*
_function __construct(){_
_add_action('init', array($this,'custom_post_type'));_
_}_
*refresh the rewrite rule* 6:33
_flush_rewrite_rules();_
This can be executed on activate plugin function and the deactivate plugin function
what if we dont add the flush rewrite rules()
thanks a lot!
your comments should be pinned
It's amazing! When I first came across this series I had no idea what OOP PHP was or why I would be using it and stopped at part three. Now I'm building a more modular OOP theme and want my custom post type in a plugin and... BAM! This is _exactly_ what I'm looking for! Solid content as always, champ!
Thank you so much, I'm glad my videos are helpful for you
You can’t send classname in a static way, because it is not going to be same instance, so it’ll never be able to read same data, $this means current instance of object/class (I know video author know, but whoever didn’t get it this is the reason) and neither methods are defined as static. This video was autoplayed and decided to watch, I like how explain it, great job.
thank you.
You are the Master of Wordpress developer.
9:25 This method will trigger a fatal error in PHP 7.4, the recommended version of PHP that the current version of WordPress (5.6) is using. If you haven't run into this issue yet, you will once you update your version of PHP.
This function is correct. custom_post_type needs to be called each time the plugin is initiated, not just when activated.
function __construct() {
add_action( 'init ', array( $this, 'custom_post_type ') );
}
This next function, as written, is what will throw the error:
function activate() {
$this->custom_post_type();
flush_rewrite_rules();
}
$this can no longer be used when not in object context. In addition, the action has already been called in the __construct function. This is all that is needed so far:
function activate() {
flush_rewrite_rules();
}
Many thanks to the uploader for posting these clips. They are appreciated.
I was wondering about that, thanks for the explanation
Very clear tutorial....Is this approach still current?
Yup!
Hi,
thx for the great tutorials.. pls grant me a question:
at minute 9:40 you are calling the method custom_post_type() directly from AlecadddPlugin::activate() and propagading, that the registration of the CPT will now only be done, when the plugin will be activated and the callback function is called.
But since you left the add_action hook in the constructor of the class nothing really changes, but to call the AlecadddPlugin::custom_post_type() twice.
Can you pls explain why you are following this approach?
Whoop, yeah, you're right. I forgot about that hook.
Don't worry too much about it anyway, because these lessons, up until 11 or 12, are made to give a quick overview of OOP, with different approaches and methods. After these videos, we're gonna start building the actual plugin with a proper structure. Cheers
Alessandro, you called 2 times register_post_type(). Both at 'init' and at ACTIVATE. You told that it is because it may not register at wordpress initialization. Did I understand it right? Could you explain it a little more. If registering at wordpress "init" then why register? If it works on "init" then why call again at activation?
OK, suuuuper bit noob mistake. I've been coding for years, but new to WP and PHP... The plugin was just showing as text on the dash board because... I had
Can you please answer the question to why you are running custom_post_type() in both the constructor and in the activate methods? It just looks like a bad practice.
Firstable, thanks for sharing your knowledge, I truly appreciate it. Aren't we calling the custom_post_type function twice in this example? once while the initiation through the init hook and again through the activation function?
The first 10-11 episodes of the series are just an overview of different methods and how to setup a plugin in OOP. We're gonna start coding the real plugin around video n.12, so yeah, some duplication or other stupid things can happen :D
I did everything exact as you said but the I don't see any custom post type on the left panel WHY?
Same😅😅
@@yanafit376 I had the same issue. For me it was register_post_type('book', ['public' => true, 'label' => 'Books']);. I had single quotes around 'true' and there should not be any as it is a boolean datatype. Hope that helps
@@tonyrums That was a great catch. Books now displays in the left menu. Thank you
Hello! For developer with VS Code, how extension need to install? The "register_...hook" show 'undefined functions'......
In my enveronment show this messege "The plugin generated 23 characters of unexpected output during activation. If you notice "headers already sent" messages, problems with feeds or other issues, try disabling or removing this plugin." how can I do fix this ?
after declaring add_action in constructor and custom_post_type in activate method. I went to my new generated plugin. I click on add new post put the data. after that i went to database to check the data that i have given in that new post form. guess what i did not find the data in post_type column in post table.
so my question is where is my data in table? as you have describe in this video that should placed in database table.
i have created a cpt but i cannout get the front end to show the contents of the post i have initialized the has_archive argument to true but i cannot seem to show my custom cpt recent posts or my post content to be shown
I cant believe I made a plugin that works. Fantastic tutorial! Thanks so much.
Good stuff, isn't building something from scratch amazing? Keep going and Happy Coding!
I was able to Activate/Deactivate plugin but the custom post type will not display in the left hand side of the admin menu.
i think you must call the class
if(class_exists('AlecadddPlugin'))
{
$alecadddalugin= new AlecadddPlugin();
}
While debugging I discovered the root problem to the 'Books' Label not appearing in the left collum. At 5:36 when he fixed/added the 'label' parameter it appears he corrected a syntax error and removed the single quotes around 'true'.
INCORRECT: register_post_type('book', ['public' => 'true', 'label' =>'Books'] );
CORRECT: register_post_type('book', ['public' => true, 'label' =>'Books'] );
@@JohnLittlefield I did that but still the same
I want to ask if the custom post type is created twice with examples you used.
i.e
1. first time the constructor is called during the instantiation of the class?
2. during the activation of the plugin
class AlecaddPlugin
{
public function __construct(){
add_action( 'init',[$this,'custom_post_type']);
}
public function activate(){
$this->custom_post_type();
flush_rewrite_rules();
}
//...
public function custom_post_type(){
register_post_type( 'book', ['label' => 'Books','public' => true] );
}
}
if(class_exists('AlecaddPlugin')):
$alecaddPlugin = new AlecaddPlugin();
endif;
//activation
register_activation_hook( __FILE__ , array($alecaddPlugin, 'activate'));
In this case yes, but WordPress doesn't create 2 post types, simply overrides the old one. Anyway, keep watching the series as these initial videos are just examples tho show some methodologies and different approaches. We won't keep this code
Hello, I'm trying to follow your tutos to create a simple plugin,but so far I haven't been able to achieve what I want, I'm trying to connect the front of my site with my plugin.
Basically I have a button on the page and I'm trying to make that when you click it a simple function in the plugin is called, but so far It hasn't worked, do you have any resources that I could consult?
THX
function custom_post_type() {
register_post_type('book', ['public'=>true, 'label'=>'Books']);
}
function __construct() {
add_action('init', array($this, 'custom-post-type'));
}
CPT doesnt appear to the left, anyone know why? Been trying to fix this for a week now, but can't wrap my head as to why it doesnt, does it affect the coding that I am using a webhotel?
how did you access the database at 10:19? Is it a special software you have to download? I've looked around and they direct towards cpannel, but I unfortunately don't have that
That's called Sequel Pro, it's MacOS SQL client to access local and remote DBs.
There are various alternatives for Windows and Linux, or even online, like PhpMyAdmin.
Ciao Alessandro.
grazie per il tuo lavoro.
Does Wordpress delete the custom post type information in the database once the plugin is deleted/uninstalled as default?
Thanks
how can we create new file for our CTP .. like single-books.php archive-books.php etc
Alessandro sigue así hermano, es de lo mejor que he visto hasta ahora en TH-cam!
why did you use both $this->create_custom_post_type(); --> in activate function and add_action('init',[$this, 'create_custom_post_type']); --> in construct function
This question is kind of off topic. There is this cool plugin which is made for the admin dashboard only. Do you know what kind of hook I can use to make the plugin appear for all users/public. As in add the plugin to the menu for all to see and access.
I did everything you asked in future videos. Keep watching the series and you'll learn everything you need.
Another great job by Alessandro Castellani. A million thanks for you... greetings from Romania
Thank you so much :D
Hi Alessandro, can I make this way custom_product_type ?
Your GitHub Last Class Don't activate in WordPress Plugins. I like to follow it with the code writing fist too see if it works.
Hello Alex, I have a question. When I use register_post_type ('book', array('lable' => 'Books', 'public' => 'true')), it seems not be working over here. However, when I assign the array to $args, and use register_post_type('book', $args), it works well. So what is the different?
Interesting. That shouldn't be any different.
Maybe it's related to your version of PHP. Do you know which version are you running?
register_post_type ('book', array('lable' => 'Books', 'public' => true)) true without ' '
great answer @Matthias Santschi! Indeed, true should be passed without quotes. Tested on PHP v7.2 and Wordpress v4.9.5
@@theGoldenification Thank You :D I was wondering why Books Wasn't Showing Up. Looking through the comments seemed to work
Great video. What editor are you using, and how does it autocomplete for all the WP functions?
VSCode which have extensions helping autocomplete related to PHP and WP
I do not get it cause at the end we code 2 times the activation of posttype... one in constructor the other one is on activation ? so we are making a double CaLL ?
My man, you are the best for this series.
Thank you so much :D
Alessandro, what function would someone use if the plugin they are creating was for accessing the files in the media library? I'm trying to create a visual effect on the thumbnails.
I created a custom widget the access the media library in this series. Keep watching and you'll learn everything you need :D
Hey Alessandro... I m learning with your tutorial. But this tutorial can't work in my case... the Book (Custom_post_type) do not show in my dashboard. How can I resolve this. Or how can I check the error. please help me.
Check my source code on GitHub to see if you have any typos or mistakes
Hy sir my plugin was not working
im too
This is great tutorial indeed, I rarely found a youtube videos creating plugin tutorial with OOP approach. Hope you can make a tutorial videos on how to create an extension/plugin for woocommerce using OOP. I hardly found on youtube a video like that. Anyway, this videos is what i'm looking for, Thank you very much.
Very good tutorial. I liked and subscribed. Thank you so much.!!
Welcome :D
I thought that $this refers to the object instantiated by the class, but the presenter says that it refers to the class. Please help me understand. Thanks
The official statement says that "it's a reference to the current object", and since the object it's the class, it's a reference of the class. Different ways to say the same thing :D
Hi Sir,
I have created the simple plugin using your part 2 & part 3 video and while clicking of the Activate button getting the below error.
"Plugin could not be activated because it triggered a fatal error.
"
Even enabled the debug mode getting the same error even after increase the memory limit.
Can you please help me on this.
Hi I did exactly the same, However I dont see the "Books" post, do you know why ??
Check my source code on GitHub to see if you have any typos or mistakes. Cheers
This is confusing.. You put the register post type function inside the constructor. Does'nt this calls this function every time its instantiated , regardless of activation/deactivation?
Was it necessary to call the custom_post_type() function in both places, or is one preferable?
Awesome tutorial, BTW, very thorough. Looking forward to learning more and watching your series on the custom theme with settings.
Are you sure that we need to call this->custom_post_type() inside 'activate' method?
Each time we activate our plugin we get a redirection to a new page, which means that our plugin will be executed and custom_post_type() will be called on 'init' hook.
So, your advice doesn't make sense.
This is just an example of how you can achieve something, it's not the golden standard, and if you think there's a better approach, go for it.
Anyway, until episode 12, these videos are just for introduction and generic overview of how WordPress manages plugins. The real structure and actual plugin will be build later.
Hey!
Great Series! Super happy to learn this :) But when i try to create the custom_post_type it wont create it, and i can see the code on the top in wp. The showin code starts from when we set the public => true. Any clue? :)
He changes the type of public from a string to boolean during a camera cut around 5:45. He changed 'public'->'true' to 'public'=>true by removing the single quotes. He doesn't mention this in the video. If you go forward a bit, you'll see he made the correction at some point.
@@suntank6 thanks!
Thanks!! awesome video to take my skills to the next level. Great work Alessandro. Ringraziamenti!!
good job Alessandro, can you share the code because i write the same code but nothing new when activating the plugin, and i get an 2834 error note in plugins page.
There you go: github.com/Alecaddd/WordPressPlugin101
I didnt watched whole video but have question. There are deact act unins methods. What's they function ? When we are deactivating or activating plugin they are already doing by wordpress itself am i wrong ? (when we deactivating or uninstalling this plugin CPT already deleting by wordpress)
You should watch the video where I explain why these hooks are useful for a developer.
Cheers
Thank you so much!
complimenti per la splendida serie di tutorial, personalmente ho un problema con la visualizzazione del cpt, che non mi compare nel backend nonostante abbia ricontrollato piu volte il tuo listato e ricopiato in maniera pedissequa, non visualizzo errori ne il custom post type, magari è cambiata la sintassi con le nuove versioni di wp? utilizzo wp 5.9 installato in locale con wplocal, qualche suggerimento... Grazie in anticipo
congratulations for the wonderful series of tutorials, personally I have a problem with the display of the cpt, which does not appear in the backend despite having rechecked several times your listing and copied slavishly, I do not see errors nor the custom post type, maybe the syntax has changed with the new versions of wp? I use wp 5.9 installed locally with wplocal, any suggestions ... Thanks in advance
what if i want to create 2-3 custom post or more then that, do i have create and write that all code again and again repeatedly????
You have to write just the code related to the CPT, not the entire plugin.
i have an error even i have exactly same code written as your tutorial , i have error saying when i activate the plug in
Plugin could not be activated because it triggered a fatal error.
could you please advise ?
Did you check my source code on GitHub?
What IDE is that?
These videos have got me hooked. New subscriber :)
Thank you so much :D
Is there anything changed on WordPress now? because your tutorial is fine, but register_post_type( 'Book', ['public'=>true] ); Here the book is not showing when I activate the plugin. Instead of that another custom post is created which is also named "posts" , In easy words this is creating a new custom post with the default name "posts"
oops okay. Thank you very much for your response buddy. Have a very nice day :)
Hi Alessandro,
On the same note of custom post type, I added metaboxes with rating and want to allow comments on the post type. So I want to make the site allow users to comment(review) and edit via front end without accessing the dashboard.
In case it is too much for a tutorial, can you explain how to make that happen?
We will tackle pretty much all the features you need in this plugin series. On Episode 14 I wrote down everything I'm planning to do.
Cheers
Do you can create github repository of this lessons? Thanks for your videos!
It's already up on my Github profile, I need to push the code for this lesson, will do later this evening
if(class_exists('Alecadddplugin')){ : Only works here when plugin is activated. shouldn't this condition be true as far as class Alecadddplugin is defined? regardless of Plugin Activated / Deactivated. Can anyone please explain this to me?
no words..your explanation... perfect..
Thank you so much :D
Greate tutorials but I did not get it "Books" on Wordpres dashboard?! I check everything twice.
Check my source code on Github to see if you have any typo.
Tnx, now work, awsome!
Alessandro, is it good or bad using SINGLETON structure instead of creating everytime new object: $alacadddPlugin=new AlacadddPlugin() - lines 61,62 ?
Nor good or bad, it all depends on what you want to use and how you want to. I find Singletons a bit limiting and somewhat complicated, but nonetheless are super helpful if you want to avoid unwanted mutations and keeping some logic under a class that can't be init more than once.
Thank you man
You're welcome!
What's the difference between $this->method() and array($this, 'method')
when you write $this inside a class you a referring to the current instance of the class, when you write $this->method(), you are calling the method on the class instance and returns the result. If in this case your method doesn't return anything (what's known as a void method) then you would actually be passing null to the add action argument.
however when you pass and array and say $this as one argument, you a passing the instance fo the class as the first argument and then a string which is the name of the method as the second argument.
So in short you want to tell the add_action method the name of the method it should call, you don't actually want to call it yourself.
Thank you Alessandro!
You're very welcome
Thank you Alessandro....
You're very welcome
Thanks Alessandro, that's really helpful. Really appreciate it.
You're very welcome :D
Thank you very much for your information, it is very useful and clear, you give very valuable information !!
Ah, thank you so much for this lovely comment
When created the function of custom post type and adding the action, it is giving this error, and thats why the label books is not showing in side bar:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'ahmedplugin' does not have a method 'custom_post_type' in C:\xampp\htdocs\plugintutorial\wordpress-5.2\wordpress\wp-includes\class-wp-hook.php on line 286
Check my source code on GitHub if something doesn't work
again - great tutorial:)
Can't wait for the next lesson!
Thanks for watching
please if the book post didn't appear for you maybe you should use this method it worked for me class TestPlugin
{
function __construct() {
add_action('init', 'custom_post_type');
}
function activate() {
//flush rewrite rules
flush_rewrite_rules();
}
function deacivate() {
}
function uninstall() {
}
function custom_post_type() {
register_post_type( 'book', ['Public' => true, 'label' => 'books'] );
}
}
Your code is wrong. I showed in the tutorial that you need to pass the array with the instance of the class in the add_action() method. Also, 'public' should be written lowercase.
First of all, I wanna thank you for all your videos, I have learned (And i'm still learning) a lot about wordpress thanks to you.
I looked for the function "flush_rewrite_rules" in codex and I found that it takes an expensive operation so it should only be used when absolutely necessary (codex)
My question is : Should we do the same thing that we have in the example in this link : codex.wordpress.org/Function_Reference/flush_rewrite_rules
I mean, create a function that register the CPT and flush rewrite rules then call it in deactivation and activation
Is it OK to call the registation of CPT in deactivation ?
I Hope my question is clear, and sorry for my english.
Hi, thank you so much for watching my videos.
Yes, the flush_rewrite_rules() should be used carefully. If you keep following my videos, you'll see that I actually transfer it from the simple activation of the plugin to only being used when creating or deleting a custom post type from within the plugin.
You don't need to call again the registration of the CPT on deactivation, just have the flush_rewrite_rules() there to deal with it.
Cheers
thank you very much - i love it. 'It would be a great pleasure if you create more vids.
you have helped me alot.
if you come up with more vids - you probably would attract many many ppl - wordpress user and fans from all over the globe
super crisp and clear tutorial ;-)
Thanks for the videos, it helps a lot, but when using OOP the methods (functions) in the class shouldn't have the accessor keyword (public/private) it will be more correct.
It's not required if all your methods are public, but yeah, definitely it's good to have it for better readability
I'm truly surprised to see there are 2 thumbs down on this video ... this baffles me! What kind of people would thumbs down this kinda awesome training ... my guess, they must be mentally challenged! In that case, my apologies ...
It's impossible to please everyone, someone will always dislike you, no matter what :D
@@alecaddd Some people don't know what is the meaning of like/dislike :D Ignore please :)
Awesome...Classes and methods. OOP. I like it. Still on track. Great stuff. Thanks
Thinks are starting to get modular :D
Thank you Sir
You're welcome
AMAZING
confused in flush rewrite rules
What's confusing?
what does it do?
I said it in the video, exactly at this minute 1:00
Part 1 - 4 done viewing but need to return back later after i'm done with Shopify Mastery!
from where you are doing shopify??
please provide source code.
awesome videos Its better than Udemmy and Lynda courses very very professional
Oh wow, thank you so much. And most importantly, it's free :P
awesome
!
continuo a commuovermi
Dai, non fare così, ti giuro che di svarioni linguistici ne faccio in abbondanza
register_post_type( 'books', ["public" => true, 'label' => 'Books'] );
Don't add single quote to true, else it is not showing wordpress Version 6.6.1.
source code wii be helpful for us
There's the link to my GitHub repo in the description with full source code.
github.com/Alecaddd/WordPressPlugin101
First I click like and then I watched the video:D
Sorry for my English:P
Thanks for the trust :D
Your comment summarizes the current political situation in my country.
Including the apologies x''D
This is what i have been doing, so i don't forget about it!
I'm getting a syntax error warning for the custom_post_type function in Dreamweaver. I'm not getting any warnings or errors when I test it though. Has anyone else had this happen?
Maybe because it's CS5 they haven't updated their code to include wordpress stuff? or maybe I've just typed something wrong and can't see it haha
Here's the code, within the class. I've named it ACOrder instead of Books.
function custom_post_type(){
register_post_type('acorder', ['public' => true, 'label' => 'ACOrder']);
}
Thank you :)
You should switch to a more modern and free code editor like VSCode or Sublime Text
@@alecaddd
Thank you for all your work! Legend! :)
your videos are dope!! one of the best in the world at learning to be a wordpress developer, as i'm on my way to that!! Thanks a lot for this excellent material dear Master :)
The only critic is that you don't enlarge your screen enough while you are coding, it is short and not that easy to see when i wish to code live while u do!!
Pliz could you do a special tuto on how to build a plugin with the kind of dashboard woocommerce has, especially at the level of the Orders or Commands of products: because i wish to know how you can add each product with its quantity, in the same command... i don't know if they do that with jquery or vanillajs... it will be dope you have time for this big challenge for me (Wordpress + jquery or vanillajs)
May Jesus bless you for your gift :)
Thanks for watching my videos. I thought my resolution was big enough, sorry for the issue, I'll try to increase it a bit more.
Keep watching the plugin series as I'm building a complex administration area with repeater fields, custom sections, and adavanced options. Cheers
"Keep watching the plugin series as I'm building a complex administration area with repeater fields, custom sections, and adavanced options." "sorry for the issue, I'll try to increase it a bit more."
Thanks in advance for answering my queries... Cheers man
You should make a plugin tutorial demonstrating the WordPress Http API.
developer.wordpress.org/plugins/http-api/
Sure, I will, thanks for the suggestion
1/13/2019 => thank you : merci : شكرا : Gracias
You're very welcome :D
Hey, if anyone wants to know a little more about rewrite rules in wordpress, check out this blog post www.pmg.com/blog/a-mostly-complete-guide-to-the-wordpress-rewrite-api/
More wordpress posts from this site www.pmg.com/blog/tag/wordpress/
Thanks for sharing
Great effort, A lot of appreciations. Thanks
i did that. well nevermind i love your teachings keep the good work, i want to write a plugin about tracking , where i can create a tracking id with map and a short-code to display a tracking form so that when a user enters a tracking id in the front end it will display the tracking details with the location under it.
Keep watching this series and you'll learn everything you need. I tackled the creation of shortcodes, storing dynamic data, and more good stuff. Cheers
5:10 - Deeez
This tutorials must named as "Learning OOP while you Create a WordPress Plugin from Scratch"
I know OOP.. so I don't need to learn it again!
Skip 3 or 4 videos and you'll be fine
❤🙏
Respect :))
Thanks :D