WordPress 教程:在文章新增或保存后执行函数(函数.新增.保存.执行.教程...)

wufei123 发布于 2025-09-02 阅读(8)

wordpress 教程:在文章新增或保存后执行函数

本文旨在解决在 WordPress 中,当特定类型的文章(例如 "award_category")新增或更新后,需要自动创建或更新 WooCommerce 产品的问题。通过使用合适的 WordPress 钩子,确保在文章数据完全保存后执行相关函数,从而避免数据不一致的问题。本文将介绍两种可行的解决方案,并提供详细的代码示例和注意事项。

解决方案一:使用 wp_after_insert_post 钩子

wp_after_insert_post 是 WordPress 5.6.0 版本新增的钩子,它会在文章及其关联的 terms 和 meta 数据保存完成后触发。这使得它成为在文章保存后执行自定义逻辑的理想选择,例如创建或更新相关的 WooCommerce 产品。

代码示例:

/**
 * 在 "award_category" 文章保存后创建/更新 WooCommerce 产品。
 *
 * @param int     $post_id     文章 ID。
 * @param WP_Post $post        文章对象。
 * @param bool    $update      是否为更新现有文章。
 * @param WP_Post $post_before 更新前的文章对象,新建文章为 null。
 */
function save_award_category( $post_id, $post, $update, $post_before ) {

    if ( get_post_type( $post_id ) == 'award_category' ) {

        // 获取文章对象
        $post = get_post( $post_id );

        // 检查是否存在同名产品
        $product = get_page_by_path( $post->post_name, OBJECT, 'product' );

        // 如果不存在同名产品
        if ( !$product ) {

            // 创建产品文章
            $product_id = wp_insert_post(array(
                'post_title'  => get_the_title($post->ID),
                'post_type'   => 'product',
                'post_status' => 'publish'
            ));

        } else {
            $product_id = $product->ID;
        }

        // 获取文章的 ACF 字段组数组
        $award_category_fields = get_fields( $post->ID );

        // 确保 $award_category_fields 是一个数组且包含 'description' 键
        if (is_array($award_category_fields) && isset($award_category_fields['description'])) {
            // 更新产品描述,使用文章描述
            update_field( 'description', $award_category_fields['description'], $product_id );
        }
    }

}

add_action( 'wp_after_insert_post', 'save_award_category', 10, 4 );

代码解释:

  1. add_action( 'wp_after_insert_post', 'save_award_category', 10, 4 );: 将 save_award_category 函数挂载到 wp_after_insert_post 钩子上。10 是优先级,4 表示该函数接收 4 个参数。
  2. save_award_category( $post_id, $post, $update, $post_before ): 定义函数,接收文章 ID、文章对象、是否为更新以及更新前的文章对象作为参数。
  3. if ( get_post_type( $post_id ) == 'award_category' ): 确保该函数只在 "award_category" 类型的文章保存时执行。
  4. $product = get_page_by_path( $post->post_name, OBJECT, 'product' ): 检查是否存在同名产品。
  5. wp_insert_post(...): 如果不存在同名产品,则创建新的 WooCommerce 产品。
  6. update_field( 'description', $post['description'], $product_id ): 使用文章的描述更新产品的描述字段。此处的 $post = get_field( 'all_fields', $post->ID ); 改为直接获取award_category_fields的值,避免覆盖之前定义的 $post 变量。添加了对$award_category_fields数组的判断,确保存在且包含description键。

注意事项:

  • wp_after_insert_post 钩子在 WordPress 5.6.0 及更高版本中可用。
  • 确保你的主题或插件没有禁用此钩子。
  • get_fields() 函数是 Advanced Custom Fields (ACF) 插件提供的函数,用于获取 ACF 字段的值。如果未使用 ACF,请使用相应的函数获取文章的自定义字段。
解决方案二:使用 updated_post_meta 钩子

updated_post_meta 钩子会在特定类型的文章的元数据更新后触发。 虽然这个钩子是针对元数据的,但可以用来确保在所有 ACF 字段(包括描述)更新后执行操作。

代码示例:

/**
 * 在 "award_category" 文章元数据更新后创建/更新 WooCommerce 产品。
 *
 * @param int    $meta_id     已更新元数据的 ID。
 * @param int    $post_id     文章 ID。
 * @param string $meta_key    元数据键。
 * @param mixed  $meta_value  元数据值。
 */
function save_award_category( $meta_id, $post_id, $meta_key = '', $meta_value = '' ) {

    if ( get_post_type( $post_id ) == 'award_category' ) {

        // 获取文章对象
        $post = get_post( $post_id );

        // 检查是否存在同名产品
        $product = get_page_by_path( $post->post_name, OBJECT, 'product' );

        // 如果不存在同名产品
        if ( !$product ) {

            // 创建产品文章
            $product_id = wp_insert_post(array(
                'post_title'  => get_the_title($post->ID),
                'post_type'   => 'product',
                'post_status' => 'publish'
            ));

        } else {
            $product_id = $product->ID;
        }

        // 获取文章的 ACF 字段组数组
        $award_category_fields = get_fields( $post->ID );

        // 确保 $award_category_fields 是一个数组且包含 'description' 键
        if (is_array($award_category_fields) && isset($award_category_fields['description'])) {
            // 更新产品描述,使用文章描述
            update_field( 'description', $award_category_fields['description'], $product_id );
        }
    }

}

add_action( 'updated_post_meta', 'save_award_category', 10, 4 );

代码解释:

  1. add_action( 'updated_post_meta', 'save_award_category', 10, 4 );: 将 save_award_category 函数挂载到 updated_post_meta 钩子上。10 是优先级,4 表示该函数接收 4 个参数。
  2. save_award_category( $meta_id, $post_id, $meta_key = '', $meta_value = '' ): 定义函数,接收元数据 ID、文章 ID、元数据键和元数据值作为参数。
  3. if ( get_post_type( $post_id ) == 'award_category' ): 确保该函数只在 "award_category" 类型的文章元数据更新时执行。
  4. 其余代码与 wp_after_insert_post 示例相同,负责检查同名产品是否存在、创建新产品以及更新产品描述。

注意事项:

  • updated_post_meta 钩子会在每次元数据更新时触发,因此可能会多次执行 save_award_category 函数。 为了避免不必要的重复操作,可以添加额外的逻辑来检查是否已经创建或更新了产品。例如,可以添加一个自定义字段来记录是否已经处理过该文章。
  • 同样需要确保 ACF 插件已安装并启用,或者使用相应的函数获取文章的自定义字段。
总结

本文介绍了两种在 WordPress 中,当特定类型的文章新增或更新后执行自定义函数的解决方案:使用 wp_after_insert_post 钩子和 updated_post_meta 钩子。 wp_after_insert_post 钩子是首选方案,因为它只会在文章及其关联数据完全保存后触发。 updated_post_meta 钩子则需要在函数中添加额外的逻辑以避免重复操作。 选择哪种方案取决于你的具体需求和 WordPress 版本。 无论选择哪种方案,都应该仔细测试代码,确保其能够正确地创建或更新 WooCommerce 产品,并且不会对网站的性能产生负面影响。

以上就是WordPress 教程:在文章新增或保存后执行函数的详细内容,更多请关注知识资源分享宝库其它相关文章!

标签:  函数 新增 保存 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。