Skip to content

Instantly share code, notes, and snippets.

@josanua
Last active November 10, 2024 22:28
Show Gist options
  • Select an option

  • Save josanua/86f65ffa29c798910b0e195df771db64 to your computer and use it in GitHub Desktop.

Select an option

Save josanua/86f65ffa29c798910b0e195df771db64 to your computer and use it in GitHub Desktop.
wp hooks
<?php
/**
*
* The WordPress Hooks
*
*/
https://kinsta.com/blog/wordpress-hooks/
// loading order
https://carlalexander.ca/wordpress-adventurous-loading/#plugins_loaded
/**
*
* About pluggable functions.
*
*/
// Override parent theme functions
// https://webdesign.tutsplus.com/tutorials/a-guide-to-overriding-parent-theme-functions-in-your-child-theme--cms-22623
// To write a pluggable function, you simply enclose it in a conditional tag to check if a function with that name has already been run:
if ( ! function_exists ( 'my_function' ) ) {
function my_function() {
// Contents of your function here.
}
}
// Then, when you come to write a function in your child theme which you want to override the one in the parent theme, you just give it the same name as the one in the parent theme:
function my_function() {
// Contents for your function override here.
}
//--> Remove actions
https://developer.wordpress.org/reference/functions/remove_action/
// Remove actions declared in a class
https://zeropointdevelopment.com/how-to-remove-an-action-hook-or-filter-added-by-a-class-in-wordpress/
// action
add_action( 'admin_menu', 'callback_function' );
https://tommcfarlin.com/remove-wordpress-meta-boxes/
https://developer.wordpress.org/reference/hooks/default_hidden_meta_boxes/
add_action( 'default_hidden_meta_boxes', 'acme_remove_meta_boxes', 10, 2 );
/**
* Removes the category, author, post excerpt, and slug meta boxes.
*
* @since 1.0.0
*
* @param array $hidden The array of meta boxes that should be hidden for Acme Post Types
* @param object $screen The current screen object that's being displayed on the screen
* @return array $hidden The updated array that removes other meta boxes
*/
function acme_remove_meta_boxes( $hidden, $screen ) {
if ( 'acme_post_type' == $screen->id ) {
$hidden = array(
'acme_post_type_categorydiv',
'authordiv',
'postexcerpt',
'slugdiv'
);
}
return $hidden;
}
// Posts Actions
https://wordpress.stackexchange.com/questions/64072/when-is-the-new-status-post-post-type-transition-hook-fired
https://www.wp-plugin-api.com/hook/new_status_post-post_type/?version=wordpress-5.3.2
https://wordpress.org/support/article/post-status/
// examples, send mails then custom posts are changed
add_action('publish_laundries', 'myfunction');
function myfunction(){
$to = 'a-josanu@office.simpals.com';
$subject = 'Published laundries post';
$message = "Your laundries is published at: " . get_permalink($post_id);
wp_mail($to, $subject, $message );
// echo 'Posts Added';
}
add_action('draft_laundries', 'myfunction');
function myfunction(){
$to = 'a-josanu@office.simpals.com';
$subject = 'Published laundries post';
$message = "Your laundries is published at: " . get_permalink($post_id);
wp_mail($to, $subject, $message );
// echo 'Posts Added';
}
************ Filters ************
// Filter content, add rel="lightbox" to link, add tag properties.
add_filter('the_content', 'my_addlightboxrel');
function my_addlightboxrel($content) {
global $post;
$pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 rel="lightbox" title="'.$post->post_title.'"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
/**
* Filter Primary Navigation to add navigation search.
*
* @param string $markup the markup for the navigation addons.
*
* @access public
* @return mixed
*/
add_filter( 'wp_nav_menu_args', 'modify_primary_menu');
function modify_primary_menu( $markup ) {
if ( 'primary' !== $markup['theme_location'] ) {
return $markup;
}
$markup['items_wrap'] = display_filtered_navigation();
return $markup;
}
// Display navigation.
function display_filtered_navigation() {
$nav = '<ul id="%1$s" class="%2$s">';
$nav .= '%3$s';
$nav .= apply_filters( 'hestia_after_primary_navigation_addons', search_in_menu() );
$nav .= '</ul>';
return $nav;
}
// Display search form in menu.
function search_in_menu() {
add_filter( 'get_search_form', 'filter_search_form' );
$form = get_search_form( false );
remove_filter( 'get_search_form','filter_search_form' );
return $form;
}
// Filter the search form to adapt to our needs.
function filter_search_form( $form ) {
$output = '';
$output .= '<li class="hestia-search-in-menu">';
$output .= '<div class="hestia-nav-search">';
$output .= $form;
$output .= '</div>';
$output .= '<a class="hestia-toggle-search"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="16" height="16"><path d="M505 442.7L405.3 343c-4.5-4.5-10.6-7-17-7H372c27.6-35.3 44-79.7 44-128C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c48.3 0 92.7-16.4 128-44v16.3c0 6.4 2.5 12.5 7 17l99.7 99.7c9.4 9.4 24.6 9.4 33.9 0l28.3-28.3c9.4-9.4 9.4-24.6.1-34zM208 336c-70.7 0-128-57.2-128-128 0-70.7 57.2-128 128-128 70.7 0 128 57.2 128 128 0 70.7-57.2 128-128 128z"></path></svg></a>';
$output .= '</li>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment