Open grantpalin opened 9 years ago
Do you have the full project somewhere that I can poke around?
Yes, here's the repo: https://bitbucket.org/grantpalin/wp-plugin-projecting
I have same problem too. Actually, where is the best place or file in boilerplate to put function of register post type, calling that function from activate, flush_rewrite_rules and then add_action it? I have tried with the static function that regist cpt, and put it in class-plugin-name-admin, and everthing fine without 404. but i want to avoid static function to do that. Can you give the example how to put CPT and flush_rewrite_rules in a good way please? Thanks.
What I have doing in one of my plugins, is following the tips given by this blog post.
Let's assume that I am creating a plugin to manage library books. So what I do is:
1. Activator: In Plugin_Name_Activator::activate
I check and create a flag, like this:
if( ! get_option( 'prefix_do_flush_rewrite' ) ) {
add_option( 'prefix_do_flush_rewrite', true );
}
2. Admin Class: Then in Plugin_Name_Admin
class, I create a method like this:
public function maybe_flush_rewrite_rules() {
if( get_option( 'prefix_do_flush_rewrite' ) ) {
flush_rewrite_rules();
delete_option( 'prefix_do_flush_rewrite' );
}
}
3. Main Class: Finally, to bring the blocks together, in Plugin_Name::define_admin_hooks
instantiate the classes and register the hooks with appropriate priorities.
$plugin_admin = new Plugin_Name_Admin( $this->get_plugin_name(), $this->get_version() );
$books_manager = new Plugin_Name_Books( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'init', $plugin_admin, 'maybe_flush_rewrite_rules', 20 );
$this->loader->add_action( 'init', $books_manager, 'setup_post_type', 10 );
The Plugin_Name_Books::setup_post_type
method is where I call the WordPress register_post_type
function.
Hope to help.
I'm using the boilerplate to implement a custom plugin for my own use. I've added code to register one custom post type and one custom taxonomy. I use a custom permalink structure for my posts, which my custom post type by default appears under. It works here - an instance does appear as expected when viewed from the front-end. To get around the permalink base, I tried adding
with_front => false
to therewrite
array; when I do this, there is no apparent error, but the same CPT entry generates a 404 error when viewed under the now-unprefixed permalink. Oddly, I have no issue with viewing the custom taxonomy archive. Also, the CPT archive listing does work - it's just the single view that does not.I've added below the relevant code blocks.
In the activator class:
In the orchestration class:
In the CPT orchestration class:
And in the CPT registration class:
Apologies for all the code blocks, they seem the relevant ones to include. I'm still figuring out the boilerplate, it does seem to take some coordination effort to correctly register custom types/taxonomies.
Again, the CPT views correctly when
with_front
is left off or set totrue
, yet the same, with the updated permalink, 404s out whenwith_front
is set to false.http://.../blog/projects/projectname/
- OKhttp://.../projects/projectname/
-> 404 errorI could possibly live with updating my base custom permalink for posts (this artifact of the blog-only days is a nuisance at times) since I was thinking to do it anyway, but I'd like to know if there's a reason why the otherwise functional settings do not work here.