Monday, 3 April 2017

Understanding Namespaces in the WordPress Hook System — SitePoint


Get 5 WordPress Themes for the Price of 1 For A Limited Time! Show Me The Themes
Hooks are a fundamental concept for WordPress developers. In previous articles on SitePoint, we’ve learned what hooks are and their importance, the two types of hooks: actions and filters with code examples of how they work, and an alternative way of firing actions and filters events and how to hook static and non-static class methods to actions and filters.
In this article, I will cover how to hook methods of an instantiated class (object) to actions and filters, how to integrate a namespaced class method to a hook, caveats of using namespaces in WordPress hook system and solution to them.
Hooking Object Methods
Assume you were tasked by your employer to build an ad manger plugin for a large news website, to make ad insertion to news content seamless. This is how you might go about building it.
You would create an AdManager class with a number of methods that contain the various ad-networks ad-code.
class AdManager {

/**
* AdSense unit code.
*/
public function adsense() { ?>
//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js
Source: https://managewp.org/articles/14759/understanding-namespaces-in-the-wordpress-hook-system-sitepoint




source https://williechiu40.wordpress.com/2017/04/03/understanding-namespaces-in-the-wordpress-hook-system-sitepoint/

No comments:

Post a Comment