php - 自定义 Post_Type 插件导致为 wp-includes/post.php 中的 foreach() 提供的参数无效

标签 php wordpress plugins custom-post-type

我为一个 wordpress 网站开发了一个主题和一些插件。 这些插件全部用于自定义帖子类型。我尝试搜索几个小时来寻找解决方案,但我发现的所有解决方案似乎都不适合我。许多解决方案建议需要调用一个数组才能使 foreach 参数起作用,但我相当确定我的插件已经准确地使用了一个数组。我在本地开发人员上创建了一个完全可用的网站,没有任何错误,但是当我在我网站的实时子目录中安装任何插件时(用于演示),我收到以下三个错误:

  1. 为/home3/adamshap/public_html/usdbls/wp-includes/post.php 第 1468 行中的 foreach() 提供的参数无效

  2. "无法修改 header 信息 - header 已发送(输出开始于/home3/adamshap/public_html/usdbls/wp-includes/post.php:1468)在/home3/adamshap/public_html/usdbls/wp-includes/option.php 第 787 行

  3. "无法修改 header 信息 - header 已发送(输出开始于/home3/adamshap/public_html/usdbls/wp-includes/post.php:1468)在/home3/adamshap/public_html/usdbls/wp-includes/option.php 第 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 );
}

第二个和第三个错误指向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;

鉴于这两个文件都是 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 );
}

为此:

if (is_array($taxonomy) || is_object($taxonomy)) { 
    foreach ($args ->taxonomies as $taxonomy) {
        register_taxonomy_for_object_type($taxonomy, $post_type);
    } 
}

虽然这解决了我的问题,但这是一个糟糕的解决方案,因为它需要编辑 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);

如您所见,$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',
    );

在自己寻找答案的过程中,我发现很多人似乎都有这个问题。如果您正在寻找类似的解决方案,我建议您查看插件的 register_post_type 函数以查看数组如何处理分类法。

关于php - 自定义 Post_Type 插件导致为 wp-includes/post.php 中的 foreach() 提供的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33156258/

相关文章:

plugins - Eclipse RCP : Add required plugins automatically to feature

php - 查询表中数组中的每个值

javascript - 如果选中 radio ,如何包含 php 文件

css - 如何修复从水平切换到下拉菜单(WP Astra 主题)的主菜单(主标题)?

wordpress - 引导移动菜单按钮在手机上离开屏幕

css - Facebook Like 社交插件大小问题

php - 如何判断用户名是否已在数据库中使用

php - 使用ajax将JS变量传递到同一个php文件

wordpress - 多个 WordPress 安装 - wpengine 中的单个域

plugins - Sublime Text 3 插件 Anaconda 不起作用