使用wp_INSERT_POST创建带有ACF图像域的自定义帖子

时间:2017-09-06 作者:Renan Correard Motta

几天来我一直在寻找答案。我有一个项目,我需要创建一个前端表单,该表单将请求一组信息,并使用它创建一个后期草稿,供客户在管理面板上批准或不批准。到目前为止,我能够创建一个短代码,在前端显示HTML表单,获取所需信息,并根据我的自定义帖子类型创建草稿。最大的问题是上传图像,我似乎无法让它工作,甚至无法在任何地方找到解决方案。

这是我当前的代码:

add_action ( \'init\', \'create_cpt_teste\');

function create_cpt_teste() {
    register_post_type (\'cpt-teste\',
        array(
            \'labels\' => array(
                \'name\' => \'CPT Teste\',
                \'singular_name\' => \'CPT Teste\',
                \'add_new\' => \'Adicionar Novo\',
                \'add_new_item\' => \'Adicionar Novo CPT Teste\',
                \'edit\' => \'Editar\',
                \'edit_item\' => \'Editar CPT Teste\',
                \'new_item\' => \'Novo CPT Teste\',
                \'view\' => \'Visualizar\',
                \'view_item\' => \'Visualizar CPT Teste\',
                \'search_items\' => \'Pesquisar CPT Teste\',
                \'not_found\' => \'Nenhum CPT Teste Encontrado\',
                \'not_found_in_trash\' => \'Nenhum CPT Teste Encontrado na Lixeira\',
                \'parent\' => \'CPT Teste Superior\'
            ),

            \'public\' => true,
            \'publicly_queryable\' => true,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'query_var\' => true,
            \'rewrite\' => array( \'slug\' => \'cpt-teste\' ),
            \'capability_type\' => \'post\',
            \'has_archive\' => true,
            \'hierarchical\' => false,
            \'menu_position\' => 25,
            \'supports\' => array( \'title\', \'editor\', \'thumbnail\' ),
            \'menu_icon\' => \'dashicons-store\'
        )
    );
}

function zmg_form() {
    ?>
    <form id="teste-cpt" name="teste-cpt" method="post" action="">
        <p>
            <label for="nome">Nome</label><br />
            <input type="text" id="nome" value="" size="20" name="nome" />
        </p>
        <p>
            <label for="teste1">Teste 1</label><br />
            <input type="text" id="teste1" value="" size="20" name="teste1" />
        </p>
        <p>
            <label for="teste2">Teste 2</label><br />
            <input type="text" id="teste2" value="" size="20" name="teste2" />
        </p>
        <p>
            <label for="teste3">Teste 3</label><br />
            <input type="text" id="teste3" value="" size="20" name="teste3" />
        </p>
        <p>
            <label for="teste-dropdown">Teste Dropdown</label><br />
            <select name="teste-dropdown">
                <option value="Drop1">Drop1</option>
                <option value="Drop2">Drop2</option>
                <option value="Drop3">Drop3</option>
            </select>
        </p>
        <p>
            <label for="foto01">Foto 01</label><br />
            <input type="file" id="foto01" name="foto01" />
        </p>
        <p>
            <label for="foto02">Foto 02</label><br />
            <input type="file" id="foto02" name="foto02" />
        </p>
        <p>
            <input type="submit" value="Enviar" id="submit" name="submit" />
        </p>
        <input type="hidden" name="post-type" id="post-type" value="cpt-teste" />
        <input type="hidden" name="action" value="cpt-teste" />
        <?php wp_nonce_field ( \'name_of_my_action\',\'name_of_nonce_field\') ?>
    </form>

    <?php
    if($_POST) {
        zmg_save_post_data();
    }

}

add_shortcode(\'teste-form\',\'zmg_form\');

function zmg_save_post_data() {
    if ( empty($_POST) || !wp_verify_nonce($_POST[\'name_of_nonce_field\'],\'name_of_my_action\') )
    {
        print \'Desculpe, o nonce não foi verificado.\';
        exit;
    } else {
        //Validações
        if (isset ($_POST[\'nome\'])) {
            $nome = $_POST[\'nome\'];
        } else {
            echo \'Por favor, preencha o nome.\';
            exit;
        }
        if (isset ($_POST[\'teste1\'])) {
            $teste1 = $_POST[\'teste1\'];
        } else {
            echo \'Por favor, preencha o teste1.\';
            exit;
        }
        if (isset ($_POST[\'teste2\'])) {
            $teste2 = $_POST[\'teste2\'];
        } else {
            echo \'Por favor, preencha o teste2.\';
            exit;
        }
        if (isset ($_POST[\'teste3\'])) {
            $teste3 = $_POST[\'teste3\'];
        } else {
            echo \'Por favor, preencha o teste3.\';
            exit;
        }
        if (isset ($_POST[\'teste-dropdown\'])) {
            $testedropdown = $_POST[\'teste-dropdown\'];
        } else {
            echo \'Por favor, preencha o teste-dropdown.\';
            exit;
        }
        if (isset ($_POST[\'foto01\'])) {
            $foto01 = $_POST[\'foto01\'];
        } else {
            echo \'Por favor, selecione a foto 01.\';
            exit;
        }
        if (isset ($_POST[\'foto02\'])) {
            $foto02 = $_POST[\'foto02\'];
        } else {
            echo \'Por favor, selecione a foto 02.\';
            exit;
        }


        //Colocando conteúdos no array
        $post_info = array(
            \'post_title\' => wp_strip_all_tags( $nome ),
            \'post_content\' => \'\',
            \'post_status\' => \'draft\',
            \'post_type\' => \'cpt-teste\',
        );

        $postID = wp_insert_post( $post_info, true );

        update_field(\'field_59b0072b00b03\', $teste1, $postID);
        update_field(\'field_59b0073e00b04\', $teste2, $postID);
        update_field(\'field_59b0074d00b05\', $teste3, $postID);
        update_field(\'field_59b0075b00b06\', $testedropdown, $postID);
        update_field(\'field_59b023839f5fc\', $foto01, $postID);
        update_field(\'field_59b023ca9f5fe\', $foto02, $postID);


        $location = home_url();

        echo "<meta http-equiv=\'refresh\' content=\'0;url=$location\' />";
        exit;
    }
}
文本字段工作正常,我的问题是图像。

1 个回复
最合适的回答,由SO网友:finitesource 整理而成

ACF有在前端创建表单的文档。我会坚持他们的建议。https://www.advancedcustomfields.com/resources/create-a-front-end-form/

我可以通过将表单的字段组分配给自定义帖子类型并为表单使用唯一的模板页面来创建它。

<?php
/**
  * Template Name: Form Page Template
  */
 ?> 

<?php acf_form_head(); ?>
<?php get_header(); ?>

        <?php while ( have_posts() ) : the_post(); ?>

            <?php acf_form(array(
                \'post_id\'       => \'new_post\',
                \'new_post\'      => array(
                    \'post_type\'     => \'cpt-teste\',
                    \'post_status\'   => \'draft\'
                ),
                \'submit_value\'  => \'Submit\'
            )); ?>

        <?php endwhile; ?>

<?php get_footer(); ?>

结束

相关推荐

Featured images get shrunken

上传特色图片时,我遇到的问题是,所有图片都会自动缩小。我想保留原始图像。检查this example. 所有员工的图片都会自动缩小,但在特色图片中,我上传了员工的完整图片。我认为图像太大,wordpress会自动缩小图像,因此,我在媒体库中缩放并裁剪了图像。一些图像已修复,但一些图像仍然存在问题。有没有一种方法可以让原始上传的图像按原样显示?