这是我的第一个问题。
我一直试图从Wordpress前端发布帖子,所以我创建了两个自定义帖子类型' order'和'产品'。当用户创建订单号(第一个自定义帖子类型)时,他们可以创建与之相关的多个产品(第二个自定义帖子类型)。
我创建了一个短代码,我可以将这个表单放在页面中。
一切正常,我可以发布帖子和每个元帖子。问题是,wordpress继续显示此消息:
警告:strip_tags()
要求参数1为字符串,数组在 / THE-FILE-PATH AND LINE 中给出。
我已经在字符串上转换了所有内容,但警告仍在继续,代码也正常运行。
以下是代码:
<?php
//================================
/*--- CRIAR NOVO NO FRONT END --*/
//================================
add_shortcode('create_order_number', 'create_order_number_shortcode');
function create_order_number_shortcode() {
ob_start();
$current_user = wp_get_current_user();
if($current_user->ID!=0){ ?>
<form method="post" id="create_order_number" action="" enctype="multipart/form-data">
<table style="width:100%">
<tr>
<td valign="top" width="50%">
<label for="order_number">Número de Ordem:</label>
<input type="text" name="order_number" value="" /><br />
</td>
<td valign="top" width="50%">
<label for="order_status">Status:</label>
<select name="order_status">
<option value="faturado">Faturado</options>
<option value="pedido">Pedido</option>
<option value="concluido">Concluído</option>
</select>
</td>
</tr>
</table>
<table class="products_unit" style="width:100%">
<tr>
<td valign="top" width="25%">
<label for="product_unit">Produto</label>
<input type="text" name="product_unit[]" value="" /><br />
</td>
<td valign="top" width="25%">
<label for="product_qtd">Quantidade</label>
<input type="text" name="product_qtd[]" value="" /><br />
</td>
<td valign="top" width="25%">
<label for="product_status">Status:</label>
<select name="product_status[]">
<option value="faturado">Faturado</options>
<option value="pedido">Pedido</option>
<option value="concluido">Concluído</option>
</select>
</td>
<td valign="middle" width="25%"></td>
</tr>
</table>
<button type="button" class="add_product_unit">Adicionar Produto</button>
<button type="submit">Criar</button>
<input type="hidden" name="action" value="create_order_number" />
</form>
<script type="text/javascript">
var i=0;
jQuery('.add_product_unit').click(function(){
jQuery('.products_unit').append('<tr> <td valign="top" width="25%"> <label for="product_unit">Produto</label> <input type="text" name="product_unit[]" value="" /><br /> </td> <td valign="top" width="25%"> <label for="product_qtd">Quantidade</label> <input type="text" name="product_qtd[]" value="" /><br /> </td> <td valign="top" width="25%"> <label for="product_status">Status:</label> <select name="product_status[]"> <option value="faturado">Faturado</options> <option value="pedido">Pedido</option> <option value="concluido">Concluído</option> </select> </td> <td valign="middle" width="25%"> <a href="#" class="close-'+i+'">Deletar</a> </td> </tr>');
jQuery('.close-'+i).click(function(e){
e.preventDefault();
jQuery(this).closest('tr').remove();
});
i++;
});
</script>
<?php }//endif
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action('init','save_order_number');
function save_order_number(){
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "create_order_number") {
/* ==== NUMBER ORDER ==== */
$new_order = array(
'post_title' => $_POST['order_number'],
'post_status' => 'publish',
'post_type' => 'order'
);
$orderId = wp_insert_post($new_order);
update_post_meta($orderId, 'order_number', $_POST['order_number']);
update_post_meta($orderId, 'order_status', $_POST['order_status']);
/* ==== PRODUCT UNIT ==== */
$product_unit = $_POST['product_unit'];
$product_qtd = $_POST['product_qtd'];
$product_status = $_POST['product_status'];
foreach ($product_unit as $key => $value) {
//echo "{$key} => {$value} ";
$this_unit = strip_tags($product_unit[$key]);
$this_qtd = strip_tags($product_qtd[$key]);
$this_status = strip_tags($product_status[$key]);
$new_product = array(
'post_title' => $this_unit,
'post_status' => 'publish',
'post_type' => 'product'
);
$productId = wp_insert_post($new_product);
update_post_meta($productId, 'product_unit', $this_unit);
update_post_meta($productId, 'product_qtd', $this_qtd);
update_post_meta($productId, 'product_status', $this_status);
update_post_meta($productId, 'product_order', $orderId);
}
/* ==== REDIRECT ==== */
$the_post_url = get_post_permalink($orderId);
//echo '<script>window.location.href = "'.$the_post_url.'"</script>';
}
}
错误在于:
'post_title' => $this_unit,
'post_status' => 'publish',
'post_type' => 'product'
如果我改变产品&#39;到&#39;命令&#39;或者&#39;发布&#39;警告未显示。
答案 0 :(得分:0)
过了一段时间我明白了一切。在我的后端wordpress&#39;产品&#39;上有一个元数据盒。自定义帖子类型处理相同的帖子元标记。
问题是,当你调用函数wp_insert_post()时,你输入你的wordpress hook add_action(&#39; save_post&#39;),其中保存了相同的输入名称。所以我只是添加了一个隐藏的输入来过滤&#39; save_post&#39; (if(get_post_type($ argument-&gt; ID)==&#39; order&#39;&amp;&amp; $ _POST [&#39;情境&#39;] ==&#39;编辑&#39;){ })。
这就是:
<?php
//========================
/*--- NOVO CAMPO TYPE --*/
//========================
//CRIA O TIPO DO POST NO MENU ADMIN
add_action( 'init', 'create_post_type_order' );
function create_post_type_order() {
$labels = array(
'name' => 'Nº de Ordem',
'singular_name' => 'Nº de Ordem',
'add_new' => 'Adicionar Ordem',
'add_new_item' => 'Criar Número de Ordem',
'edit_item' => 'Editar Nº de Ordem',
'new_item' => 'Novo Número de Ordem',
'all_items' => 'Todas Ordens',
'view_item' => 'Ver Nº de Ordem',
'search_items' => 'Buscar Nº de Ordem',
'not_found' => 'Nenhum Nº de Ordem Encontrado',
'not_found_in_trash' => 'Nenhum Nº de Ordem na Lixeira',
'parent_item_colon' => '',
'menu_name' => 'Nº de Ordem'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-clipboard',
'rewrite' => array(
'slug' => 'order',
'with_front' => false,
),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title')
);
register_post_type( 'order', $args );
flush_rewrite_rules();
}
//CRIA O METABOX LINK DENTRO DO CUSTOM POST
add_action( 'add_meta_boxes', 'order_add_meta_box' );
function order_add_meta_box() {
add_meta_box(
'order_number_metabox',
'Número de Ordem',
'order_inner_meta_box',
'order'
);
}
function order_inner_meta_box( $argument ) {
?>
<table style="width:100%">
<tr>
<td valign="top" width="50%">
<label for="order_number">Número de Ordem:</label>
<input type="text" name="order_number" value="<?php echo get_post_meta( $argument->ID, 'order_number', true ); ?>" /><br />
</td>
<td valign="top" width="50%">
<label for="order_status">Status:</label>
<?php $order_status = get_post_meta( $argument->ID, 'order_status', true ); ?>
<select name="order_status">
<option value="faturado" <?php if($order_status=='faturado'){echo ' selected';} ?>>Faturado</options>
<option value="pedido" <?php if($order_status=='pedido'){echo ' selected';} ?>>Pedido</option>
<option value="concluido" <?php if($order_status=='concluido'){echo ' selected';} ?>>Concluído</option>
</select>
</td>
</tr>
</table>
<input type="hidden" name="situation" value="editing">
<?php
}
//SALVA OS NOVOS METABOX NO POST
add_action( 'save_post', 'order_save_post', 10, 2 );
function order_save_post( $argument_id, $argument ) {
//Verifica se o custom post type é o correto, garantindo-nos que estamos a guardar valores da página.
if(get_post_type($argument->ID)=='order' && $_POST['situation'] == 'editing'){
update_post_meta( $argument_id, 'order_number', strip_tags( $_POST['order_number'] ) );
update_post_meta( $argument_id, 'order_status', strip_tags( $_POST['order_status'] ) );
return true;
} else {
return;
}
}
?>