list of some daily uses WordPress hooks with example

0
5K

 

  • wp_head Action Hook:

    • Description: Used to inject code into the <head> section of the HTML document.
    • Example:
       
      function custom_code_in_head() { echo '<meta name="description" content="Custom description">'; } add_action('wp_head', 'custom_code_in_head');
  • wp_footer Action Hook:

    • Description: Allows insertion of code just before the closing </body> tag.
    • Example:
       
      function custom_code_in_footer()
      { echo '<script>alert("Hello, World!");</script>'; }
       
      add_action('wp_footer', 'custom_code_in_footer');
  • init Action Hook:

    • Description: Used for initializing code or actions.
    • Example:
       
      function custom_init_function() { // Your initialization code here } add_action('init', 'custom_init_function');

 

2. Filter Hooks:

the_content Filter Hook:

 

    • Description: Allows modification of the content before it is displayed.
    • Example:
       
      function custom_content_filter($content) { return $content . '<p>Custom content added.</p>'; }
      add_filter('the_content', 'custom_content_filter');
  • the_title Filter Hook:

    • Description: Used to modify the post/page title before it is displayed.
    • Example:
      function custom_title_filter($title)
      { return 'Custom Prefix: ' . $title; }
      add_filter('the_title', 'custom_title_filter');
  • excerpt_length Filter Hook:

    • Description: Adjusts the length of the excerpt.
    • Example:
      function custom_excerpt_length($length)
      { return 20; // Set the desired length }
      add_filter('excerpt_length', 'custom_excerpt_length');

admin_menu Action Hook:

  • Description: Used to add menu items to the WordPress admin menu.
  • Example:
    function add_custom_menu()
    { add_menu_page('Custom Page', 'Custom Menu', 'manage_options', 'custom-page', 'custom_page_callback'); }
    add_action('admin_menu', 'add_custom_menu'); function custom_page_callback() { // Content for the custom admin page }

    widgets_init Action Hook:

    • Description: Enables the registration of custom widgets.
    • Example:
      function register_custom_widget() { register_widget('Custom_Widget_Class'); }
      add_action('widgets_init', 'register_custom_widget'); class Custom_Widget_Class extends WP_Widget
      { // Custom widget implementation }

    the_excerpt Filter Hook:

    • Description: Modifies the excerpt content before it is displayed.
    • Example:
      function custom_excerpt_filter($excerpt)
      {
      return $excerpt . ' <a href="' . get_permalink() . '">Read more</a>';
      }
      add_filter('the_excerpt', 'custom_excerpt_filter');
     
     
     

    login_headerurl Filter Hook:

    • Description: Modifies the login page header URL.
    • Example:
    • function custom_login_url() {
          return home_url('/');
      }
      add_filter('login_headerurl', 'custom_login_url');
     
     

    wp_nav_menu_items Filter Hook:

    • Description: Adds custom items to the navigation menu.
    • Example:
    • function add_custom_menu_item($items, $args) {
          if ($args->theme_location == 'primary') {
              $items .= '<li><a href="#">Custom Link</a></li>';
          }
          return $items;
      }
      add_filter('wp_nav_menu_items', 'add_custom_menu_item', 10, 2);
    • save_post Action Hook:

      • Description: Triggered after a post or page is saved in the database.
      • Example:
      • function custom_save_post_callback($post_id, $post, $update) {
            // Custom logic to be executed after post/page is saved
        }
        add_action('save_post', 'custom_save_post_callback', 10, 3);

      wp_enqueue_scripts Action Hook:

      • Description: Used to enqueue scripts and styles on the front end.
      • Example:
      • function enqueue_custom_scripts() {
            wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0', true);
        }
        add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
      • the_title Filter Hook:

        • Description: Modifies the post or page title before it is displayed.
        • Example:
        • function custom_title_filter($title) {
              return 'Custom Prefix: ' . $title;
          }
          add_filter('the_title', 'custom_title_filter');

      excerpt_length Filter Hook:

      • Description: Modifies the number of words in the excerpt.
      • Example:
      • function custom_excerpt_length($length)
      • { return 20; // Set the custom excerpt length }
      • add_filter('excerpt_length', 'custom_excerpt_length');

      body_class Filter Hook:

      • Description: Adds custom classes to the body element.
      • Example:function add_custom_body_class($classes) {
            $classes[] = 'custom-class';
            return $classes;
        }
        add_filter('body_class', 'add_custom_body_class');
Sponsorizzato
Cerca
Sponsorizzato
Sponsorizzato
WordPress Quick Solution
Categorie
Leggi tutto
Networking
How to Use the New JSX Transform in WordPress 6.6
WordPress 6.6 introduces significant enhancements, including improved support for JSX (JavaScript...
By Wp India 2024-06-25 17:17:53 0 4K
Altre informazioni
Ultra-Thin Glass Market 2023 Overall Industry Size, Growth, Trends, Competitor Analysis & Forecast up to 2032
The ultra-thin glass market is experiencing rapid growth and is poised to expand...
By shubham7007 2023-10-06 03:30:56 0 9K
Networking
Essential Steps Every Website Owner Should Follow for Online Success
Building a website is the first step toward establishing a digital presence, but making it...
By Wp India 2024-11-04 18:35:03 0 4K
Shopping
E-Commerce vs. WooCommerce: Choosing the Right Platform for Your Online Business
Overview: E-Commerce is a generic term that refers to electronic commerce, encompassing any...
By Wp India 2023-12-08 17:16:53 0 9K
Networking
Which step how much improved your WordPress website while optimization
Optimizing a WordPress website is a step-by-step process where each step can significantly...
By abhira 2025-01-16 17:11:04 0 3K
Abhira Social Media https://abhira.in