wp_localize_script() and JavaScript namespaces

The way wp_localize_script() is supposed to be used is for that 2nd argument to be your namespace. Then you put an array as the 3rd argument for the values: wp_localize_script( ‘my-script’, ‘namespace_name’, array( ‘var_name’ => $var, ) ); Now you can access the variable in the script with namespace_name.var_name. If you also use the namespace … Read more

Problem with autoloader and namespaces

The callback [ ‘Book’, ‘vt_register_book_post_type’ ] is adding a hook to the static method vt_register_book_post_type in the Book class of the global namespace. If you want to add a callback to a static class of the Book namespace, you need to use [ ‘\Book\Book\’, ‘vt_register_book_post_type’ ] as a callback, but that will also cause a … Read more

How do I define and register a shortcode function in a namespaced functions.php file?

Thanks to Stanislav Khromov (also active on WPSE!), I learned any Callable referenced in add_shortcode() must be fully qualified, even if it is defined in the same file you’re referencing it. <?php /** * theme’s functions.php */ namespace MyNamespace; //[foobar] function foobar_func( $atts ){ return “foo and bar”; } add_shortcode( ‘foobar’, ‘\MyNamespace\foobar_func’ ); You may … Read more

Why is a wp function used in current PHP namespace’s callback not resolved to global scope?

From your comment: Apparently the function can indeed not be used on the frontend in wp (see here), can anyone confirm this? I can confirm that the post_exists() function can be used on the front-end, but you need to manually load the file that defines the function, which is wp-admin/includes/post.php — see the “File: <path>” … Read more

Autoloader not finding classes from my plugin

Given the first PHP code snippet you posted, I don’t see that you’re using the class yet, which explains why you’re not seeing your classes in the autoload error log that you’re debugging with. Note that use will not trigger PHP’s autoloader. use My_Plugin\Includes; You need to actually instantiate a class, check that it exists, … Read more