钩子文档

add_link

💡 云策文档标注

概述

add_link 是一个 WordPress 动作钩子,在链接被添加到数据库后触发,允许开发者执行自定义函数。

关键要点

  • 触发时机:当新链接被添加到 WordPress 数据库时触发。
  • 参数:$link_id,表示被添加链接的 ID。
  • 用途:用于在链接添加后执行自定义操作,如日志记录、数据同步等。
  • 相关函数:与 wp_insert_link() 函数关联,用于插入或更新链接。

代码示例

// 添加动作
add_action( 'add_link', 'wpdocs_execute_on_add_link_event' );

// 定义回调函数
function wpdocs_execute_on_add_link_event( $link_id ) {
    // 自定义代码,当新链接添加时执行
    // 使用 $link_id 参数进行与新链接相关的操作
}

// 移除动作(如果需要)
remove_action( 'add_link', 'wpdocs_execute_on_add_link_event' );

注意事项

  • 确保自定义函数在使用 add_action 钩入之前已定义。
  • add_action 和 remove_action 中的优先级和参数数量应匹配。
  • 可以向同一钩子添加多个函数,它们将按优先级顺序执行。

📄 原文内容

Fires after a link was added to the database.

Parameters

$link_idint
ID of the link that was added.

Source

do_action( 'add_link', $link_id );

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Overview
    The add_link action hook is triggered in WordPress when a new link is added. You can use this hook to execute custom functions whenever a link is added to the WordPress database.

    // Add the action
    add_action( 'add_link', 'wpdocs_execute_on_add_link_event' );
    
    // Define the callback function
    function wpdocs_execute_on_add_link_event( $link_id ) {
        // Custom code to be executed when a new link is added
        // Use the $link_id parameter to perform operations related to the new link
    }
    
    // Remove the action (if needed)
    remove_action( 'add_link', 'wpdocs_execute_on_add_link_event' );

    Notes

    1. Ensure that your custom function is defined before you hook it using add_action.
    2. The priority and number of arguments in add_action and remove_action should match.
    3. You can add multiple functions to the same hook, and they will be executed in the order of their priority.

    By following the steps above, you can effectively use the add_link action hook to add custom functionality whenever a new link is added in WordPress.