我正在try 制作一个在提交表单后更新帖子的函数,因为我上传了一个带有表单的文件,该表单将图像转换为URL,然后我想将其设置为帖子中的特色图像,但必须手动更新帖子才能加载特色图像.

更新我想出了如何获取帖子ID,但仍然存在不更新帖子的问题.我知道我需要在wp_UPDATE_POST中使用某种类型的POST数组,但我不想更改任何内容,我只想加载特色图像.如果有更简单的方法,请告诉我.

add_action( 'gform_after_submission_2', 'custom_action_after_apc', 10, 2 );
function custom_action_after_apc( $entry, $form ) {

    //if the Advanced Post Creation add-on is used, more than one post may be created for a form submission
    //the post ids are stored as an array in the entry meta
    $created_posts = gform_get_meta( $entry['id'], 'gravityformsadvancedpostcreation_post_id' );
    foreach ( $created_posts as $post )
    {
        $post_id = $post['post_id'];
        wp_update_post($post_id);
    }
}

推荐答案

我找到了一个解决我的问题的方法,就是在帖子上有一个特色图片,所以如果其他人有同样的问题,这些功能对我来说是有效的.

您必须修改一些字段名称,但这应该不是什么大事.

/*
 * Plugins
 * =======
 * 
 * Gravity Forms by Gravity Forms
 * <https://wordpress.com/plugins/gravityforms>
 *
 * Advanced Post Creation Add-On By Gravity Forms
 * <https://www.gravityforms.com/add-ons/advanced-post-creation/>
 *
 * Advanced Custom Fields (ACF) by WP Engine
 * <https://wordpress.org/plugins/advanced-custom-fields/>        
 */

add_action('gform_after_submission_2', 'custom_action_after_apc', 10, 2);
// (The _2 in the gform_after_submission is the form id.)

function custom_action_after_apc($entry, $form)
{

    // If the Advanced Post Creation add-on is used, more than one post
    // may be created for a form submission.
    // The Post-IDs are stored as an array in the entry meta.
    $created_posts = gform_get_meta(
        $entry['id'],
        'gravityformsadvancedpostcreation_post_id'
    );

    foreach ($created_posts as $post) {
        $post_id = $post['post_id'];

        // Get the title and featured image URL from the form fields.
        $title = get_field('title', $post_id);
        // $title = 'nu ska vi testa';
        $featured_image_url = get_field('featured_image', $post_id);
        // $featured_image_url
        
        // Update the post-title.
        $post_data = array(
            'ID' => $post_id,
            'post_title' => $title,
        );

        wp_update_post($post_data);
        
        // No featured image to update.
        if (empty($featured_image_url)) {
            continue;
        }
        
        // Download the image and set it as the featured image.
        $image_id = custom_upload_featured_image(
            $featured_image_url, 
            $post_id
        );

        // Set the featured image for the post.
        set_post_thumbnail($post_id, $image_id);
    }
}

// Function to upload and set a featured image from a URL.
function custom_upload_featured_image($image_url, $post_id)
{
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    require_once(ABSPATH . 'wp-admin/includes/image.php');

    // Upload the image from the URL.
    $attachment_id = media_sideload_image($image_url, $post_id, null, 'id');

    if (! is_wp_error($attachment_id)) {
        return $attachment_id;
    }

    return 0;
}

Php相关问答推荐

通过DO LAMP进行身份验证并从邮箱发送邮箱的最简单方式是什么

用于计算付款费用的WooCommerce管理编辑产品中的自定义复选框

WooCommerce常规价格增加额外的附加平价

根据WooCommerce Cart小计阈值自动应用优惠券

上传WordPress模板中的翻译文件(.mo和.po)

在Laravel中创建辅助函数时的约定

Select 本地提货时隐藏WooCommerce显示的发货成本

表单提交返回空白页

Symfony:从控制器内部调用自定义命令

单个产品页面 WooCommerce 订阅价格字符串的更改

Docker-用于部署的 PHP 应用程序映像无法在本地运行

如何处理 Null 上的 array_shift() ?

用于更新 WooCommerce 管理产品列表中库存数量的自定义输入字段

根据支付网关选项更改 WooCommerce Checkout 上的立即付款按钮文本

在 WooCommerce 管理变量产品中启用其他变体图像

循环中未序列化数组显示的问题

WooCommerce单个产品页面上启用额外选项卡的产品元框字段

如何在 laravel 中的同一个 slug 上显示不同的内容?

PHP 中是否有 is_closure() 函数?

为什么 PDO 允许使用带命名占位符的索引数组,但仅在禁用仿真时才允许?