Hot answers tagged

19

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....


8

You can use like this, shortcode inside the Class class stockData{ function __construct() { add_shortcode( 'your_shortcode_name', array( $this, 'showData' ) ); //add_action('login_enqueue_scripts', array( $this,'my_admin_head')); } function showData(){ return '<h1>My shortcode content</h1>' ; } } $...


7

You need to match the $priority you used to hook the action: remove_action( 'save_post', array( $this, 'save_box' ), 20 /* Same as add_action call */ ); http://codex.wordpress.org/Function_Reference/remove_action Make sure you take the $post_id argument in your save_box method too: function save_box( $post_id ) { ... }


7

AFAIK $query->get for main query works only with public query vars, i.e. vars that can be triggered via url, but nothing prevents to directly access directly to tax_query property of query, but notice that it is an object, instance of WP_Tax_Query and the current queried taxonomy arguments are in the queries property of that object. Accessing to that ...


7

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 graph @Rarst built, on the left, you can see where wp() function is called. In terms of hooks, global post variable is set just before "wp" hook runs, so that ...


7

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 classes at all or classes and other code together. It doesn’t make sense when all of your files contain just one class and nothing else. If you would follow that ...


6

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 action hook. /** * Check if WooCommerce is active **/ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( '...


6

Some basic rules for object organization in a plugin. One class declaration per file, no other code in that file. The creation of a new instance – new Dummy_Class – should not be in the same file as the class declaration. Everything that is sending data to the user (presentation) should get its own class: HTML, CSV, XML output or HTTP headers. Every ...


6

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 try to access the property with wp_get_theme()->headers. You need to make use of the magic __get() method of the class to get the info you are after. Example:...


6

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. * * Pluggable functions are also available at this point in the loading order. * * @since 1.5.0 */ do_action( 'plugins_loaded' ); I would even wait for ...


6

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 will write my answer using modern PHP, which is not meant for a distributed plugin, but rather for own projects where you have control over the PHP version ...


6

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 the application on the server, and it would recieve requests, and respond. It's a continuous active program. As a result, you can store things in memory and ...


5

That is not the way you add an object method as a callback. function make_shortcode() { add_shortcode('ShowMsg', array($this,'ShowMsg')); } This is explained in the Codex as it pertains to actions and filters, but the principle is the same. I should add that anonymous classes make for painful debugging. Instantiate that class to a variable. It will ...


5

WordPress itself is not very object oriented - it has objects (like WP_User, for example), but most interactions with the core API take place through plain function calls. It also depends widely on global state, though it does a good job of managing this through the abstractions of actions and filters. In my experience there is a sweet spot between sticking ...


5

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 if your current application structure is already built upon inheritance, and you can't or don't want to change it, you should avoid at least using filters for ...


5

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;


5

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 hook was hooked from plugins_loaded it was never run on activation. And since it's never fired ever again, this way of hooking results in register_activation_hook ...


5

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);


4

If the classes are not in the same file, you'll need to require it into the child class ( myCustomClass ). Are you doing that? Example : //get the base class if(!class_exists('MyParentClass')) { require_once plugin_dir_path( __FILE__ ) . '/_inc/MyParentClass.php'; } /** * Class Definition */ class MyChildClass extends MyParentClass{ // class ...


4

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;


4

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 $wpdbinfo (->nameinfo). You'll either want to loop through the $wpdbinfo array, or if you're certain you'll get only a single record (possibly a safe ...


4

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 name that contains the class. Then, when you access a class that is not yet known to PHP, the autoloader checks for the file that relates to that class and loads ...


4

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 $post->ancestors() what you actually get is not some value from the object, but return of get_post_ancestors() function executed on it. So in a nutshell this ...


4

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', 'render_callback' => [ $obj, 'block_save_function' ] ] ); In that code [ $obj, 'block_save_function' ] is equivalent to $obj->block_save_function(.... The ...


3

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, your code had thrown a warning. When you need one single row form db, use wpdb::get_row() and when you need a single variable (like in your example) use wpdb::...


3

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( 'description' => 'Hi there!' ) ); $foo = get_taxonomy( 'foo' ); echo $foo->description; // output "Hi there!" });


3

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 class is just a very small part of what OOP should be. Anyway, your problems is that a there are only two way to refere to an class method, either when it is ...


3

Working Example class PluginSkeleton { public function __construct(){ add_action('admin_menu',array($this,'pluginskeleton_menu')); } public function pluginskeleton_menu(){ add_menu_page( 'Application Users', 'Application Users', 'manage_options', 'application-users.php',array($this,'application_users_page')); } ...


3

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 options but to remove all the hooks for that action/filter and add yours (@birgre's answer is a variation on this theme). While far from being elegant this might work ...


3

Woocommerce support sent me the solution of my problem: function unregister_parent_hook() { if ( function_exists( 'wc_dve' ) ) { $instance = wc_dve(); remove_action( 'woocommerce_after_order_notes', array( $instance, 'exemption_field' ) ); } }


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