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

Imagine the following script: <?php namespace MySpace; class MyClass { public static function execute() { // Do something post_exists(1,'','','my_post_type'); } } ?> I'm now calling this function at the top of the page template, before any HTML. The problem I get is that wp resolves the post_exists function to the namespace instead of falling back to the global scope, where wp functions are usually recognized in such cases. So I actually get a PHP error saying Call to undefined function …
Category: Web

How do I add an action using namespaces, and file separation?

I'm working on a plugin, and I'm trying to get to grips with namespaces and how they work with actions. In a separate file, I have a single function with a namespace declared at the top: namespace verism\ImageUpload; function image_upload() { ... echo 'function successfully called'; wp_die(); } In the main plugin php file I'm trying to add an action as follows: namespace verism; use function verism\ImageUpload\image_upload as image_upload; add_action( 'wp_ajax_set_image_from_url', 'image_upload' ); add_action( 'wp_ajax_nopriv_set_image_from_url', 'image_upload' ); I have also …
Category: Web

worldpay class not working with namespace in WordPress

I am trying to create Worldpay gateway with help of Worldpay library and my own extending code. Here is my code which I tried and failed to work on. require_once('init.php'); use lib\Worldpay; if($_POST == $_SERVER['REQUEST_METHOD']) { //Form Submit Actions, Here i have called Worldpay class to create object. $worldpay = new Worldpay('Key'); //line no: 25 } ?> <script src="https://cdn.worldpay.com/v1/worldpay.js"></script> <form action="" method="post"> </form> I have tried to find similar question and answer. I found this. Eventhough i can't make it …
Category: Web

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

I wrote a function in my theme's functions.php and am attempting to register it with add_shortcode() as shown in the Codex: <?php /** * theme's functions.php */ namespace MyNamespace; //[foobar] function foobar_func( $atts ){ return "foo and bar"; } add_shortcode( 'foobar', 'foobar_func' ); However this does not work and a post containing [foobar] simply renders "[foobar]" as text instead of being replaced by the handler function's return value. My guess is it has something to do with my functions.php being …
Category: Web

WooCommerce custom product type not saving with namespaces

I am try to create my custom product type, but when I save it is still simple product. This because I am using namespacees but I can not figure out the problem. This is class with new pt namespace giftbox\models\woocommerce; class WC_Product_Giftbox extends \WC_Product_Simple { protected $product_type = 'giftbox'; public function get_type() { return 'giftbox'; } } the name of the class I think is correct and I understood I don't need construct in WC 3.0 Then I have the …
Category: Web

Namespaces declared by a theme/plugin should start with the theme/plugin prefix. Found: "eustatos\test_plugin"

I want to use namespace in my plugin. And I have error after PHP_Code_Sniffer check. Namespaces declared by a theme/plugin should start with the theme/plugin prefix. Found: "eustatos\test_plugin". (WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound) My folder structure: . └── test-plugin ├── composer.json ├── phpcs.xml ├── test-plugin.php └── vendor // test-plugin.php <?php namespace eustatos\test_plugin; phpcs.xml content from here How I must name namespace?
Category: Web

Does the namespace always have to be prefixed to the function or class when referencing callbacks in WordPress, even when they are in the same file?

I want to use namespaces instead of prefixes for a plugin, since now the minimum supported PHP version for WP is 5.6.20 and namespaces can be used. But it does not seem to work with hooks: namespace My\Whatever; // Enqueue CSS and JS function enqueue_css_and_js() { // Do it... } add_action( 'wp_enqueue_scripts', 'enqueue_css_and_js' ); Results in this error: call_user_func_array() expects parameter 1 to be a valid callback, function 'enqueue_css_and_js' not found or invalid function name However, if I prefix the …
Category: Web

Getting wp_timezone undefined within a namespace

My workaround at the moment, within a shortcode, is to just copy the WP native code to get the Timezone within my (namespaced) class: new \DateTimeZone( $this->wp_timezone_string() )); private function wp_timezone_string() { $timezone_string = get_option( 'timezone_string' ); if ( $timezone_string ) { return $timezone_string; } $offset = (float) get_option( 'gmt_offset' ); $hours = (int) $offset; $minutes = ( $offset - $hours ); $sign = ( $offset < 0 ) ? '-' : '+'; $abs_hour = abs( $hours ); $abs_mins = …
Category: Web

Use Composer Package inside class - Namespacing error

I have my theme, with a folder called classes, with a single class: wp-content |- themes |- mycustomtheme |- classes |- FooBar.php In FooBar, I would like to use this package here: Soundasleep / HTML2Text. And use it like this: In FooBar.php doSomethingBadass( $some_html ){ wp_mail( '[email protected]', 'test', \Soundasleep\Html2Text::convert( $some_html ) ); } But whatever I do, I keep getting this error: Fatal error: Uncaught Error: Class 'Soundasleep\Html2Text' not found in /app/public/wp-content/themes/mycustomtheme/classes/FooBar.php:60 My attempt I assume that it's something to …
Category: Web

Custom Rest API namespace and endpoints are responding with 404 & 503 errors

I am new to WordPress plugin development. I was developing a plugin for WordPress that would be used to create mobile apps for WordPress Bloggers. The whole source code of the plugin is at:- https://github.com/delhideveloper/Delhi-Developer-PLugin-For-Blogger-Mobile-Apps I have implemented the JWT Authentication in the plugin using firebase JWT. I know there was already a plugin for implementing JWT authentication but it was not improved since 2 years and I was facing some problems with it. I have used only POST requests …
Category: Web

Sage WordPress - Plugin Namespace: Not Found

I originally posted in the Sage forum here. I'm trying to get a plugin to work but it's not, and I feel like it's a very general Namespace problem, and not something with the actual plugin, so I feel I could find success here. The plugin (and it's code/repo) is here: https://github.com/ItinerisLtd/acf-gutenblocks Any ideas how I can solve this error? Fatal error: Uncaught Error: Class 'Itineris\AcfGutenblocks\Plugin' not found in /app/public/wp-content/plugins/acf-gutenblocks/acf-gutenblocks.php:32 Stack trace: #0 /app/public/wp-includes/class-wp-hook.php(286): Itineris\AcfGutenblocks\{closure}('') #1 /app/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #2 …
Category: Web

Override plugin class which has namespace

I would like to override this plugin class in child theme : class-config.php namespace um; if ( ! defined( 'ABSPATH' ) ) exit; if ( ! class_exists( 'um\Config' ) ) { /** * Class Config * * Class with global variables for UM * * @package um */ class Config { //some code here... } //end class } But as you can see, this class uses a namespace, so I can't just write this in my functions.php : functions.php class …
Category: Web

How to call a WP Class inside my theme

I wrote a WP theme using namespaces and autoload. Everything went smooth until I started to use WP classes which I understand of course because I understand how namespacing works. My question is, is there a way possible to use a Wordpress class inside one of my theme classes, for example I want to use $wp_customize, an instance of WP_Customize_Manager? How would I achieve that, keeping my OOP theme structure. UPDATE: Thanks to Pat J. I was able to solve …
Category: Web

Why does wp_die() not work when inside a namespace?

Consider the following code copied from ACF docs, refactored to work inside a namespace: add_filter('acf/location/rule_match/user', __NAMESPACE__ . '\acf_location_rule_match_user', 10, 4); function acf_location_rule_match_user($match, $rule, $options, $field_group) { var_dump($options); wp_die(); } The problem is, nothing happens. I confirmed this only occurs when I'm inside a namespace. What am I doing wrong?
Category: Web

How to reference a function from a class in a different file which is also namespaced?

For example I am inside a file1.php with a namespaced class, like so: <?php namespace FrameWork\CPT; class CPT{ ..... public function register_custom_post_type() { $args = array( 'register_meta_box_cb' => //PROBLEM: How to reference from a different file which also contains a namespaced class register_post_type('plugin-cpt', $args); } How do I access a public function from a namespaced class from file2.php? <?php namespace FrameWork\Helper; class Metabox{ ..... public function register_metaboxes() { // I want to reference this function }
Category: Web

Access WP_REST_Server from within plugin namespace

I am new to OOP and developing plugins. I have developed a plugin using the Plugin-Boilerplate with namespace. I expanded the boilerplate to include a REST folder and class so I could create custom routs/endpoints. It is working; however I cannot use the WP_REST_Server object. I get the following error: ..class-wp-rest-server.php does not exist.From my limited knowledge of oop my guess I it is not in my namespace. But how do I include/access it? My guess is there is a …
Category: Web

Update body class based on theme as well as a html attribute

I just started using wordpress. Now I'm trying to create a theme for a static web page I wrote. For that web page I'm using barbajs, which is why I have a html tag (in my case a section tag) with a custom data-namespace attribute. Also I use body classes for each page. So basically a page looks something like that: <body class="page-about"> <main> <section data-namespace="about"> </section> </main> </body> I was able to use a function from this question to …
Category: Web

Problem with autoloader and namespaces

I'm writing a simple plugin with oop and using namespaces and autoloading. Here is my main file: add_action('plugins_loaded', array(My_Test_Plugin::get_instance(), 'plugin_setup')); class My_Test_Plugin { /** * Plugin instance. * * @see get_instance() * @type object */ protected static $instance = NULL; /** * URL to this plugin's directory. * * @type string */ public $plugin_url = ''; /** * Path to this plugin's directory. * * @type string */ public $plugin_path = ''; /** * Access this plugin’s working instance * …
Category: Web

Hiding class namespace from plugin's extensions

I'm developing a plugin using this boilerplate and I designed it to be extensible. The boilerplate supports namespacing and uses it and namespaces are bind to folder structure. The extensible part is about loading a class from the extension and I want to require extension developers to: check the existance of the class loader (namespace: Plugin_Name\Inc\Admin) extend their class from a base class (namespace: Plugin_Name\Inc\Admin) I'm looking for the easiest way to fulfill the above so that they won't get …
Category: Web

wp_localize_script() and JavaScript namespaces

As you are aware, wp_localize_script() allows the creation of a JavaScript object from a PHP object. So, after running the code in the example below: wp_localize_script('my-script', 'var_name', $var); // This works We will end with a JavaScript variable named var_name that is accessible in the script my-script. This JavaScript variable is copied from the original PHP variable $var. Everything is OK so far. The issue I am facing is this: I like to be able to assign var_name to a …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.