WordPress的ACF:如何拥有一个可以编程的验证消息?

ryoqjall  于 2023-02-03  发布在  WordPress
关注(0)|答案(1)|浏览(143)

当我使用acf_form时,我可以得到一个验证消息。在下面的代码中:

<?php
acf_form_head();

    

    $args = array(
        'post_id' => 'new_post', // On va créer une nouvelle publication
        'post_title' => true,
        'new_post' => array(
            'post_type' => 'entreprise', // Enregistrer dans l'annuaire
            'post_status' => 'publish', // Enregistrer en publique
            'post_id' => 'CHR'
        ),
        'field_groups' => array( 205 ), // L'ID du post du groupe de champs
        'submit_value' => 'Create', // Intitulé du bouton
        'updated_message' => "Enterprise creation done",
        'html_updated_message'  => '<div id="message" class="acf-notice -success"><p>%s</p></div>',
         
    );

 acf_form( $args ); // Afficher le formulaire
?>

按下“创建”按钮后,显示“企业创建完成”
我希望验证消息包含来自ACF的值。例如,ACF EnterpriseName。我希望消息是“企业创建完成”。我不知道如何操作。谢谢您的帮助

czq61nw1

czq61nw11#

您可以使用html_updated_message参数传递自定义消息。该参数采用带有占位符“%s”的字符串,该占位符将被ACF字段的实际值替换。
您可以使用高级自定义字段(ACF)提供的get_field函数来检索ACF字段“EnterpriseName”的值,并将其包含在自定义消息中。
下面是一个更新的示例:

$args = array(
    'post_id' => 'new_post', 
    'post_title' => true,
    'new_post' => array(
        'post_type' => 'entreprise', 
        'post_status' => 'publish',
        'post_id' => 'CHR'
    ),
    'field_groups' => array( 205 ), 
    'submit_value' => 'Create', 
    'updated_message' => "Enterprise creation done",
    'html_updated_message'  => '<div id="message" class="acf-notice -success"><p>Enterprise %s creation done</p></div>',
);

acf_form( $args );

然后,在调用acf_form函数之后,您可以检索“EnterpriseName”字段的值并替换自定义消息中的占位符:

$enterprise_name = get_field('EnterpriseName', 'CHR');
$args['html_updated_message'] = sprintf($args['html_updated_message'], htmlspecialchars($enterprise_name));

相关问题