Why namespaces are rarely used in WordPress plugins?
Why namespaces are rarely used in WordPress plugins?
Why namespaces are rarely used in WordPress plugins?
Composer generates the autoloader for you. Thus, you don’t need to require a specific file from vendor packages. All you need to do is require the composer autoloader: Change require_once dirname(__FILE__) . ‘/vendor/Frozensheep/RightmoveADF/RightmoveADF.php’; to require_once plugin_dir_path(__FILE__) . ‘vendor/autoload.php’; In some cases, classes still not be found. Re-generate the composer autoloader by $ composer dump-autoload. Usually, … Read more
Firstly, to do this the register_metaboxes() method needs to be static: public static function register_metaboxes() { } Then, for the callback you pass an array with the full class name including the namespace: $args = array( ‘register_meta_box_cb’ => [ ‘FrameWork\Helper\Metabox’, ‘register_metaboxes’ ], ); If, for whatever reason, register_metaboxes() isn’t static (i.e. you’re using $this) then … Read more
At first, we need to update phpcs.xml. Let’s replace “my_prefix” with your plugin name (then, assuming “eustatos”): <rule ref=”WordPress.NamingConventions.PrefixAllGlobals”> <properties> <property name=”prefixes” type=”array”> <element value=”eustatos”/> </property> </properties> </rule> Then, you can use namespace start with eustatos in project PHP file. ex: namespace eustatos\test_plugin; cf. Customizable sniff properties ยท WordPress/WordPress-Coding-Standards Wiki To execute this check one … Read more
You must specify in the roots sage theme composer.json the runtime where autoload can find the classes. “autoload”: { “psr-4”: { “App\\”: “app/”, “App\\Blocks\\”: “app/Blocks/”, “Itineris\\AcfGutenblocks\\”: “../../plugins/acf-gutenblocks/src/”, } } Don’t forget to run composer dump-autoload
The register_post_type has_archive option also accepts a string. That string will be used for the archives. See the changes in the code below: function create_reviews_post_type() { register_post_type(‘acme_reviews’, array( ‘labels’ => array( ‘name’ => __(‘Reviews’), ‘singular_name’ => __(‘Review’) ), ‘menu_position’ => 5, ‘public’ => true, ‘has_archive’ => ‘reviews’, ); } add_action(‘init’, ‘create_reviews_post_type’);
From the PHP manual on defining namespaces: Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the top of the file before any other code – with one exception: the declare keyword. To fix the issue, simply make sure that your namespace declaration comes before the other … Read more
The problem is with your Autoloader set-up. You need to convert your Camelcase namespacese to dashes for locating files as per your current folder structure. I have added convert function and updated your autoloader function. In wp-plugin-template/autoloader.php: <?php spl_autoload_register( ‘autoload_function’ ); function autoload_function( $classname ) { $class = str_replace( ‘\\’, DIRECTORY_SEPARATOR, str_replace( ‘_’, ‘-‘, strtolower(convert($classname)) … Read more
Before going too far down this path I would suggest that you familiarize yourself with PHP name resolution rules. To answer your actual question – When you namespace functions, the fully qualified name of those functions includes the namespace. In your example, you have defined two functions: \myPlugin\add_activation_notice and \myPlugin\activation_notice. When calling either of these … Read more