Cách sử dụng Filter Hook trong WordPress

48 sec read

filter hook wordpress

Để có thể trở thành deverloper thực thụ về WordPress thì bạn sẽ phải biết cách sử dụng hook (add_action(), add_filter()), nó là thứ cốt lõi trong WordPress. Hook cũng được sử dụng trong Core của WP. WordPress có 2 kiểu Hook chính: Action hook và Filter hook.
Trong bài hôm nay chúng ta sẽ tìm hiểu về Filter hook

Filter hook là gì?

Filter Hook là một điểm neo được khai báo trong mã nguồn WordPress, plugin hoặc theme để chúng ta có thể sửa lại giá trị của biến PHP tại nơi mà điểm neo đó đã được khai báo.

<?php
    $copyright = ‘Design by Ncc’;
    echo apply_filters( ‘ncc_copyright’, $copyright );
?>

Đặc tính

Dùng để làm thay đổi giá trị

function ncc_change_copyright( $output ) {
    $output = ‘Design by Ncc Soft’;
    return $output;
}
add_filter( ‘ncc_copyright’, ‘ncc_change_copyright’ );

Bây giờ đoạn text “Design by Ncc” đã được thay thế thành đoạn text mới “Design by Ncc Soft” nhờ sử dụng filter.

Cách sử dụng filter hook

Đây là những filter có sẵn trong WordPress:

https://codex.wordpress.org/Plugin_API/Filter_Reference
Avatar photo

𝐌𝐄𝐙𝐎𝐍 – 𝐓𝐇𝐄 𝐔𝐋𝐓𝐈𝐌𝐀𝐓𝐄 𝐏𝐋𝐀𝐓𝐅𝐎𝐑𝐌 𝐅𝐎𝐑 𝐂𝐎𝐌𝐌𝐔𝐍𝐈𝐓𝐈𝐄𝐒 & 𝐁𝐔𝐒𝐈𝐍𝐄𝐒𝐒

Discord is a widely used communication platform, but is it truly the best solution for community and business management? While it excels in creating...
Avatar photo Ngan Ton Thuy
3 min read

𝐌𝐞𝐳𝐨𝐧 𝐯𝐬 𝐒𝐥𝐚𝐜𝐤 – 𝐖𝐡𝐢𝐜𝐡 𝐎𝐧𝐞 𝐄𝐧𝐡𝐚𝐧𝐜𝐞𝐬 𝐘𝐨𝐮𝐫 𝐁𝐮𝐬𝐢𝐧𝐞𝐬𝐬…

Both Mezon and Slack are powerful communication platforms, but which one is the right fit for your organization? Let’s compare their key features: 𝐔𝐬𝐚𝐠𝐞...
Avatar photo Ngan Ton Thuy
1 min read

𝐌𝐄𝐙𝐎𝐍 𝐂𝐀𝐌𝐏𝐔𝐒 𝐏𝐀𝐑𝐓𝐍𝐄𝐑 — 𝐄𝐦𝐩𝐨𝐰𝐞𝐫𝐢𝐧𝐠 𝐒𝐭𝐮𝐝𝐞𝐧𝐭-𝐋𝐞𝐝 𝐈𝐧𝐢𝐭𝐢𝐚𝐭𝐢𝐯

Mezon Campus Partner is an all-in-one program designed to support: – Student competitions – Career development initiatives – Student-led clubs and creative teams 𝐖𝐡𝐚𝐭...
Avatar photo Ngan Ton Thuy
44 sec read

Leave a Reply

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