{"id":75143,"date":"2017-02-18T06:31:25","date_gmt":"2017-02-18T06:31:25","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=75143"},"modified":"2017-02-18T07:25:09","modified_gmt":"2017-02-18T07:25:09","slug":"wordpress-oop-based-plugin-development","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/","title":{"rendered":"WordPress OOP Based Plugin Development"},"content":{"rendered":"<p>Object Oriented Approach is beneficial over \u00a0Procedural Approach in terms of code re-usability, providing much more organised structure to code, etc. So we will learn a basic of WordPress Object Oriented Plugin Development.<\/p>\n<p>&nbsp;<\/p>\n<h3>WordPress Object Oriented Plugin Development<\/h3>\n<p>&nbsp;<\/p>\n<p>So first of all we will start from initialising a class named WordPress.<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nclass WordPress{\r\n\t\r\n\tfunction __construct(){\r\n\r\n\t}\r\n\r\n}\r\nnew WordPress();\r\n?&gt;<\/pre>\n<p>Every class contains the &#8220;__construct&#8221; function. It is a constructor method for class based approach and &#8220;new&#8221; is used to create object of class.<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nclass WordPress{\r\n\t\r\n\tfunction __construct(){\r\n\r\n\t}\r\n\tfunction first(){\r\n            echo 'Hello World';\r\n\t}\r\n\r\n}\r\n$obj = new WordPress();\r\n$obj-&gt;first();<\/pre>\n<p>Now let&#8217;s say we have a function named\u00a0&#8220;first&#8221; and we want to get its content i.e. &#8216;Hello World&#8217;. So we will first create an object of class by using &#8220;new&#8221; and then we will call the function named &#8220;first&#8221; like $obj-&gt;first(), and it will simply print &#8220;Hello World&#8221;.<\/p>\n<p>If we want to get the contents of function &#8220;first()&#8221; in the beginning then we can simply call this function in our constructor as:<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nclass WordPress{\r\n\t\r\n\tfunction __construct(){\r\n\t\t$this-&gt;first();\r\n\t}\r\n\tfunction first(){\r\n\r\n\t}\r\n\r\n}\r\n$obj = new WordPress();<\/pre>\n<p>Now, as there are many predefined hooks in WordPress, so now a next pain\u00a0we face is how to load a function when the particular hook fires. \u00a0So here is the simple solution.<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nclass WordPress{\r\n\t\r\n\tfunction __construct(){\r\n\r\n\t\tadd_action( 'hook_name', array( $this, 'first' ) );\r\n\r\n\t}\r\n\tfunction first(){\r\n\t\techo 'Hello World';\r\n\t}\r\n\r\n}\r\n$obj = new WordPress();<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"panel panel-primary\">\n<div class=\"panel-heading\">\n<div>\n<h3 class=\"panel-title\">Support<\/h3>\n<\/div>\n<\/div>\n<div class=\"panel-body\">\n<div>\n<p>Still have any issue feel free to add a ticket and let us know your views to make the code better \u00a0<a href=\"https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/\" target=\"_blank\">https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/<\/a><\/p>\n<p>Thanks for Your Time! Have a Good Day!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object Oriented Approach is beneficial over \u00a0Procedural Approach in terms of code re-usability, providing much more organised structure to code, etc. So we will learn a basic of WordPress Object Oriented Plugin Development. &nbsp; WordPress Object Oriented Plugin Development &nbsp; So first of all we will start from initialising a class named WordPress. &lt;?php class <a href=\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1260],"tags":[4505,4506,42,1258,1501],"class_list":["post-75143","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-oop","tag-oot","tag-plugin","tag-wordpress","tag-wordpress-plugin"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>WordPress Object Oriented Plugin Development<\/title>\n<meta name=\"description\" content=\"WordPress Object Oriented Plugin Development is useful in maintaining standard code structure and code re-usability.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WordPress Object Oriented Plugin Development\" \/>\n<meta property=\"og:description\" content=\"WordPress Object Oriented Plugin Development is useful in maintaining standard code structure and code re-usability.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-02-18T06:31:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-02-18T07:25:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ayush Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ayush Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/\"},\"author\":{\"name\":\"Ayush Singh\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5deb39718ae59fa5659a825549b7df4a\"},\"headline\":\"WordPress OOP Based Plugin Development\",\"datePublished\":\"2017-02-18T06:31:25+00:00\",\"dateModified\":\"2017-02-18T07:25:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/\"},\"wordCount\":230,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"OOP\",\"OOT\",\"plugin\",\"wordpress\",\"wordpress plugin\"],\"articleSection\":[\"WordPress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/\",\"url\":\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/\",\"name\":\"WordPress Object Oriented Plugin Development\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2017-02-18T06:31:25+00:00\",\"dateModified\":\"2017-02-18T07:25:09+00:00\",\"description\":\"WordPress Object Oriented Plugin Development is useful in maintaining standard code structure and code re-usability.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WordPress OOP Based Plugin Development\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5deb39718ae59fa5659a825549b7df4a\",\"name\":\"Ayush Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3488792c5856a76629020ea17053d952aa9915d88309f1f38e901acce88408d2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3488792c5856a76629020ea17053d952aa9915d88309f1f38e901acce88408d2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ayush Singh\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/ayush-singh265\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WordPress Object Oriented Plugin Development","description":"WordPress Object Oriented Plugin Development is useful in maintaining standard code structure and code re-usability.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/","og_locale":"en_US","og_type":"article","og_title":"WordPress Object Oriented Plugin Development","og_description":"WordPress Object Oriented Plugin Development is useful in maintaining standard code structure and code re-usability.","og_url":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2017-02-18T06:31:25+00:00","article_modified_time":"2017-02-18T07:25:09+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Ayush Singh","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ayush Singh","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/"},"author":{"name":"Ayush Singh","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5deb39718ae59fa5659a825549b7df4a"},"headline":"WordPress OOP Based Plugin Development","datePublished":"2017-02-18T06:31:25+00:00","dateModified":"2017-02-18T07:25:09+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/"},"wordCount":230,"commentCount":3,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["OOP","OOT","plugin","wordpress","wordpress plugin"],"articleSection":["WordPress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/","url":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/","name":"WordPress Object Oriented Plugin Development","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2017-02-18T06:31:25+00:00","dateModified":"2017-02-18T07:25:09+00:00","description":"WordPress Object Oriented Plugin Development is useful in maintaining standard code structure and code re-usability.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/wordpress-oop-based-plugin-development\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"WordPress OOP Based Plugin Development"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5deb39718ae59fa5659a825549b7df4a","name":"Ayush Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3488792c5856a76629020ea17053d952aa9915d88309f1f38e901acce88408d2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3488792c5856a76629020ea17053d952aa9915d88309f1f38e901acce88408d2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ayush Singh"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/ayush-singh265\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/75143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=75143"}],"version-history":[{"count":5,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/75143\/revisions"}],"predecessor-version":[{"id":75169,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/75143\/revisions\/75169"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=75143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=75143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=75143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}