use*_*469 2 php wordpress plugins custom-post-type
我为 wordpress 网站开发了一个主题和一些插件。这些插件都用于自定义帖子类型。我尝试搜索了几个小时的解决方案,但我找到的所有解决方案似乎都不适合我。许多解决方案表明需要调用一个数组才能使 foreach 参数工作,但我相当确定我的插件已经准确地使用了一个数组。我在我的本地开发人员上创建了一个完全正常工作的站点,没有任何错误,但是当我在我的网站的实时子目录(用于演示)上安装任何插件时,我收到以下三个错误:
“在第 1468 行的 /home3/adamshap/public_html/usdbls/wp-includes/post.php 中为 foreach() 提供的参数无效”
"无法修改标头信息 - 标头已由 /home3/adamshap/public_html/usdbls/wp-includes/option.php 中的(输出开始于 /home3/adamshap/public_html/usdbls/wp-includes/post.php:1468)发送在线787 “
"无法修改标头信息 - 标头已由 /home3/adamshap/public_html/usdbls/wp-includes/option.php 中的(输出开始于 /home3/adamshap/public_html/usdbls/wp-includes/post.php:1468)发送在线788 “
附加信息:我只在上传我的自定义 post_type 插件时收到错误(不是我开发的主题,只是插件)。错误出现在我所有的插件上,而不是任何一个。我知道错误在于我制作的插件,所以我需要找出如何修复我的插件,而不是 wordpress 核心文件。
第一个错误指向 wp-includes/post.php 的第 1468 行。对应部分代码如下:
if ( $args->register_meta_box_cb )
add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );
$args->labels = get_post_type_labels( $args );
$args->label = $args->labels->name;
$wp_post_types[ $post_type ] = $args;
add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
foreach ( $args->taxonomies as $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy, $post_type );
}
Run Code Online (Sandbox Code Playgroud)
第二个和第三个错误指向 wp-includes/option.php 文件中的这部分代码:
// The cookie is not set in the current browser or the saved value is newer.
$secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) );
setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
$_COOKIE['wp-settings-' . $user_id] = $settings;
Run Code Online (Sandbox Code Playgroud)
看到这两个文件都是 wordpress 核心文件,我相信错误实际上是在我的插件代码中,而不是上面的片段。下面的保管箱链接链接到我已完成的插件之一的 txt 文件(它超过了 stackoverflow 的字符限制)。
https://www.dropbox.com/s/137djd6x13wp2e5/about-intro.txt?dl=0
在此先感谢您的帮助!
找到解决方案,部分感谢用户 Marc B 的上述评论。
我们发现在三个错误中,真正的错误只是第一个错误,第一个错误导致了另外两个错误。
我的第一个解决方案是将 wp-includes/post.php 文件(第 1468 - 1470 行)从:
foreach ( $args->taxonomies as $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy, $post_type );
}
Run Code Online (Sandbox Code Playgroud)
对此:
if (is_array($taxonomy) || is_object($taxonomy)) {
foreach ($args ->taxonomies as $taxonomy) {
register_taxonomy_for_object_type($taxonomy, $post_type);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这解决了我的问题,但这是一个糟糕的解决方案,因为它需要编辑 wp 核心文件。在 Marc B 建议使用 var_dump 并找到我的插件中使用分类数组(错误地)的部分之后,我找到了真正的解决方案:
我的插件使用以下原始代码注册了自定义 post_type:
$args = array(
'label' => __( 'Statement', 'text_domain' ),
'description' => __( 'About Statement', 'text_domain' ),
'labels' => $labels,
'supports' => array('',''),
'taxonomies' => false,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'query_var' => 'about',
'capability_type' => 'page',
);
register_post_type( 'about_type', $args );
}
add_action( 'init', 'about_post_type', 0);
Run Code Online (Sandbox Code Playgroud)
如您所见,$args 数组将“分类法”定义为“假”。这就是导致错误的原因。解决方案是简单地将值 'false' 更改为数组。固定函数如下:
$args = array(
'label' => __( 'Statement', 'text_domain' ),
'description' => __( 'About Statement', 'text_domain' ),
'labels' => $labels,
'supports' => array('',''),
'taxonomies' => array(),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'query_var' => 'about',
'capability_type' => 'page',
);
Run Code Online (Sandbox Code Playgroud)
在自己寻找答案时,我发现很多人似乎都有这个问题。如果您正在寻找类似的解决方案,我建议您查看插件的 register_post_type 函数以了解数组如何处理分类法。