Skip to content

Instantly share code, notes, and snippets.

@Web-Assembler
Created January 17, 2023 16:22
Show Gist options
  • Save Web-Assembler/4f174c34970e333a75ddb9b12252dfa7 to your computer and use it in GitHub Desktop.
Save Web-Assembler/4f174c34970e333a75ddb9b12252dfa7 to your computer and use it in GitHub Desktop.
Wordpress Edinburgh Meetup - Example actions and filters
<?php
/**
* Here is some custom code for the WordPress Edingburgh Hooks Talk titled
* "An introduction to WordPress Hooks". This demo uses the wordpress
* twentytwentythree theme.
*
* To use, drop this functions.php file within the theme.
*
* @package webassembler
*/
// FILTERS
/* ====================================== */
/**
* Example: Add a class onto the body tag.
* https://developer.wordpress.org/reference/hooks/body_class/
*
* With a filter, it's usually always the first argument that gets
* returned so in the case below, $classes would be the returned
* argument.
*/
function wa_add_a_class( $classes, $class ) {
$classes[] = 'webassembler';
// Example: Add class to body if list in use.
if ( has_block( 'core/list' ) ) {
$classes[] = 'my-page-has-a-list';
}
return $classes;
}
add_filter( 'body_class', 'wa_add_a_class', 10, 2 );
/**
* Example: How to add a class if a block is in use.
*/
function wa_modify_classes( $classes, $class ) {
if ( has_block( 'core/list' ) ) {
$classes[] = 'my-page-has-a-list';
}
return $classes;
}
add_filter( 'body_class', 'wa_modify_classes', 10, 2 );
/**
* Example: Change the post title
* https://developer.wordpress.org/reference/hooks/the_title/
*/
function wa_modify_title( $post_title, $post_id ) {
// Frontend only.
if ( ! is_admin() ) {
return 'hello world 👋 ' . $post_title;
} else {
return 'hello admin 🤠 ' . $post_title;
}
return $post_title;
}
add_filter( 'the_title', 'wa_modify_title', 10, 2 );
/**
* Example: Show how priorities work to order the functions.
*
* @return void
*/
function wa_content_custom_one( $content ) {
return $content . '<h3>Custom Heading 1</h3>';
}
add_filter( 'the_content', 'wa_content_custom_one', 11, 1 );
// two.
function wa_content_custom_two( $content ) {
return $content . '<h3>Custom Heading 2</h3>';
}
add_filter( 'the_content', 'wa_content_custom_two', 10, 1 );
// three.
function wa_content_custom_three( $content ) {
return $content . '<h3>Custom Heading 3</h3>';
}
add_filter( 'the_content', 'wa_content_custom_three', 9, 1 );
// ACTIONS
/* ====================================== */
/**
* Example: Add Analytics scripts to header
*/
function wa_add_analytics_to_head() {
// echo '<script>alert("hello world");</script>';
echo '<script type="text/javascript">// HELLO WORLD </script>';
}
add_action( 'wp_head', 'wa_add_analytics_to_head' );
/**
* Example: Load my own stylesheet
*/
function wa_enqueue_my_css() {
wp_enqueue_style( 'style-name', get_template_directory_uri() . '/my-styles.css' );
}
add_action( 'wp_enqueue_scripts', 'wa_enqueue_my_css' );
// Woocommerce
/* ====================================== */
/**
* Example: Change the woocommerce button text
* https://woocommerce.com/document/change-add-to-cart-button-text/
*/
function wa_change_add_to_cart_text( $text, $product ) {
if ( $product->is_type( 'variable' ) ) {
return 'MAKE A DECISION!';
}
return 'BUY NOW!';
}
add_filter( 'woocommerce_product_add_to_cart_text', 'wa_change_add_to_cart_text', 10, 2 );
/**
* Example: In the Admin > Posts list, add some text next to
* a post when a particular block is shown on that item
* https://developer.wordpress.org/reference/hooks/the_title/
*/
function wa_modify_title_admin( $post_title, $post_id ) {
if ( ! is_admin() ) {
return $post_title;
}
if ( has_block( 'core/list', $post_id ) ) {
$post_title = 'HAS LIST - ' . $post_title;
}
return $post_title;
}
add_filter( 'the_title', 'wa_modify_title_admin', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment