Martin  Dreech

Martin Dreech


How to Preventing free-range Wordpress hooks

One of the major frustrations I experience when developing for WordPress is the abundance of functionality that relies on writing opaque strings. The most common of these are Actions and Filters. Pretty much every tutorial or piece of documentation you might find on creating and applying filters will present you with code like this:

add_filter('post_class', 'my_post_class_modifier', 10);

function my_post_class_modifier($classes){
$classes[] = ‘another-css-class’;
return $classes;

I’ve always had a disdain for this approach. It requires:

  • Prefixing the function name with a clunky namespace to avoid potential clashes
  • Writing the function name twice, one of which is inside a string, so Intellisense won’t be any help

You won’t be able to option-click on the function name and find out where it’s being called, or find the function by clicking on the calling code. This is more of an issue when you need to use the same function for multiple filters. Your function gets further and further away from the calling code and unless you’re very organised your functions.php file (which by default is where you’ll put this code) will end up becoming an unnavigable dumping area full of nasty opaque string function calls. Ack!

Object method hooks

Ok, so most of the time I am a little bit cleaner in my approach and I’ll try to corral hooks related to eachother or to particular custom post types into a class. You can of course use object methods in your hooks.

class myClass {
public function applyHooks(){
add_action(‘hook_string’, array($this, ‘hookForThisThing’), 10);

public function hookForThisThing($arg){
    // Perform an action here


This approach means you don’t have to prefix faux namespaces to function names and it does at least allow you to more easily find the original priority value - you could even make it a property of the class.

You can also use class properties inside your function’s body which is a nice way of passing arbitrary data to the conditionals within. But you still have to use opaque strings which I’m frankly just against. I want my IDE to write as much of my code as possible as there’s less room for typographic errors. Remembering function names is also an issue. A proper IDE will help you find it even if you only know one word in its name (a good reason for writing clear and long if necessary function names).

Anonymous functions

One approach I’ve started to use more frequently is to just pass an anonymous function to the filter:

add_filter(‘post_class’, function($classes){
$classes[] = ‘another-css-class’;
return $classes;
}, 10);

This has a few advantages:

  • No clunkily named loose function knocking around the functions file
  • The hook being used is adjacent to the function.
  • You can pass in local variables if you need to like this:
$mySpecialValue = 87;
add_filter(‘post_class’, function($classes) use ($mySpecialValue) {
if(count($classes) == $mySpecialValue){
$classes[] = ‘has-88-css-classes’;
return $classes;
}, 10);

But disadvantages include the inability to use the function on multiple hooks, and crucially the ability to REMOVE the hooked function if you need to later. You can’t use the WordPress remove_filter method using anonymous functions.

For the vast majority of cases though, this has worked well for me. I could always define the anonymous function as a variable if I needed to use it on multiple filters but that removal thing became a problem that I needed to solve.

The problem

I had a bit of a hacky filter that I needed to run on ‘posts_where’ to modify the main query. Once applied to the main query, however, it was subsequently applied to any and all other queries used within the page building process. In the past I’d removed this type of filter by use of the overly-convoluted method of applying an action to remove the filter on some arbitrary filter following the main query being run. This results in more cruft drifting about in the functions file. It also requires recall of the priority value since the removal parameters must match. If at some point in the future you, or another developer (you of the future is another developer), changes the priority on the original hook then your remove operation (which is buried in the body of an unrelated function somewhere else) won’t work any more. Your IDE won’t tell you about it either since this code is impenetrable.

I decided to see if I could make self-removing filter.

Introducing the Closure class

namespace MyNamespace;

class closure {
public $priority = 10;
public $method;
public $args = 1;
public $hook;
public $persistent = false;
public $hookType;

const TYPE_ACTION = 1;
const TYPE_FILTER = 2;

// Instantiators
public static function createFilter($hook, $method, $priority=10, $args=1, $persistent=false, $hookType=self::TYPE_FILTER){
    $closure = new static();
    if(is_callable($method)) {
        $closure-&gt;hookType = $hookType;
        $closure-&gt;hook = $hook;
        $closure-&gt;method = $method;
        $closure-&gt;priority = $priority;
        $closure-&gt;args = $args;
        $closure-&gt;persistent = $persistent;
    } else {
        $closure = false;
    return $closure;

public static function createAction($hook, $method, $priority=10, $args=1, $persistent=false){
    return static::createFilter($hook, $method, $priority, $args, $persistent, self::TYPE_ACTION);

public static function createPersistentFilter($hook, $method, $priority=10, $args=1){
    return static::createFilter($hook, $method, $priority, $args, true);

public static function createPersistentAction($hook, $method, $priority=10, $args=1){
    return static::createAction($hook, $method, $priority, $args, true);

// public API [WordPress needs public visibility]
public function filter(){
    $args = func_get_args();
    $val = $args[0];
        $val = call_user_func_array($this-&gt;method, $args);
    if(!$this-&gt;persistent) {
    if($this-&gt;hookType == self::TYPE_FILTER) {
        return $val;
    return true;

// Utility methods
public static function apply(closure $closure){
    global $wheels;
    if($closure-&gt;hookType == self::TYPE_ACTION){
        add_action($closure-&gt;hook, array($closure, 'filter'), $closure-&gt;priority, $closure-&gt;args);
    } else {
        add_filter($closure-&gt;hook, array($closure, 'filter'), $closure-&gt;priority, $closure-&gt;args);

public static function remove(closure $closure){
    global $wheels;
    if($closure-&gt;hookType == self::TYPE_ACTION){
        remove_action($closure-&gt;hook, array($closure, 'filter'), $closure-&gt;priority);
    } else {
        remove_filter($closure-&gt;hook, array($closure, 'filter'), $closure-&gt;priority);



As long as it’s created using the createFilter or createAction instantiation methods, then by default, a closure object will remove it’s own hook the first time it’s employed. You can use the persistent methods if you don’t want it to do that, but as I’m not envisaging using this for every single action and filter, only for ones like my posts_where where I need it removed following its first application, then I’m happy for that to be the default.

Here’s an example of the closure class in use:

use MyNamespace\closure;

$mySpecialVar = 4077;
closure::createFilter(‘posts_where’, function($where) use ($mySpecialVar) {
$currentWhere = preg_replace(“'^\s?AND\s’i”, ‘’, $where);
$where = ’ AND wp_posts.ID IN (SELECT
DISTINCT mp_a.meta_value
FROM wp_postmeta mp_a
WHERE mp_a.meta_key = ‘my_meta_key_im_looking_for’
AND mp_a.post_id = ‘.(1*$mySpecialVar).’)
OR (’ . $currentWhere . ') ';
return $where;

If I needed to I could still store the closure object as a variable and use it on multiple filters. In fact I could probably add a method to the class to accept multiple filter strings to keep everything together.

Because the closure object remembers its own arguments, should you retain a copy of the closure you may at any time use the method:


To remove the hook. Changing the priority in the original closure creation won’t now affect the removal.

Thanks for reading. If you liked this post, share it with all of your programming buddies!

Further reading

☞ PHP with Laravel for beginners - Become a Master in Laravel

☞ Projects in Laravel: Learn Laravel Building 10 Projects

☞ Laravel for RESTful: Build Your RESTful API with Laravel

☞ Fullstack Web Development With Laravel and Vue.js

☞ Laravel 5.8 Ajax CRUD tutorial using Datatable JS

☞ Laravel 5.8 Tutorial from Scratch for Beginners

☞ Build RESTful API In Laravel 5.8 Example

☞ Login with Google in Laravel 5.8 App using Socialite Package

☞ Laravel PHP Framework Tutorial - Full Course for Beginners (2019)

This post was originally published here

#php #wordpress #web-development

What is GEEK

Buddha Community

How to Preventing free-range Wordpress hooks

Why Use WordPress? What Can You Do With WordPress?

Can you use WordPress for anything other than blogging? To your surprise, yes. WordPress is more than just a blogging tool, and it has helped thousands of websites and web applications to thrive. The use of WordPress powers around 40% of online projects, and today in our blog, we would visit some amazing uses of WordPress other than blogging.
What Is The Use Of WordPress?

WordPress is the most popular website platform in the world. It is the first choice of businesses that want to set a feature-rich and dynamic Content Management System. So, if you ask what WordPress is used for, the answer is – everything. It is a super-flexible, feature-rich and secure platform that offers everything to build unique websites and applications. Let’s start knowing them:

1. Multiple Websites Under A Single Installation
WordPress Multisite allows you to develop multiple sites from a single WordPress installation. You can download WordPress and start building websites you want to launch under a single server. Literally speaking, you can handle hundreds of sites from one single dashboard, which now needs applause.
It is a highly efficient platform that allows you to easily run several websites under the same login credentials. One of the best things about WordPress is the themes it has to offer. You can simply download them and plugin for various sites and save space on sites without losing their speed.

2. WordPress Social Network
WordPress can be used for high-end projects such as Social Media Network. If you don’t have the money and patience to hire a coder and invest months in building a feature-rich social media site, go for WordPress. It is one of the most amazing uses of WordPress. Its stunning CMS is unbeatable. And you can build sites as good as Facebook or Reddit etc. It can just make the process a lot easier.
To set up a social media network, you would have to download a WordPress Plugin called BuddyPress. It would allow you to connect a community page with ease and would provide all the necessary features of a community or social media. It has direct messaging, activity stream, user groups, extended profiles, and so much more. You just have to download and configure it.
If BuddyPress doesn’t meet all your needs, don’t give up on your dreams. You can try out WP Symposium or PeepSo. There are also several themes you can use to build a social network.

3. Create A Forum For Your Brand’s Community
Communities are very important for your business. They help you stay in constant connection with your users and consumers. And allow you to turn them into a loyal customer base. Meanwhile, there are many good technologies that can be used for building a community page – the good old WordPress is still the best.
It is the best community development technology. If you want to build your online community, you need to consider all the amazing features you get with WordPress. Plugins such as BB Press is an open-source, template-driven PHP/ MySQL forum software. It is very simple and doesn’t hamper the experience of the website.
Other tools such as wpFoRo and Asgaros Forum are equally good for creating a community blog. They are lightweight tools that are easy to manage and integrate with your WordPress site easily. However, there is only one tiny problem; you need to have some technical knowledge to build a WordPress Community blog page.

4. Shortcodes
Since we gave you a problem in the previous section, we would also give you a perfect solution for it. You might not know to code, but you have shortcodes. Shortcodes help you execute functions without having to code. It is an easy way to build an amazing website, add new features, customize plugins easily. They are short lines of code, and rather than memorizing multiple lines; you can have zero technical knowledge and start building a feature-rich website or application.
There are also plugins like Shortcoder, Shortcodes Ultimate, and the Basics available on WordPress that can be used, and you would not even have to remember the shortcodes.

5. Build Online Stores
If you still think about why to use WordPress, use it to build an online store. You can start selling your goods online and start selling. It is an affordable technology that helps you build a feature-rich eCommerce store with WordPress.
WooCommerce is an extension of WordPress and is one of the most used eCommerce solutions. WooCommerce holds a 28% share of the global market and is one of the best ways to set up an online store. It allows you to build user-friendly and professional online stores and has thousands of free and paid extensions. Moreover as an open-source platform, and you don’t have to pay for the license.
Apart from WooCommerce, there are Easy Digital Downloads, iThemes Exchange, Shopify eCommerce plugin, and so much more available.

6. Security Features
WordPress takes security very seriously. It offers tons of external solutions that help you in safeguarding your WordPress site. While there is no way to ensure 100% security, it provides regular updates with security patches and provides several plugins to help with backups, two-factor authorization, and more.
By choosing hosting providers like WP Engine, you can improve the security of the website. It helps in threat detection, manage patching and updates, and internal security audits for the customers, and so much more.

Read More

#use of wordpress #use wordpress for business website #use wordpress for website #what is use of wordpress #why use wordpress #why use wordpress to build a website

Hire WordPress Developer

Whether you want to develop a blog or you want a feature-rich, interactive WordPress website? is a distinguished leader in the WordPress development market. Hire WordPress Developer that develop easy-to-manage and high-performance WordPress websites that deliver the kind of results you have always wished for!

Consult with experts:

#hire wordpress developer #wordpress #wordpress developer #wordpress development company #wordpress development services #wordpress development

Juned Ghanchi


Wordpress Development India, Hire Wordpress Developers

Hire WordPress developers from IndianAppDevelopers on an hourly or full-time basis to build advanced custom WordPress applications. Our WordPress developers have 5+ years of experience building websites, themes and plugins for small- and large-scale businesses.

You can hire highly knowledgeable WordPress developers in India from us to maintain and deliver the highest quality standards on-time solutions.

Looking to outsource a WordPress development project? Or want to hire WordPress developers? Then, get in touch with us.

#wordpress development india #hire wordpress developers india #wordpress development #wordpress developers #wordpress programmers #hire wordpress programmers

Hire Dedicated Wordpress Developer

Looking for a WordPress Development Company to revamp your website?

Hire Dedicated WordPress Developers from, with absolute flexibility as per your business requirements such as hourly basis, monthly basis, part-time, full-time, or just according to their project requirements, with vast experience of WordPress projects of all sizes & industry verticals with complete client satisfaction.

Consult with experts:-

#hire dedicated wordpress developer #wordpress development company #wordpress development services #wordpress development #wordpress developer #wordpress

Best WordPress Development Services in India

WordPress is used by many bloggers during the world which could be the explanation for some believe it to be the foremost prevalent blogging apparatus on the web. If you’re thinking of putting up a blog site but don’t know any coding languages (or even what code is) then your next best choice can be signing up for a WordPress website. you’ll easily register for a free WordPress blog account if you’re just starting and getting a for-pay WordPress blog account in a while.

WordPress is extremely user-friendly so beginners won’t find it hard to deal with fixing their new blog account. you simply got to give the proper information about yourself and choose the blog theme for your site from the list WordPress will offer you. There are various WordPress development companies within the market offering high-quality, fast, and all-inclusive WordPress services to clients. But, only some of them are genuine. So, it becomes difficult for a standard individual to decide on a competent WordPress development company for web development projects? Are you searching for an appropriate WordPress Development Company that would help you get the required WordPress site with all essential features? Skenix infotech is a prominent solution for you. Skenix Infotech is a leading IT company located in the USA & India and offers you all-inclusive WordPress Development Services at affordable prices and with quality to all needy individuals around the World.

Benefits of using WordPress for your website:

Since it’s starting in 2003, WordPress has been the foremost popular blogging platform within the market, and over the past few years, the platform is also getting appreciated as a content management software by non-blogging web platforms. Here are some reasons why WordPress is getting the most attention in very little time.

Search Engine Loves WordPress Sites:

WordPress is endued with a clean and easy coding behind it, by which the search engines don’t need to hustle much in reading and indexing any site’s content. Moreover, you’ll add meta tag keywords, descriptions, and titles for every page, post, and image, which can help in making precise search engine optimization.

The blog is Built-in and prepared to Go:

WordPress made its debut as a blogging platform, hence, it comes with some easy to use built-in blogging capabilities like fixing RSS / email subscriptions to your blog, commenting capability & automatically adding the foremost recent blog posts to other pages of the site are very simple while functioning on this platform.

Easy to use:

WordPress has been designed in an especially easy to use and intuitive interface. Adding blog posts, new pages, and pictures, etc regularly is simply as a piece of cake on this platform. The easiness of this platform reduces an excellent amount of your working time.

Have multiple users:

The administrator of a WordPress website also has the correct to set up multiple users for the website. Moreover, he/she also can assign different access levels and capabilities to each user.

Customizable Web Design:

Another advantage of the WordPress interface (Admin panel) is that it helps you to make any changes to the design and layout of the website, such as background image/color, display of sidebars, etc. without even using any FTP software.

No HTML Editing or FTP Software Required:

Since WordPress has its inbuilt editor, you’ll perform all the coding operations right from your website’s admin panel. No got to pay money for or use any external editing software like Dreamweaver or Notepad++. WordPress editor features a very friendly user-interface where you won’t even need to edit the code for performing various tasks.

Hire WordPress Developers from Skenix Infotech:

Hire dedicated, skilled, and highly experiences WordPress developer at affordable rates at Skenix Infotech which is the prominent WordPress Development Company in India and the USA, Click here to know about: Hire WordPress Developers

#wordpress development services #wordpress development company in india #hire wordpress developers #wordpress development company usa #wordpress web development #wordpress