All Projects → stevegrunwell → One Time Callbacks

stevegrunwell / One Time Callbacks

Licence: mit
Enable WordPress actions and filter callbacks to be called exactly once.

Projects that are alternatives of or similar to One Time Callbacks

Genesis Simple Hook Guide
WordPress plugin that displays names of all Genesis hooks on the current page dynamically.
Stars: ✭ 25 (-57.63%)
Mutual labels:  wordpress, hooks
Debug Objects
WordPress Plugin for debugging and learning with and at the application.
Stars: ✭ 98 (+66.1%)
Mutual labels:  wordpress, hooks
Framework
Assely is a PHP framework which brings a little joy to the WordPress development. Develop structured, easily scalable and complex WordPress websites and web applications with true pleasure.
Stars: ✭ 53 (-10.17%)
Mutual labels:  wordpress
Spinupwp Composer Site
A WordPress site setup using Composer that is primed and ready to be hosted using SpinupWP.
Stars: ✭ 58 (-1.69%)
Mutual labels:  wordpress
Better Rest Endpoints
A WordPress plugin that serves up slimmer WP Rest API endpoints.
Stars: ✭ 56 (-5.08%)
Mutual labels:  wordpress
Gravityblocks
Gravity Forms blocks for Gutenberg, the new WordPress editor
Stars: ✭ 54 (-8.47%)
Mutual labels:  wordpress
Go Search Replace
🚀 Search & replace URLs in WordPress SQL files.
Stars: ✭ 57 (-3.39%)
Mutual labels:  wordpress
Keyring
Keyring is an authentication framework for WordPress. It comes with definitions for a variety of HTTP Basic, OAuth1 and OAuth2 web services. Use it as a common foundation for working with other web services from within WordPress code.
Stars: ✭ 52 (-11.86%)
Mutual labels:  wordpress
Ansible Wordpress
Ansible role to set up (multiple) wordpress installations in Debian-like systems (using wp-cli)
Stars: ✭ 58 (-1.69%)
Mutual labels:  wordpress
Smart Media
Smart Media enhancements for WordPress
Stars: ✭ 56 (-5.08%)
Mutual labels:  wordpress
Themosis
A framework for WordPress developers.
Stars: ✭ 1,089 (+1745.76%)
Mutual labels:  wordpress
Pcsgolh
PCSGOLH - Pointless Counter-Strike: Global Offensive Lua Hooks. A open-source Lua API for CS:GO hacking written in modern C++
Stars: ✭ 56 (-5.08%)
Mutual labels:  hooks
Wl Bootstrap
Integrating Laravel into WordPress
Stars: ✭ 54 (-8.47%)
Mutual labels:  wordpress
Genesis Starter Theme
This repo is no longer maintained. The starter theme can now be found at:
Stars: ✭ 57 (-3.39%)
Mutual labels:  wordpress
Caddy Script
🐳 Caddy installation script
Stars: ✭ 53 (-10.17%)
Mutual labels:  wordpress
Aspects
Delightful, simple library for aspect oriented programming in Objective-C and Swift.
Stars: ✭ 8,255 (+13891.53%)
Mutual labels:  hooks
Phpwpinfo
Provides an equivalent to the `phpinfo()` but with more WordPress requirements details.
Stars: ✭ 52 (-11.86%)
Mutual labels:  wordpress
Lightning
Lightning is powerful them for WordPress.
Stars: ✭ 55 (-6.78%)
Mutual labels:  wordpress
Wordpress Zero Spam
The WordPress Zero Spam plugin makes blocking spam a cinch without all the bloated options. Just install, activate, and say goodbye to spam.
Stars: ✭ 56 (-5.08%)
Mutual labels:  wordpress
Wordpress Basis Theme
I'm a WordPress starter theme.
Stars: ✭ 58 (-1.69%)
Mutual labels:  wordpress

One-Time Callbacks

Build Status Coverage Status GitHub release

The The WordPress plugin API is a fantastic way for third-party scripts to be able to inject themselves into the WordPress lifecycle. Thanks to WordPress actions and filters (collectively "hooks"), theme and plugin developers can introduce all sorts of new functionality.

Occasionally, however, the "all or nothing" mentality of WordPress hooks can put developers in a pinch, since they only want their callback to run once. For example, maybe your theme has a simple add_top_story_class() function, which appends .top-story to a list of classes. If you only wanted to apply it to the first post in a loop, you might find yourself writing code like this:

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

  <?php if ( 0 === $query->current_post ) add_filter( 'post_class', 'add_top_story_class' ); ?>

  <article <?php post_class(); ?>>
    <h2><?php the_title(); ?></h2>
    <?php the_excerpt(); ?>
  </article>

  <?php remove_filter( 'post_class', 'add_top_story_class' ); ?>

<?php endwhile; ?>

Yuck! We're conditionally adding a filter based on the current post's index, then removing the filter at the end to ensure it isn't getting applied to every post.

With One-time callbacks, this becomes much cleaner:

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

  <?php add_filter_once( 'post_class', 'add_top_story_class' ); ?>

  <article <?php post_class(); ?>>
    <h2><?php the_title(); ?></h2>
    <?php the_excerpt(); ?>
  </article>

<?php endwhile; ?>

The add_filter_once() function will let the callback execute exactly one time, then it will automatically clean up after itself. It's a small but helpful tool for themes and plugins that make heavy use of actions and filters.

Installation

The best way to install this package is via Composer:

$ composer require stevegrunwell/one-time-callbacks

The package ships with the composer/installers package, enabling you to control where you'd like the package to be installed. For example, if you're using One-time Hooks in a WordPress plugin, you might store the file in an includes/ directory. To accomplish this, add the following to your plugin's composer.json file:

{
    "extra": {
        "installer-paths": {
            "includes/{$name}/": ["stevegrunwell/one-time-callbacks"]
        }
    }
}

Then, from within your plugin, simply include or require the file:

require_once __DIR__ . '/includes/one-time-callbacks/one-time-callbacks.php';

Using as a plugin

If you'd prefer, the package also includes the necessary file headers to be used as a WordPress plugin.

After downloading or cloning the package, move one-time-callbacks.php into either your wp-content/mu-plugins/ (preferred) or wp-content/plugins/ directory. If you chose the regular plugins directory, you'll need to activate the plugin manually via the Plugins › Installed Plugins page within WP Admin.

Bundling within a plugin or theme

One-time Callbacks has been built in a way that it can be easily bundled within a WordPress plugin or theme, even commercially.

Each function declaration is wrapped in appropriate function_exists() checks, ensuring that multiple copies of the library can co-exist in the same WordPress environment.

Usage

One-time Callbacks provides the following functions for WordPress:

add_action_once()

Register an action to run exactly one time.

The arguments match that of add_action(), but this function will also register a second callback designed to remove the first immediately after it runs.

Parameters

(string) $hook
The action name.
(callable) $callback
The callback function.
(int) $priority
Optional. The priority at which the callback should be executed. Default is 10.
(int) $args
Optional. The number of arguments expected by the callback function. Default is 1.

Return value

Like add_action(), this function always returns true.

add_filter_once()

Register a filter to run exactly one time.

The arguments match that of add_filter(), but this function will also register a second callback designed to remove the first immediately after it runs.

Parameters

(string) $hook
The action name.
(callable) $callback
The callback function.
(int) $priority
Optional. The priority at which the callback should be executed. Default is 10.
(int) $args
Optional. The number of arguments expected by the callback function. Default is 1.

Return value

Like add_filter(), this function always returns true.

License

Copyright 2018 Steve Grunwell

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].