Hot answers tagged

23 votes
Accepted

add_action in namespace not working

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 ...
user avatar
9 votes

PHP error with shortcode handler from a class

You can use like this, shortcode inside the Class class stockData{ function __construct() { add_shortcode( 'your_shortcode_name', array( $this, 'showData' ) ); //add_action('...
user avatar
9 votes
Accepted

When and Where is `global $post` Set and Available?

Global $post var is set by WP::register_globals() method. It is called by WP::main() method, on its turn called by wp() function that is called when wp-blog-header.php is loaded. If you look at the ...
user avatar
  • 44.8k
7 votes

How to name files of namespaced classes?

First, ignore the class- prefix. This comes from WordPress’ pure procedural code approach, classes are used as containers for procedural code, not for real objects, and most files do not contain ...
user avatar
  • 105k
6 votes

How to use classes declared in another plugin?

This is too late but I would like to share how to use woocommerce and its classes without having an error class not found. First is to check if woocommerce is installed and use the woocommerce_loaded ...
user avatar
6 votes
Accepted

Getting headers property from WP_Theme object

You cannot access the $headers property as it is a private property. Members declared as private may only be accessed by the class that defines the member For that reason, you get NULL when you ...
user avatar
6 votes
Accepted

Current user in plugin returns NULL

Wait for the action plugins_loaded before you create the class instance. The pluggable functions are loaded at this time. From wp-settings.php: /** * Fires once activated plugins have loaded. * * ...
user avatar
  • 105k
6 votes

Creating a custom post type using a object oriented approach?

I'll answer scenario #3, even though it can be adapted for scenarios #1 and #2, too. WordPress codex recommends that we create Custom Post Types (CPTs) in a plugin. Following that recommendation, I ...
user avatar
6 votes
Accepted

OOP Plugin Development. Including external object

The root of your problem is not object oriented programming, but a fundamental misunderstanding about how PHP handles requests. If this were a Java or a Node application for example, you would start ...
user avatar
  • 53.4k
5 votes
Accepted

Namespaces in WordPress - How do I initiate the main class?

Give it a try like so: File: oowp/bootstrap.php <?php /* Plugin name: OOWP */ require_once( 'autoload.php' ); use oowp\objects\Custom_Objects; new Custom_Objects;
user avatar
  • 1,533
5 votes
Accepted

How Can I Add a Filter to Class Instance Only?

The problem is that filters in WordPress are global; if you add a filter somewhere it persists everywhere, unless you remove it. Also consider that you should prefer composition over inheritance, and ...
user avatar
  • 44.8k
5 votes
Accepted

__NAMESPACE__ with register_activation_hook

The reason that the plugin activation hook wasn't working in the code provided in the question is that on plugin activation the plugins_loaded hook is never run. Since the register_activation_hook ...
user avatar
5 votes
Accepted

How to run query inside a class with namespace?

You need to add a \ in front of the WP_Query call to tell PHP that it's in the global namespace and not the namespace of this class. Try this: $query = new \WP_Query($args);
user avatar
4 votes

Get menu object from theme_location

Or if you need it in one line, just copy that and replace "change-this-location-slug" by our own. $string = get_term(get_nav_menu_locations()['change-this-location-slug'], 'nav_menu')->name;
user avatar
4 votes
Accepted

Object method calling for global $wpdb in header.php

I think the problem might lie in: $wpdbinfo = $wpdb->get_results("SELECT * FROM bo_mytable WHERE id=3"); $wpdb->get_results() returns an array of objects, yet you are referencing a property on ...
user avatar
4 votes

What is the best way to instantiate a class of a plugin in your WordPress theme?

There are two common solutions to access classes in a plugin from inside a theme: 1. Use an autoloader in the plugin (preferred). You tell the autoloader how to relate a class name to a PHP file ...
user avatar
4 votes
Accepted

Where is the "ancestors" post object attribute?

The reason you don't see it if you dump object is because it's not actual object property. WP_Post implements magical __get() method for ancestors and several more keys like that. When you access $...
user avatar
  • 98k
4 votes
Accepted

How do you render_callback for register_block_type to a method in another class?

Option 1 is the solution, here's a smaller example: $obj = new Obj(); .... register_block_type( 'my-plugin/my-block', [ 'editor_script' => 'editor-script-handle', '...
user avatar
  • 53.4k
3 votes
Accepted

How to Set Taxonomy Object Description?

I don't see an option for it in register_taxonomy() It's not in Codex, but is fully supported: add_action( 'init', function() { register_taxonomy( 'foo', 'post', array( ...
user avatar
  • 44.8k
3 votes

Object method calling for global $wpdb in header.php

As said in Codex, wpdb::get_results(): the function returns the entire query result as an array (bold mine) So, your $wpdbinfo is not an object, but an array, and if you had turn on WP_DEBUG, ...
user avatar
  • 44.8k
3 votes
Accepted

Adding an admin page - OOP approach not working

I hate OOP mixed in the functional structure of wordpress (here come the downvotes.....). If you use OOP just for name spacing then just use the name spacing feature of PHP, putting function in a ...
user avatar
  • 22.8k
3 votes

Adding an admin page - OOP approach not working

Working Example class PluginSkeleton { public function __construct(){ add_action('admin_menu',array($this,'pluginskeleton_menu')); } public function pluginskeleton_menu(){ ...
user avatar
3 votes

syntax for remove_filter in parent theme with class

Filters and actions should always be static methods exactly to make them easy to remove. If they are not static and the object is not a singleton (seems that way in your case) then there are no ...
user avatar
  • 22.8k
3 votes

Extend a class of a plugin

Woocommerce support sent me the solution of my problem: function unregister_parent_hook() { if ( function_exists( 'wc_dve' ) ) { $instance = wc_dve(); remove_action( '...
user avatar
  • 101
3 votes

add_action in namespace not working

The easiest way would be to enclose your code in curly brackets: namespace myPlugin { add_action( 'admin_init', __NAMESPACE__ . '\\add_activation_notice' ); function add_activation_notice() ...
user avatar
3 votes
Accepted

WP Plugin + OOP: Adding Menu Page doesn't create required effect

the call at the anonymous function is done after all the executions of add_new_page. then you need to store all elements to create menu items. try to modify the class Helper like that public ...
user avatar
  • 3,604
3 votes

Using OOP plugin's methods throughout the website

I'm not entirely sure what you want to do exactly. But basically you just create one instance and then add functions like public static function FunctionName() to be used anywhere you want after the ...
user avatar
  • 2,923
3 votes

Using OOP plugin's methods throughout the website

One way is to use static methods from class itself, as @leymannx explained in his answer. I prefer a little bit different approach, which is a little bit nicer/cleaner (but it's only my opinion, so ...
user avatar
2 votes

Registering Class methods as hook callbacks

For static methods you can also do this: ['foo','bar'] when the following gives Undefined class constant 'bar': foo::bar example - when specifying the $control_callback for ...
user avatar
2 votes

How to get media objects

Have you tried to use get_children() function? Edit: All media attached to any post is treated as children of it. The function wp_get_attachment_image() will only return images. With the function ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible