Skip to content

Instantly share code, notes, and snippets.

@laxmariappan
Created November 19, 2024 11:28
Show Gist options
  • Select an option

  • Save laxmariappan/f30aab48897fe219c1b249b352eac91f to your computer and use it in GitHub Desktop.

Select an option

Save laxmariappan/f30aab48897fe219c1b249b352eac91f to your computer and use it in GitHub Desktop.
WP hooks introduction
<?php
/**
* Plugin Name: WP Hooks Guide
* Description: A guide to WordPress hooks.
* Version: 1.0
* Author: Your Name
* Author URI: http://yourwebsite.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-hooks-guide
*
* @package WP_Hooks_Guide
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Example of the following actions and filters in WordPress:
*
* add_action()
* add_filter()
* do_action()
* apply_filters()
*/
/**
* Example of add_action() and do_action().
*
* @see https://developer.wordpress.org/reference/functions/add_action/
* @see https://developer.wordpress.org/reference/functions/do_action/
*/
// Register the custom action
add_action('footer_message', 'wp_hooks_guide_display_message');
function wp_hooks_guide_display_message() {
echo '<p>Message from the footer!</p>';
}
/**
* Register custom filter.
*/
add_filter('wp_hooks_guide_filter', 'wp_hooks_guide_filter_callback');
function wp_hooks_guide_filter_callback($content) {
$original = 'Original content: ' . $content;
$filtered = 'Filtered content: ' . substr($content, 0, 10);
return $original . '<br>' . $filtered;
}
// Fires before the product summary on the single product page.
add_action('woocommerce_before_single_product_summary', 'wp_hooks_guide_display_message');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment