{"id":3338,"date":"2020-09-19T11:59:16","date_gmt":"2020-09-19T10:59:16","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3338"},"modified":"2026-02-25T10:05:56","modified_gmt":"2026-02-25T10:05:56","slug":"plugin-namespace","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/plugins\/create-your-own-plugin\/plugin-namespace","title":{"rendered":"Plugin namespace"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Creating a plugin inside a namespace<\/h4>\n\n\n\n<p>By using a namespace, everything inside the namespace does not need globally unique naming prefixes, solving clashing issues with other plugins, themes and wordpress itself.  Using a namespace is much simpler than using a class for your plugin.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Add all your php files to the namespace<\/h4>\n\n\n\n<p>Add this to the top of each file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nnamespace MyPluginName;<\/code><\/pre>\n\n\n\n<p>Every function or class defined in this file will be considered as part of this namespace<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">WordPress hooks, Shortcodes, etc<\/h4>\n\n\n\n<p>You must include your namespace in the definition, e.g.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'wp_enqueue_scripts', '\\MyPluginName\\my_function_name' );\n\/\/add_filter('wp_enqueue_scripts', __NAMESPACE__ . '\\my_function_name');    &lt;&lt;&lt;Alternative method\nfunction my_function_name()\n{\n}\n\nadd_shortcode('my_shortcode', '\\MyPluginName\\my_shortcode_function');\nfunction my_shortcode_function()\n{\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Using plugin define values<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$MyValue = \\MyPluginName\\MY_PLUGIN_DEFINE;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Calling Functions<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Functions in a file under the same namespace<\/h5>\n\n\n\n<p>You can call it directly<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">WordPress functions<\/h5>\n\n\n\n<p>If you don&#8217;t have a function with the same name under your namespace then you can call it directly.<\/p>\n\n\n\n<p>Forcibly specifying that the function is in the global space:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $post_id = \\wp_insert_post();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Functions in another namespace (and calling functions in your plugin from outside the plugin)<\/h5>\n\n\n\n<p>You need to specify the namespace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$Result = \\SomeOtherNamespace\\function_name();<\/code><\/pre>\n\n\n\n<p>If you want to define the other namespaces function a being used within this namespace you can use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use function SomeOtherNamespace\\function_name;\n\n\/\/Now your can use that function directly\n$Result = function_name();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Functions using the same name in your namespace and also not in any namespace<\/h5>\n\n\n\n<p>How a call to function foo() is resolved &#8211; calling from inside the namespace:<br>1) It looks for a function from the current namespace call foo(). <br>2) If that fails it tries to find and call the global function foo().<\/p>\n\n\n\n<p>How a call to function foo() is resolved &#8211; calling from outside the namespace:<br>It tries to find and call the global function foo().<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Defines<\/h4>\n\n\n\n<p>By default define()  defines a constant in the global namespace, even if used in a file that is part of a specific namespace.<\/p>\n\n\n\n<p>To define a constant for use inside a namespace you need to prefix the constant name with the namespace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>define(__NAMESPACE__ . \"\\MY_CONSTANT_NAME\",          24);\n\n\/\/Which is the same as:\ndefine(\"MyNamespaceName\\MY_CONSTANT_NAME\",          24);<\/code><\/pre>\n\n\n\n<p>Use of the define within the namespace doesn&#8217;t require the prefex, just the definition does.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a plugin inside a namespace By using a namespace, everything inside the namespace does not need globally unique naming prefixes, solving clashing issues with other plugins, themes and wordpress itself. Using a namespace is much simpler than using a class for your plugin. Add all your php files to the namespace Add this to [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[251,297,303],"tags":[],"class_list":["post-3338","post","type-post","status-publish","format-standard","hentry","category-create-your-own-plugin","category-namespace","category-namespace-wordpress"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3338"}],"version-history":[{"count":11,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3338\/revisions"}],"predecessor-version":[{"id":5293,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3338\/revisions\/5293"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}