Articles of oop

problem with implementing widget via the_content()

hey guys, I really need you help with this one. I’m using the subscribe2 Plugin (for email subscriptions). I want to show the signup form as a widget. The plugin author recommends doing it the following way. $content = apply_filters(‘the_content’, ‘<!–subscribe2–>’); $content = remove_filter(‘the_content’, ‘page_route’); echo $content; this works fine, except for the fact that […]

How to include one class/instance without using global variables

I would like to add http://mobiledetect.net/ class to my template. Which is the best approach to dynamically use this class in every part(s) of my template? For the moment I just loaded the class in functions.php like this function devicecontrol() { require_once ‘_/inc/md/Mobile_Detect.php’; } add_action(‘wp_loaded’, ‘devicecontrol’, ‘1’); but to use it, I must first create […]

Conditional tags inside a class

I have a couple of functions that I now combined in a class. The reason for that was that all these functions go towards one goal, and it is more appropriate way to use the globals between these functions. The class consists of three main sections, section one to add my meta boxes, section two […]

Extend a class of a plugin

There is a plugin that uses a class; and create an object like this: class WC_Disability_VAT_Exemption { public function __construct() { add_action( ‘woocommerce_after_order_notes’, array( $this, ‘exemption_field’ ) ); } public function exemption_field() { //some code here } } /** * Return instance of WC_Disability_VAT_Exemption. * * @since 1.3.3 * * @return WC_Disability_VAT_Exemption */ function wc_dve() […]

Current user in plugin returns NULL

I am currently developing a plugin and I am trying to make use of the $current_user global. class Something { public function __construct() { $this->get_user(); var_dump( $this->user ); } private function get_user() { require_once( ABSPATH . ‘/wp-includes/pluggable.php’ ); $this->user = wp_get_current_user(); } } This actually works. However, I need to call pluggable.php file which shouldn’t […]

Wp_update_post: Infinite loop even with remove_action solution, OOP

I have a quite complex PHP class, reponsible for creating and printing meta_boxes, generating and saving meta fields, … Each meta_box (object of class) has its own save function, that updates meta data of post (fields for them are instances of another class) as well as its post_excerpt and post_content. Yep, it’s probably a little […]

How to structure a plugin into multiple files using classes?

I am writing my first simple plugin from scratch so I’m looking for a bit of advice on structuring the classes within the plugin. I encapsulated some basic functions in a class in the main plugin file but I guess it will become quite ungainly with a lot of functions in a single file. The […]

array of objects to do_action, callback gets singular object?

So I was passing an array of objects through an action today, no biggy, and things were working fine. Until I was iterating over one of the arguments to a callback and started getting errors. The only difference was that the passed array to the do_action call only had a single object in it. Confused […]

The ideal place for storing persistent PHP objects

I’m writing some plugins that do background batch processing, i.e. operations that take longer than 30 seconds. I’m creating objects with the necessary job parameters set like this (this is a hugely simplified example): class MyPlugin_Create_Terms { public $job_id; public $terms; public $taxonomy; public $created = 0; public function init( $terms, $taxonommy ) { $this->job_id […]

When to use 'get_category_by_path' vs. 'get_term_by' to get category object from `get_query_var( 'category_name' )`?

I’d like to retrieve the category object from the category name, using the public query variable category_name, via get_query_var( ‘category_name’ ): $cat = get_query_var( ‘category_name’ ); There appear to be two methods: get_category_by_path() get_term_by() Using get_category_by_path() Based on WordPress core code (wp-includes/canonical.php), I found out that I’d do it this way: $category = get_category_by_path( $cat […]