这是我的代码的基础。我的想法是,当我点击将表单帖子提交到同一页面并在if语句中运行我的代码时。问题是,当我点击提交时,我收到Wordpress错误,说无法加载tb-export。我做了很多谷歌搜索,找不到解决方案。
编辑:这是全班。 https://pastebin.com/i03m9a3x
再次编辑:我把代码带到了课外并简化了它,我仍然得到错误。以下是简化代码,提交表单后我收到错误无法加载export_display。
add_action('admin_menu', 'tb_admin_menu');
function tb_admin_menu() {
add_menu_page(
'Table Builder',
'Table Builder',
'read',
'i15-table-builder',
'',
'dashicons-screenoptions',
21
);
add_submenu_page('i15-table-builder', 'Export', 'Export', 'manage_options', 'export_display', 'export_display');
}
function export_display(){
if ($_POST['submit']) {
$post_args = array(
'post_type' => $_POST['post_type'],
'post__in' => $_POST['ids']
);
$posts = get_posts($post_args);
$count = 0;
foreach ( $rtp_posts as $post ) {
$write_array[$count] = array(
'post_id' => $post->ID,
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $post->post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => $post->post_status,
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
$write_array[$count]['taxonomies'][$taxonomy] = $post_terms;
}
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
$countmetas = 0;
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes($meta_info->meta_value);
$write_array[$count]['metas'][$countmetas]['meta_key'] = $meta_key;
$write_array[$count]['metas'][$countmetas]['meta_value'] = $meta_value;
$countmetas++;
}
}
$count++;
}
$write_array_encode = json_encode($write_array);
header('Content-disposition: attachment; filename=Table_Builder_Exported_Data.json');
header('Content-type: application/json');
echo $write_array_encode;
exit();
} else {
$template_args = array(
'post_type' => 'tb_template',
'post_status' => 'any',
'numberposts' => '-1'
);
$template_posts = get_posts($template_args);
foreach ( $template_posts as $template_post ) {
$select_templates .= '<option value="'.$template_post->ID.'">'.$template_post->post_title.'</option>';
}
echo '<form method="post" action="" enctype="multipart/form-data">';
echo '<input type="hidden" name="post_type" value="tb_template" />';
echo '<select multiple="multiple" name="ids[]" size="10">'.$select_templates.'</select>';
submit_button('Export Table Builder Templates');
echo '</form>';
echo "<hr />";
$table_args = array(
'post_type' => 'tb_table',
'post_status' => 'any',
'numberposts' => '-1'
);
$table_posts = get_posts($table_args);
foreach ( $table_posts as $table_post ) {
$select_tables .= '<option value="'.$table_post->ID.'">'.$table_post->post_title.'</option>';
}
echo '<form method="post" action="" enctype="multipart/form-data">';
echo '<input type="hidden" name="post_type" value="tb_table" />';
echo '<select multiple="multiple" name="ids[]" size="10">'.$select_tables.'</select>';
submit_button('Export Table Builder Tables');
echo '</form>';
}
}
答案 0 :(得分:0)
问题出在我的表格中。以前隐藏输入的名称是post_type。我想这有些东西搞砸了,因为在我摆脱了下划线之后,它起作用了。
echo '<input type="hidden" name="posttype" value="tb_template" />';