WordPress.org

Make WordPress Core

Opened 19 months ago

Closed 19 months ago

Last modified 19 months ago

#21943 closed enhancement (wontfix)

register_post_type() expect array but should object as well

Reported by: thomask Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: Posts, Post Types Keywords: close
Focuses: Cc:

Description

$wp_post_types global uses objects for all post types and objects for some of their properties (labels and cap, on the other hand taxonomies property is array)

problem is, that when you get this object and want to reuse it using register_post_type (e.g. you want easy copy the post type to another post type), it throws error, because it expect all properties are array (even if you cast the main object as array):

      global $wp_post_types;
      $new_post_type = (array) $wp_post_types['post'];
      register_post_type( 'new_post_type', $new_post_type );

throws

Fatal error: Cannot use object of type stdClass as array in 
/var/www/projects/zdrojak/htdocs/public/wp-includes/post.php
on line 1259

you have to first do something like

      foreach ($new_post_type as &$arg) {
              if (is_object($arg)) $arg = array($arg);
      }

I am not sure, if it is a bug of register_post_type(), so that it should accept objects as well, or if it is a bug of the $wp_post_types global, that it uses objects in place of arrays

Change History (9)

comment:1 nacin19 months ago

  • Type changed from defect (bug) to enhancement

I don't think this is a bug.

What $wp_post_types stores is not necessarily the same that is received by register_post_type(). I don't think they are necessarily interchangeable, either. IF they are interchangeable now, I'm not sure they should need to be in the future.

For example, 'capabilities' (input array) versus 'cap' (output object). capability_type can take an array but reduces it to its first value once it is done with it.

comment:2 scribu19 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Yeah; due to the pass-by-reference thing, register_post_type() would have to create a new instance anyway, to avoid weird side-effects.

Besides, I don't think cloning post types should be something that the API encourages.

comment:3 follow-up: thomask19 months ago

  • Resolution wontfix deleted
  • Status changed from closed to reopened

register_post_type does not passing anything by reference.

I will explain, why i think it is necessary, but maybe you will tell me there is an easier way:

I wanted to change some arguments of buid-in post_types.
There is no update_post_type etc. function, the only way i know is to take the existing post type, change what you want and then reregister it. Only this way it is registered properly, i cannot change only the $wp_post_type.

here is my code:

      global $wp_post_types;
      $wp_post_types['post']->has_archive = true;
      $wp_post_types['post']->rewrite     = array (
                                              'enabled' => '1',
                                              'feeds' => true,
                                              'pages' => true,
                                              'with_front' => false,
                                              'slug' => 'clanky',
                                              'ep_mask' => 1,
                                            );
      $post_type = $wp_post_types['post'];
      foreach ($post_type as &$arg) {
              if (is_object($arg)) $arg = array($arg);
      }
      register_post_type( 'post', (array) $post_type );

I think, that someone may need it in the future and this obstruction is realy pointless. If there would be e.g. easy (array) casting for the labels and cap, it would not harm anyone.

comment:4 in reply to: ↑ 3 scribu19 months ago

  • Keywords close added
  • Milestone set to Awaiting Review

Replying to thomask:

register_post_type does not passing anything by reference.

No, it doesn't, but objects are always passed by reference in PHP5

http://php.net/manual/en/language.oop5.references.php

I wanted to change some arguments of buid-in post_types.

Then we should add a hook for allowing that.

When opening a ticket, state the problem, not the solution.

Version 0, edited 19 months ago by scribu (next)

comment:5 thomask19 months ago

  • Resolution set to wontfix
  • Status changed from reopened to closed

yes, but imo after typecasting the reference's gone. But leave it as it - you are definitely far better programmer then me ;-) and for me i got a working solution now unless there will be some better way. thx and sorry

comment:7 thomask19 months ago

thank you, next time i will try to be more accurate with my descriptions

comment:8 scribu19 months ago

No problem. It's been bugging me for a while too, but I didn't think to open a ticket until now, so thanks for bringing it up.

comment:9 SergeyBiryukov19 months ago

  • Milestone Awaiting Review deleted
  • Version changed from trunk to 3.4
Note: See TracTickets for help on using tickets.