{"id":502,"date":"2021-08-19T11:27:48","date_gmt":"2021-08-19T10:27:48","guid":{"rendered":"https:\/\/codeamp.com\/?p=502"},"modified":"2024-03-03T11:56:06","modified_gmt":"2024-03-03T11:56:06","slug":"using-php-namespaces-in-wordpress-plugins-creating-an-autoloader","status":"publish","type":"post","link":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/","title":{"rendered":"Using PHP namespaces in WordPress plugins + creating an autoloader"},"content":{"rendered":"\n<p>I&#8217;ve been working on upgrading one of my WordPress plugins &#8211; Search &amp; Filter for some time now, quite the overhaul you might say\u2026<\/p>\n\n\n\n<p>When planning this update there were a bunch of things <em>I knew<\/em> I needed to implement. I won&#8217;t bore you with all the details, but two tasks I wanted to tick off were:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Use PHP namespaces<\/li><li>Setup an autoloader<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"php-namespaces\">PHP namespaces<\/h2>\n\n\n\n<p>The plugin is getting quite big, and having everything as a class with a unique name was getting a bit messy.<\/p>\n\n\n\n<p>PHP namespaces solve this issue perfectly as you can scope your classes and then you only need to worry about the uniqueness of your top-level namespace (uniqueness is important in WP plugins in order to avoid conflicts with other plugins).<\/p>\n\n\n\n<p>This also lends itself very nicely to consistent project structure because you will likely nest your files in directories matching your namespaces &#8211; everything becomes more predictable and uniform.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"php-autoloader\">PHP autoloader<\/h2>\n\n\n\n<p>When PHP applications grow in size, rather than having to <code>require<\/code> every other <code>.php<\/code> file in a long list (or by some other method) it&#8217;s good practice to automatically load them.<\/p>\n\n\n\n<p>As long as your application has a predictable structure (namespace and class names), you don&#8217;t need to specify which files to load because we can predict where the relevant file is for a particular class (more on that later).<\/p>\n\n\n\n<p>What&#8217;s also great about autoloaders is they only load files at the last minute &#8211; when a class is used &#8211; giving your application a lighter memory footprint.<\/p>\n\n\n\n<p><em>These two concepts absolutely go hand in hand.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"wordpress-enters-the-room\">WordPress enters the room\u2026<\/h2>\n\n\n\n<p>Ok, so we know from the above that if our plugin is going to grow that we&#8217;re going to want to use both namespaces and an autoloader.<\/p>\n\n\n\n<p>And if we&#8217;re good developers, we also want to follow the <a href=\"https:\/\/developer.wordpress.org\/coding-standards\/wordpress-coding-standards\/php\/\">WordPress coding standards<\/a>.<\/p>\n\n\n\n<p>Coding standards are a good thing and often in the life of a developer, we&#8217;ll find conflicting scenarios where we might want to use one set of standards or the other.<\/p>\n\n\n\n<p>The problem here is, while <a href=\"https:\/\/developer.wordpress.org\/coding-standards\/wordpress-coding-standards\/php\/\">WordPress coding standards<\/a> cover a lot they don&#8217;t mention anything about namespaces.<\/p>\n\n\n\n<p>I&#8217;ve read a few articles similar to this on ways to use autoloading with WordPress like <a href=\"https:\/\/www.smashingmagazine.com\/2015\/05\/how-to-use-autoloading-and-a-plugin-container-in-wordpress-plugins\/\">this one<\/a> and <a href=\"https:\/\/awhitepixel.com\/blog\/autoloader-namespaces-theme-plugin\/\">this one<\/a> &#8211; and I&#8217;m going to share my take on a possible setup.<\/p>\n\n\n\n<p>There is no mention of namespaces in the docs which leaves this wide open to interpretation and using our best guess &#8211; this post is my best guess as to what I think the WordPress way could be.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"wordpress-php-coding-standards\">WordPress PHP Coding Standards<\/h2>\n\n\n\n<p>In the <a href=\"https:\/\/developer.wordpress.org\/coding-standards\/wordpress-coding-standards\/php\/#naming-conventions\">naming conventions<\/a>, it&#8217;s stated that class names should be cased like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">My_Class<\/pre>\n\n\n\n<p>And that they should have a corresponding filename something like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class-my-class.php<\/pre>\n\n\n\n<p>The class name is lowercased, underscores are hyphenated and the filename is prepended with <code>class-<\/code>.<\/p>\n\n\n\n<p><em>So far so good.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"plugin-development-best-practises\">Plugin Development Best Practises<\/h3>\n\n\n\n<p>There is a <a href=\"https:\/\/developer.wordpress.org\/plugins\/plugin-basics\/best-practices\/\">section in the best practises<\/a> which is hugely important:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Prefix everything!<\/p><\/blockquote>\n\n\n\n<p>When a site can have hundreds of plugins, the only way our code won&#8217;t be messed with is if we prefix everything (including class names).<\/p>\n\n\n\n<p>You might want to go with a simple abbreviation prefix, eg, if your plugin was called <code>Speed Up<\/code>, you could use <code>SU_My_Class<\/code> but in my opinion (and in the examples) the full name is better, so in theory, the ideal class name would be:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Speed_Up_My_Class<\/pre>\n\n\n\n<p><em>It&#8217;s already getting ugly, but it is necessary.<\/em><\/p>\n\n\n\n<p>This would ideally be in a file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class-speed-up-my-class.php<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"switching-to-namespaces\">Switching to namespaces<\/h2>\n\n\n\n<p>When structuring a plugin like this it is common to find yourself with tons of files starting with <code>class-speed-up-... .php<\/code> &#8211; to me it&#8217;s ugly, but it&#8217;s necessary because it accurately represents the class name, which has been prefixed too <code>Speed_Up_...<\/code><\/p>\n\n\n\n<p>If we switch to namespaces we can alleviate some of this pain and repetition:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">namespace Speed_Up;\n\nclass My_Class {\n\n}<\/pre>\n\n\n\n<p>The great thing here is, now <code>My_Class<\/code> lives inside the <code>Speed_Up<\/code> namespace, we don&#8217;t need to worry about prefixing it with <code>Speed_Up<\/code> again. By using a namespace, we&#8217;ve kind of prefixed everything inside of it automatically and it won&#8217;t conflict with other plugins.<\/p>\n\n\n\n<p>This is how we would use the class now:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$my_class = new \\Speed_Up\\My_Class;<\/pre>\n\n\n\n<p>So I think there is not much complexity in this process &#8211; just start using them and drop the plugin prefix from all your class names (and filenames) &#8211; to be honest I think it&#8217;s keeping in line with the spirit of <strong>prefix everything<\/strong>.<\/p>\n\n\n\n<p>*Also &#8211; I haven&#8217;t mentioned it explicitly &#8211; in my opinion, the naming convention of the namespace should match that of a class, with underscores separating words with initial capital letters.<\/p>\n\n\n\n<p><strong>A quick note about namespaces<\/strong> &#8211; they support nesting! So you can keep adding names spaces and sub-levels to improve your code organisation &#8211; eg:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Speed_Up\\Frontend\\Scripts\\Register<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"setting-up-the-autoloader\">Setting up the autoloader<\/h2>\n\n\n\n<p>Now we&#8217;ve got the namespace out of the way let&#8217;s head back to the autoloader.<\/p>\n\n\n\n<p>There are a few ways to setup autoloaders (each with its own unique benefits) <em>which I&#8217;m not going to get into right now<\/em> &#8211; I&#8217;m going to go for the easiest and most straightforward method while also keeping things in the spirit of the WordPress coding standards.<\/p>\n\n\n\n<p>Setting one up requires using <a href=\"https:\/\/www.php.net\/manual\/en\/language.oop5.autoload.php\">built-in PHP features<\/a>.<\/p>\n\n\n\n<p>To implement this in a plugin would require adding some code at the top of your plugins main <code>php<\/code> file &#8211; if we&#8217;re sticking with the name <code>speed-up<\/code> then it would be located:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">wp-content\/plugins\/speed-up\/speed-up.php<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>We could also create a new file <code>autoload.php<\/code> in the root directory to store our autoloader &#8211; which is something you&#8217;ll probably see quite often when reading up on the subject. *<\/li><\/ul>\n\n\n\n<p>If we add the first example from the PHP docs:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">spl_autoload_register(function ($class_name) {\n    include $class_name . '.php';\n});<\/pre>\n\n\n\n<p>Then we&#8217;ll be all set to go! Well, kind of.<\/p>\n\n\n\n<p>What this code does is register a callback to be used, to specify which filename\/path we should look in order to find a class.<\/p>\n\n\n\n<p><strong>Roughly how it works:<\/strong><\/p>\n\n\n\n<p>When a class is first used, eg: <code>$my_class = new My_Class()<\/code> and it is not yet loaded, the function we passed to <code>spl_autoload_register<\/code> will be run to try to resolve the filename for the class.<\/p>\n\n\n\n<p>It is passed in the <code>$class_name<\/code>, and then you can see the file is loaded via <code>include<\/code>. In the example above, based on the class name the file<\/p>\n\n\n\n<p><code>My_Class.php<\/code> would attempt to be loaded.<\/p>\n\n\n\n<p>Also, something worth observing, when a class is used in a namespace, the full namespace + class name is passed through <code>$class_name<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"writing-an-autoloader-for-a-wordpress-plugin\">Writing an autoloader for a WordPress plugin<\/h3>\n\n\n\n<p>So let&#8217;s apply the coding standards to the autoloader and calculate the correct file path based on namespace and class name.<\/p>\n\n\n\n<p>What we want to achieve:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Class filenames must be prefixed with <code>class-<\/code>\u2026<\/li><li>Class filenames should be lowercase<\/li><li>Underscores should be replaced with dashes<\/li><\/ul>\n\n\n\n<p>And we&#8217;re going to convert namespaces. Classes belonging to a namespace should live in a folder (namespace name) that follows the conversion process of a class. So we need to add:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Namespace names will be converted to lowercase<\/li><li>Namespace names will have underscores replaced with dashes<\/li><li>Namespaces will be folders so convert <code>\\<\/code> to a directory<\/li><\/ul>\n\n\n\n<p>In addition &#8211; because the root plugin folder &#8220;is the plugin&#8221; and already has a directory name relevant to the plugin name, eg &#8211; <code>speed-up<\/code> we shouldn&#8217;t create another folder called <code>speed-up<\/code> just to match our <code>Speed_Up<\/code> plugin namespace, it&#8217;s unnecessarily repetitive and we&#8217;ll actually remove that, and instead, we&#8217;ll create a folder called <code>includes<\/code> for our classes instead.<\/p>\n\n\n\n<p>So our plugin structure would look like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">speed-up \/\/ top-level plugin folder\n-- speed-up.php \/\/ main plugin file\n-- readme.txt \/\/ required with every plugin\n-- includes \/\/ this folder is where we store all our classes\n---- database \/\/ folder for the sub namespace Database\n------ class-connect.php \/\/ class Connect\n       class-worker.php \/\/ class Worker<\/pre>\n\n\n\n<p>The class <code>Worker<\/code> in <code>includes\\database\\class-worker.php<\/code> above would be accessed like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Speed_Up\\Database\\Worker<\/pre>\n\n\n\n<p>For me, this is the perfect balance of adding namespaces to WordPress plugins while keeping in the spirit of the coding standards.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"the-code\">The code<\/h3>\n\n\n\n<p>To parse the name space and class names, and follow those above conversion rules (class name -&gt; path + filename) would result in some PHP code like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Define the main autoloader\nspl_autoload_register( 'speed_up_autoloader' );\nfunction speed_up_autoloader( $class_name ) {\n\n    \/\/ These should be changed for your particular plugin requirements\n    $parent_namespace = 'Speed_Up';\n    $classes_subfolder = 'includes';\n\n    if ( false !== strpos( $class_name, $parent_namespace ) ) {\n        $classes_dir = realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR . $classes_subfolder . DIRECTORY_SEPARATOR;\n\n        \/\/ Project namespace\n        $project_namespace = $parent_namespace . '\\\\';\n        $length = strlen( $project_namespace );\n\n        \/\/ Remove top-level namespace (that is the current dir)\n        $class_file = substr( $class_name, $length );\n        \/\/ Swap underscores for dashes and lowercase\n        $class_file = str_replace( '_', '-', strtolower( $class_file ) );\n\n        \/\/ Prepend `class-` to the filename (last class part)\n        $class_parts = explode( '\\\\', $class_file );\n        $last_index = count( $class_parts ) - 1;\n        $class_parts[ $last_index ] = 'class-' . $class_parts[ $last_index ];\n\n        \/\/ Join everything back together and add the file extension\n        $class_file = implode( DIRECTORY_SEPARATOR, $class_parts ) . '.php';\n        $location = $classes_dir . $class_file;\n\n        if ( ! is_file( $location ) ) {\n            return;\n        }\n\n        require_once $location;\n    }\n}<\/pre>\n\n\n\n<p>I&#8217;ve also setup a tiny demo plugin using the above for you to play with over on GitHub:<br><a href=\"https:\/\/github.com\/rmorse\/speed-up\">Github Repo<\/a><\/p>\n\n\n\n<p>If you want to read more articles like this you can&nbsp;<a href=\"https:\/\/twitter.com\/rmors_\">follow me on Twitter<\/a>&nbsp;to keep up to date.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on upgrading one of my WordPress plugins &#8211; Search &amp; Filter for some time now, quite the overhaul you might say\u2026 When planning this update there were a bunch of things I knew I needed to implement. I won&#8217;t bore you with all the details, but two tasks I wanted to tick [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":504,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,11,7],"tags":[],"class_list":["post-502","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-plugins","category-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using PHP namespaces in WordPress plugins + creating an autoloader - Code Amp<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using PHP namespaces in WordPress plugins + creating an autoloader - Code Amp\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been working on upgrading one of my WordPress plugins &#8211; Search &amp; Filter for some time now, quite the overhaul you might say\u2026 When planning this update there were a bunch of things I knew I needed to implement. I won&#8217;t bore you with all the details, but two tasks I wanted to tick [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Amp\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-19T10:27:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-03T11:56:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codeamp.com\/wp-content\/uploads\/2021\/08\/wp-autoload-namespace-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"899\" \/>\n\t<meta property=\"og:image:height\" content=\"495\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ross Morsali\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@rmors_\" \/>\n<meta name=\"twitter:site\" content=\"@code_amp\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ross Morsali\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/\"},\"author\":{\"name\":\"Ross Morsali\",\"@id\":\"https:\\\/\\\/codeamp.com\\\/#\\\/schema\\\/person\\\/f07e5a501a3e476f253174c4f8085e64\"},\"headline\":\"Using PHP namespaces in WordPress plugins + creating an autoloader\",\"datePublished\":\"2021-08-19T10:27:48+00:00\",\"dateModified\":\"2024-03-03T11:56:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/\"},\"wordCount\":1320,\"publisher\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeamp.com\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/wp-autoload-namespace-1.jpg\",\"articleSection\":[\"PHP\",\"Plugins\",\"WordPress\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/\",\"url\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/\",\"name\":\"Using PHP namespaces in WordPress plugins + creating an autoloader - Code Amp\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codeamp.com\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/wp-autoload-namespace-1.jpg\",\"datePublished\":\"2021-08-19T10:27:48+00:00\",\"dateModified\":\"2024-03-03T11:56:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/#primaryimage\",\"url\":\"https:\\\/\\\/codeamp.com\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/wp-autoload-namespace-1.jpg\",\"contentUrl\":\"https:\\\/\\\/codeamp.com\\\/wp-content\\\/uploads\\\/2021\\\/08\\\/wp-autoload-namespace-1.jpg\",\"width\":899,\"height\":495},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codeamp.com\\\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codeamp.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using PHP namespaces in WordPress plugins + creating an autoloader\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codeamp.com\\\/#website\",\"url\":\"https:\\\/\\\/codeamp.com\\\/\",\"name\":\"Code Amp\",\"description\":\"Developing WordPress Plugins\",\"publisher\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codeamp.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codeamp.com\\\/#organization\",\"name\":\"Code Amp\",\"url\":\"https:\\\/\\\/codeamp.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/codeamp.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/codeamp.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/codeampsquare.png\",\"contentUrl\":\"https:\\\/\\\/codeamp.com\\\/wp-content\\\/uploads\\\/2021\\\/01\\\/codeampsquare.png\",\"width\":500,\"height\":500,\"caption\":\"Code Amp\"},\"image\":{\"@id\":\"https:\\\/\\\/codeamp.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/code_amp\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codeamp.com\\\/#\\\/schema\\\/person\\\/f07e5a501a3e476f253174c4f8085e64\",\"name\":\"Ross Morsali\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8ba742824818fce32d5defa1b1ec3c694de80a68850ebcf041561ea3eb5985f2?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8ba742824818fce32d5defa1b1ec3c694de80a68850ebcf041561ea3eb5985f2?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8ba742824818fce32d5defa1b1ec3c694de80a68850ebcf041561ea3eb5985f2?s=96&d=retro&r=g\",\"caption\":\"Ross Morsali\"},\"description\":\"Web developer by trade, with over 15 years building things in PHP, HTML, CSS, and JavaScript - focusing on WordPress for the last 7 years.\",\"sameAs\":[\"https:\\\/\\\/codeamp.com\",\"https:\\\/\\\/x.com\\\/rmors_\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using PHP namespaces in WordPress plugins + creating an autoloader - Code Amp","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:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/","og_locale":"en_GB","og_type":"article","og_title":"Using PHP namespaces in WordPress plugins + creating an autoloader - Code Amp","og_description":"I&#8217;ve been working on upgrading one of my WordPress plugins &#8211; Search &amp; Filter for some time now, quite the overhaul you might say\u2026 When planning this update there were a bunch of things I knew I needed to implement. I won&#8217;t bore you with all the details, but two tasks I wanted to tick [&hellip;]","og_url":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/","og_site_name":"Code Amp","article_published_time":"2021-08-19T10:27:48+00:00","article_modified_time":"2024-03-03T11:56:06+00:00","og_image":[{"width":899,"height":495,"url":"https:\/\/codeamp.com\/wp-content\/uploads\/2021\/08\/wp-autoload-namespace-1.jpg","type":"image\/jpeg"}],"author":"Ross Morsali","twitter_card":"summary_large_image","twitter_creator":"@rmors_","twitter_site":"@code_amp","twitter_misc":{"Written by":"Ross Morsali","Estimated reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/#article","isPartOf":{"@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/"},"author":{"name":"Ross Morsali","@id":"https:\/\/codeamp.com\/#\/schema\/person\/f07e5a501a3e476f253174c4f8085e64"},"headline":"Using PHP namespaces in WordPress plugins + creating an autoloader","datePublished":"2021-08-19T10:27:48+00:00","dateModified":"2024-03-03T11:56:06+00:00","mainEntityOfPage":{"@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/"},"wordCount":1320,"publisher":{"@id":"https:\/\/codeamp.com\/#organization"},"image":{"@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/#primaryimage"},"thumbnailUrl":"https:\/\/codeamp.com\/wp-content\/uploads\/2021\/08\/wp-autoload-namespace-1.jpg","articleSection":["PHP","Plugins","WordPress"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/","url":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/","name":"Using PHP namespaces in WordPress plugins + creating an autoloader - Code Amp","isPartOf":{"@id":"https:\/\/codeamp.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/#primaryimage"},"image":{"@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/#primaryimage"},"thumbnailUrl":"https:\/\/codeamp.com\/wp-content\/uploads\/2021\/08\/wp-autoload-namespace-1.jpg","datePublished":"2021-08-19T10:27:48+00:00","dateModified":"2024-03-03T11:56:06+00:00","breadcrumb":{"@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/#primaryimage","url":"https:\/\/codeamp.com\/wp-content\/uploads\/2021\/08\/wp-autoload-namespace-1.jpg","contentUrl":"https:\/\/codeamp.com\/wp-content\/uploads\/2021\/08\/wp-autoload-namespace-1.jpg","width":899,"height":495},{"@type":"BreadcrumbList","@id":"https:\/\/codeamp.com\/using-php-namespaces-in-wordpress-plugins-creating-an-autoloader\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codeamp.com\/"},{"@type":"ListItem","position":2,"name":"Using PHP namespaces in WordPress plugins + creating an autoloader"}]},{"@type":"WebSite","@id":"https:\/\/codeamp.com\/#website","url":"https:\/\/codeamp.com\/","name":"Code Amp","description":"Developing WordPress Plugins","publisher":{"@id":"https:\/\/codeamp.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codeamp.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/codeamp.com\/#organization","name":"Code Amp","url":"https:\/\/codeamp.com\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/codeamp.com\/#\/schema\/logo\/image\/","url":"https:\/\/codeamp.com\/wp-content\/uploads\/2021\/01\/codeampsquare.png","contentUrl":"https:\/\/codeamp.com\/wp-content\/uploads\/2021\/01\/codeampsquare.png","width":500,"height":500,"caption":"Code Amp"},"image":{"@id":"https:\/\/codeamp.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/code_amp"]},{"@type":"Person","@id":"https:\/\/codeamp.com\/#\/schema\/person\/f07e5a501a3e476f253174c4f8085e64","name":"Ross Morsali","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/8ba742824818fce32d5defa1b1ec3c694de80a68850ebcf041561ea3eb5985f2?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8ba742824818fce32d5defa1b1ec3c694de80a68850ebcf041561ea3eb5985f2?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8ba742824818fce32d5defa1b1ec3c694de80a68850ebcf041561ea3eb5985f2?s=96&d=retro&r=g","caption":"Ross Morsali"},"description":"Web developer by trade, with over 15 years building things in PHP, HTML, CSS, and JavaScript - focusing on WordPress for the last 7 years.","sameAs":["https:\/\/codeamp.com","https:\/\/x.com\/rmors_"]}]}},"_links":{"self":[{"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/posts\/502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/comments?post=502"}],"version-history":[{"count":0,"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/posts\/502\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/media\/504"}],"wp:attachment":[{"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/media?parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/categories?post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeamp.com\/wp-json\/wp\/v2\/tags?post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}