Custom post types - supports while using apply_filters and add_filter()

I need some feedback on how to achieve this… in most cases, the default supports is as follow… while this is post type is done through a plugin that i created.

$args = [
     'labels' => $labels,
     'public' => true,
     'supports' => [ 'title', 'editor', 'thumbnail' ],
];

So this is what i’m trying to do. I added apply_filters();

'supports' => apply_filters( "something/goes/here/supports/portfolio', [ 'title', 'editor', 'thumbnail'] );

this is all done in the plugins i created. but when I tried to do the following in a any thing in the functions.php

add_filter('something/goes/here/supports/portfolio', function() {
    return [
    'title',
    'editor',
    'thumbnail',
    'custom-field
    ];
} );

This assumes that by calling add_filter() in the theme is way too late. but it works if i take the same code and insert in one of the plugins file. is there a way for me call the add_filter() earlier. This just tells me that the add_filter must be called before apply_filters is executed.

Yes, plugins are loaded before themes. You can confirm that here: Plugin API/Action Reference « WordPress Codex (@anon66243189 not sure if we have an equivalent of this page yet in our docs, but it is an important one).

You can get around this by having your plugin register your custom post type(s) during the init action:

add_action( 'init', function() {
    $args = [
        'labels' => $labels,
        'public' => true,
        'supports' => apply_filters( "something/goes/here/supports/portfolio", [ 'title', 'editor', 'thumbnail' ],
    ];
    // call register_post_type() here
} );

Then, the load order will work like this:

  1. Plugin loads and adds code to init action
  2. Theme loads and theme functions.php runs
  3. init action runs, and your plugin code executes here

Then your theme can call
add_filter( 'something/goes/here/supports/portfolio', ... )
in functions.php and it will work as expected.

I believed that is already set to init, here’s my code.

	/**
	 * $post post.
	 *
	 * @var $this Controller.
	 */
	protected $posts;

	/**
	 * Register Custom Post Types.
	 */
	public function register_post_types() {
		foreach ( $this->posts as $name => $value ) {
			if ( ! apply_filters( "backdrop/post/type/{$name}", false ) ) {
				register_post_type( $name, $value );
			}
		}
	}

	/**
	 * Create Posts by create_post_type().
	 *
	 * @param string $type a post type.
	 * @param string $singular_label a single label.
	 * @param string $plural_label a more than one.
	 */
	public function create_post_type( $type, $singular_label, $plural_label ) {

		$labels = [
			'name'					=> sprintf( esc_html__( '%s', 'backdrop-post-types' ),						$plural_label ),
			'singular_name'			=> sprintf( esc_html__( '%s', 'backdrop-post_types' ),						$singular_label ),
			'name_admin_bar'		=> sprintf( esc_html__( '%s', 'backdrop-post_types' ),						$singular_label ),
			'add_new'				=> sprintf( esc_html__( 'New %s', 'backdrop-post-types' ),					$singular_label ),
			'add_new_item'			=> sprintf( esc_html__( 'Add New %s', 'backdrop-post-types' ),				$singular_label ),
			'edit_item'				=> sprintf( esc_html__( 'Edit %s', 'backdrop-post-types' ),					$singular_label ),
			'new_item'				=> sprintf( esc_html__( 'New %s', 'backdrop-post-types' ),					$singular_label ),
			'view_item'				=> sprintf( esc_html__( 'View %s', 'backdrop-post-types' ),					$singular_label ),
			'search_items'			=> sprintf( esc_html__( 'Search %s', 'backdrop-post-types' ),				$plural_label ),
			'not_found'				=> sprintf( esc_html__( 'Not %s Found', 'backdrop-post-types' ),			$plural_label ),
			'not_found_in_trash' 	=> sprintf( esc_html__( 'Not %s Found in Trash', 'backdrop-post-types' ), 	$plural_label ),
		];

		$args = [
			'labels' => $labels,
			'public' => true,
			'has_archive'  => true,
			'menu_icon'    => 'dashicons-category',
			'show_ui'      => true,
			'show_in_rest' => true,
			'supports'     => apply_filters( "backdrop/post/type/supports/{$type}",  [ 'title', 'editor', 'thumbnail' ]),
			'taxonomies'   => [ $type . '_category', $type . '_tag' ],
			'rewrite'      => [ 'with_front' => false, 'slug' => $type ]
		];

		$this->posts[ 'backdrop-' . $type ] = array_merge( $labels, $args );
	}

	public function register_category() {
		foreach ( $this->posts as $key => $value ) {
			$this->create_category( $key );
		}
	}

	public function create_category( $type ) {
		$labels = [
			'name'                       => _x( 'Categories', 'Taxonomy General Name', 'backdrop-post-types' ),
			'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'backdrop-post-types' ),
			'menu_name'                  => __( 'Categories', 'backdrop-post-types' ),
			'all_items'                  => __( 'All Categories', 'backdrop-post-types' ),
			'parent_item'                => __( 'Parent Category', 'backdrop-post-types' ),
			'parent_item_colon'          => __( 'Parent Category:', 'backdrop-post-types' ),
			'new_item_name'              => __( 'New Category Name', 'backdrop-post-types' ),
			'add_new_item'               => __( 'Add New Category', 'backdrop-post-types' ),
			'edit_item'                  => __( 'Edit Categories', 'backdrop-post-types' ),
			'update_item'                => __( 'Update Categories', 'backdrop-post-types' ),
			'view_item'                  => __( 'View Categories', 'backdrop-post-types' ),
			'separate_items_with_commas' => __( 'Separate categories with commas', 'backdrop-post-types' ),
			'add_or_remove_items'        => __( 'Add or remove categories', 'backdrop-post-types' ),
			'choose_from_most_used'      => __( 'Choose from the most used', 'backdrop-post-types' ),
			'popular_items'              => __( 'Popular Categories', 'backdrop-post-types' ),
			'search_items'               => __( 'Search Categories', 'backdrop-post-types' ),
			'not_found'                  => __( 'Not Found', 'backdrop-post-types' ),
		];

		$args = [
			'labels'            => $labels,
			'hierarchical'      => true,
			'public'            => true,
			'show_ui'           => true,
			'show_admin_column' => true,
			'show_in_nav_menus' => false,
			'show_tagcloud'     => false,
			'show_in_rest'      => true,
		];
		register_taxonomy( $type . '_category', array( $type ), $args );
	}

	public function register_tag() {
		foreach ( $this->posts as $key => $value ) {
			$this->create_tag( $key );
		}
	}

	public function create_Tag( $type ) {
		$labels = [
			'name'                       => _x( 'Tags', 'Taxonomy General Name', 'backdrop-post-types' ),
			'singular_name'              => _x( 'Tag', 'Taxonomy Singular Name', 'backdrop-post-types' ),
			'menu_name'                  => __( 'Tags', 'backdrop-post-types' ),
			'all_items'                  => __( 'All Tags', 'backdrop-post-types' ),
			'parent_item'                => __( 'Parent Tag', 'backdrop-post-types' ),
			'parent_item_colon'          => __( 'Parent Tag:', 'backdrop-post-types' ),
			'new_item_name'              => __( 'New Tag Name', 'backdrop-post-types' ),
			'add_new_item'               => __( 'Add New Tag', 'backdrop-post-types' ),
			'edit_item'                  => __( 'Edit Tags', 'backdrop-post-types' ),
			'update_item'                => __( 'Update Tags', 'backdrop-post-types' ),
			'view_item'                  => __( 'View Tags', 'backdrop-post-types' ),
			'separate_items_with_commas' => __( 'Separate Tags with commas', 'backdrop-post-types' ),
			'add_or_remove_items'        => __( 'Add or remove Tags', 'backdrop-post-types' ),
			'choose_from_most_used'      => __( 'Choose from the most used', 'backdrop-post-types' ),
			'popular_items'              => __( 'Popular Tags', 'backdrop-post-types' ),
			'search_items'               => __( 'Search Tags', 'backdrop-post-types' ),
			'not_found'                  => __( 'Not Found', 'backdrop-post-types' ),
		];

		$args = [
			'labels'            => $labels,
			'hierarchical'      => true,
			'public'            => true,
			'show_ui'           => true,
			'show_admin_column' => true,
			'show_in_nav_menus' => false,
			'show_tagcloud'     => false,
			'show_in_rest'      => true,
		];
		register_taxonomy( $type . '_tag', array( $type ), $args );
	}

    	/**
	 * Construct.
	 */
	public function boot() {
		add_action( 'init', [ $this, 'register_post_types' ] );
		add_action( 'init', [ $this, 'register_category' ] );
		add_action( 'init',  [ $this, 'register_tag' ] );
	}
}

The doc isn’t yet available on cp.
It’s a manually edited doc… which I think isn’t ideal for this kind of - but probably there’s no better way to do this…
Ive added it to the notion roadmap.

@getkoded007 you seem to use a class
So it ultimately depends when your class is instantiated

Assuming you instantiate it in a local function - that’s the hook that’ll count. But if for example your class isn’t instantiated in a local function and thus runs globally then the plugins loaded hook is the one adding it.
That’s why usually you’d instantiate your class in a local function added to a specific hook (like init or so)

1 Like

WHY!!!??? would you want the theme to affect what the post type supports?
The theme should react according to what the post type supports, meaning that it doesn’t try to output a title if none is supported and no thumbnail if that’s not supported.
The theme should not be influencing the post type, only displaying it!!!

WHY!!! Because I want too and is a personal choice. Is NOT LIKE you’re going to be using my theme or whatever… you can do whaever you want with your theme on how you like it. and I can do what i want with my themes.

I agree this seems a bit strange. Themes should mostly be concerned with presentation, not functionality - that should be left to plugins.

Anyway, if that doesn’t work then there is some other issue and we would need to look at the full code to find it.

Finally, I would actually not include this filter at all. There is already a filter that allows modifying the entire $args array sent to register_post_type, which serves this purpose and also any other parameter that might need to be modified: register_post_type_args | Hook | ClassicPress Documentation

I think you guys are missing the point here. This is for my plugin even if it uses post type support all i need some inputs on how to add filter to the supports FROM the plugin. if you guys are gonna shoot things down… there’s no need for me to be here…

you guys should already know that usign register_post_type is a plugin territory not for themes having users to change supports type using a filter isn’t a bad thing… is just a damn feature…

Here’s the problem from what I see from people’s response… Responses should be ideally appropriate… No need to shoot people down when you see something dumb or whatnot… This community should be helpful in response and not shoot people down for doign something they shoudln’t or should do.

Giving a solution is better than no solution and i could just figure out what’s wrong with the code or something.

Themes should be only about presentation and I agree with that… The problem is that you guys assume that its for my theme and not for my plugin… all i wanted is a solution to see there’s a way to filter the supports so that users can add other supports if they choose too… i don’t see any issues with that… is still a presentation thing if they want to add a custom fields.

I’m sorry but the atititude here is just plain wrong.

I do appreciated for @james and @anon66243189 for giving some solutions so i can figure out stuff for my plugin

You literally said in your original post that you wanted to call the function from the functions.php file in your theme. It seems a bit much to criticize those who responded and took you at your word.

okay so… if i’m too criticize on my part fine… but same goes with both parties… i don’t need to people to tell me that themes should only presentation and not functionality because I already know that form the start of doing development for WordPress.

Is the way people responds can be hurtful. how would you espond if someone tell u WHY would you need to do that…

This should be a community of people helping other out… and not criizing on how you write a code or don’t do that or do that… giivng out a solution on things is already a good thing on my part.

People can say… here’s a better solution or this or that… i don’t need people to tell me why don’t do that or do that. that’s wrong.

Yes! That’s correct… being able to add_filter to the supports is something I have been figuring out because of the way my code is written is not doing so. I also tried do a simple register_post_type functionality with the apply_filters in place for the supports seems to work fine… so at the point… is the way of my code is writen is not allowing me… snce its filtering later.

but anways… @james , @timkaye and @joyously… sorry for snapping… it wasn’t my intention whatsoever… its just been a crappy week.

3 Likes

A post was split to a new topic: Theme vs plugin responsibilities

at this point… I’ve been following WordPress guidelines that has register_post_type as a plugin territory… and I always have a template file like single-portoflio.php or archive–portoflio.php that implements the post type…

If CP allows register_post_type as part of the theme… is totally up to u guys… i guess. i mean it would be a lot easier than havign to install a plugin whatsoever. but that’s just me.

We have a simple guideline about how to interact in this community - and using bolded text with lots of exclamations is not what I would consider “being nice”.

In fact, if I would have replied like that in my years of support (I hope) my boss would have fired me.

There’s two ways to communicate a possible error:
From “above”, or from a level of laid back professional and technical perspective.

“The theme should not” versus “why!!!??? {…} xx should never!!!” is a perhaps small difference to the untrained eye but thru experience I can say that’s not how support should be provided.

Neither in verbal, nor in written form.

The “being nice” is one of three pillars of professional support (the other being technical accuracy and speed).

So I’d say you don’t have to apologize for not digesting that so well.
The only you can do is apply the same professionalism and try to not get triggered by a possibly wrongly sentenced reply (believe me in support you get called all sort of things, and you can never “talk back”. Ignoring is one of the best approaches, the sad part with it is that then often valid and valuable information is ignored along with it, just because it was communicated in a suboptimal way)

That said, Let’s just try to stay on a certain level and start from the assumption that not everyone might speak the same language, not know the same things and most of us get scared when “yelled” at. That’s no environment I’d want to be part of.

In a marriage, the same kind of conversation approach would quickly be “verbal abuse”

5 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.