1

I’m attempting to upgrade a WP install from 4.0.6 to 4.4.2. Most of the update went swimmingly, but I’m faced with a pretty cool error message I can’t seem to figure out:

Warning: Creating default object from empty value in /nas/content/staging/thebaffler/wp-content/themes/baffler/inc/custom-posts.php on line 216

The error seems to be thrown by a theme file that renames the default labels WP uses for the ‘post’ type of content. What’s odd is that all the ‘post’ labels still continue to render with the settings specified in the theme file ‘custom-posts.’

Here’s a snippet of the code in question. 214 is the first line excerpted here. If I comment line 216, the error message refers to 215 instead:

$labels = &$wp_post_types['post']->labels;
$labels->name = 'Blog';
$labels->singular_name = 'Blog';
$labels->add_new = 'Add Blog entry';
$labels->add_new_item = 'Add Blog entry';
$labels->edit_item = 'Edit Blog entry';
$labels->new_item = 'Blog entry';
$labels->view_item = 'View Blog entry';
$labels->search_items = 'Search Blog Entries';
$labels->not_found = 'No Blog entries found';
$labels->not_found_in_trash = 'No Blog entries found in Trash';
$labels->all_items = 'All Blog Entries';
$labels->menu_name = 'Blog';
$labels->name_admin_bar = 'Blog';

Looking in the codex, I don't see any recent changes to wp_post_types. https://codex.wordpress.org/Function_Reference/get_post_type_object

What should I do next to troubleshoot this error?

Thanks James

1
  • such question that require access to your local code are off-topic. it doesn't make any sense for that value to be null, so there is probably something else that happens there. Mar 8, 2016 at 18:19

1 Answer 1

0

What that error is saying is that $labels is an empty object, so it can't assign to it.

$foo = NULL;
$foo->labels = false; // This causes the error 

See this Stack Overflow post for technical details: https://stackoverflow.com/questions/8900701/creating-default-object-from-empty-value-in-php

4
  • Thanks Mike! I might have been vague with my question. I get that $labels is empty, but I can't figure out why. $wp_post_types hasn't changed from WP 4.0 to 4.4—why can't I set $labels = &$wp_post_types['post']->labels;? What's more—the labels DO still get relabeled.
    – jamesfacts
    Mar 8, 2016 at 19:26
  • $labels = &$wp_post_types['post']->labels; This line is roughly equivalent to: $labels = NULL; You can still create attributes (or whatever the correct PHP terminology for -> is).
    – bikemule
    Mar 8, 2016 at 19:51
  • Ugh. I was editing that last comment too much, apparently. But it makes sense the code works. There's nothing in ->labels so it creates an empty object for you. Then you can add attributes. And because $labels is a reference, it creates that object in $wp_post_types['post']. Does that make sense?
    – bikemule
    Mar 8, 2016 at 20:03
  • 👍Yes, I got you. I fixed the issue adding a reference to the global var $wp_post_types — I wonder what changed from 4.0 to 4.4 that necessitates that!
    – jamesfacts
    Mar 8, 2016 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.