Hooks in XenForo 2 are a powerful way to help you customize and extend the functionality of your forum without directly editing the native code. By using hooks, you can intervene in the forum’s operations at certain points, make changes, or add new features while still maintaining the stability of the system. In this article, we will show you how to use hooks to customize functionality on XenForo 2.

How to Use Hooks to Customize Functions
Illustrations.
Table of Contents

    1. Understanding Hooks On XenForo 2

    Hooks in XenForo are preseeding points in source code that allow you to attach code snippets or extend functionality without making direct changes to the original files. They are often used in situations such as:

    • Customize the appearance of the interface.
    • Add or edit data processing routines.
    • Integrate with external systems.

    Using hooks makes your code more manageable, avoiding conflicts when XenForo updates to a new version.

    2. Using Hooks In Templates

    One of the common ways to use hooks is to insert them into templates to change the look and feel of the forum. XenForo provides many hooks in the main templates, you can take advantage of them to add HTML, CSS, or JavaScript code.

    For example, to add custom code to the bottom of each page, you can use hooks PAGE_CONTAINER as follows:

    <!-- Hook: PAGE_CONTAINER --> 
    <xen:hook name="page_container_content_top">
        <!-- Nội dung của bạn -->
        <div class="my-custom-content">
            Nội dung tùy chỉnh của tôi.
        </div>
    </xen:hook>

    This hook will allow you to add custom content to the top of the page container without affecting the rest of the interface.

    See also  XenForo 2 API User Guide

    3. Create Custom Hooks In Add-on

    You can also create custom hooks when developing add-ons for XenForo. This makes it easy to extend and change the forum’s features without affecting the original code. To create a custom hook, you need to do the following steps:

    1. Create add-ons in XenForo through the admin console.
    2. Add a hook in your PHP code or add-on template using syntax $this->emit() or .

    For example, you want to create a hook to change the way data is handled when a user signs up for a new account. In the add-on’s PHP code, you can add a hook like this:

    // Tạo hook trong class xử lý đăng ký
    $this->emitter->emit('user_register', ($user));

    Any add-on can then listen and handle this event when a user signs up.

    4. Example of Hook to Customize Posting Function

    Suppose you want to add a custom option to a user’s post form, you can use a hook to add code to the post form interface without editing the original code.

    You can add custom code to the post form using the hooks available in the template thread_create:

    <xen:hook name="thread_create_fields_extra">
        <input type="checkbox" name="custom_option" value="1"> Tùy chọn tùy chỉnh của bạn.
    </xen:hook>

    This will add a custom checkbox option to the user’s post form.

    5. Listen and Handle Hooks

    Once you have created or used an existing hook, you need to listen and process the hook to perform the desired actions. This can be done in the add-on’s PHP code or your custom module.

    For example, if you added a hook for the new user registration event, you could listen for and handle this event in the add-on as follows:

    See also  Instructions for Installing SEO Add-ons for XenForo 2

    $this->emitter->on('user_register', function($user) {
        // Thực hiện hành động khi người dùng đăng ký
        log_activity('Người dùng mới đã đăng ký: ' . $user->username);
    });

    Every time a new user registration event is triggered, this action is performed, helping you extend functionality without making direct changes to the system.

    Customize functions

    Hooks are a powerful tool for customizing and extending XenForo features without affecting the original code. By using hooks in templates or add-ons, you have the flexibility to customize functionality to your needs while keeping the system stable and easy to maintain.

    Hopefully this guide will help you better understand how to use hooks to customize functionality on XenForo 2. Wishing you success in growing and expanding your forum!

    Leave a Reply

    Your email address will not be published. Required fields are marked *