几天来我一直在寻找答案。我有一个项目,我需要创建一个前端表单,该表单将请求一组信息,并使用它创建一个后期草稿,供客户在管理面板上批准或不批准。到目前为止,我能够创建一个短代码,在前端显示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;
}
}
文本字段工作正常,我的问题是图像。