0

I am working on a plugin which register a custom post type, using a custom rewrite structure, defined as follows:

  /**
   * create_ldp_type - LDP Resource post type creation and registration
   *
   * @return {type}  description
   */
  public function create_ldp_type() {
      register_post_type('ldp_resource',
          array(
              'labels'  => array(
                  'name'              => __('Resources', 'wpldp'),
                  'singular_name'     => __('Resource', 'wpldp'),
                  'all_items'         => __('All resources', 'wpldp'),
                  'add_new_item'      => __('Add a resource', 'wpldp'),
                  'edit_item'         => __('Edit a resource', 'wpldp'),
                  'new_item'          => __('New resource', 'wpldp'),
                  'view_item'         => __('See the resource', 'wpldp'),
                  'search_items'      => __('Search for a resource', 'wpldp'),
                  'not_found'         => __('No corresponding resource', 'wpldp'),
                  'not_found_in_trash'=> __('No corresponding resource in the trash', 'wpldp'),
                  'add_new'           => __('Add a resource', 'wpldp'),
              ),
              'description'           => __('LDP Resource', 'wpldp'),
              'public'                => true,
              'show_in_nav_menu'      => true,
              'show_in_menu'          => true,
              'show_in_admin_bar'     => true,
              'supports'              => array('title'),
              'has_archive'           => true,
              'rewrite'               => array('slug' => 'ldp/%ldp_container%'),
              'menu_icon'             => 'dashicons-image-filter',
      ));
  }

I was previously flushing the rewrite rules on an init hook, and as it seems not to be a proper solution I decided to switch on using the register_activation_hook method. As I am using the object-oriented approach, all my hooks are wrapped and called from the object constructor:

  /**
   * Default Constructor
   **/
  public function __construct() {
    register_activation_hook( __FILE__, array($this, 'wpldp_rewrite_flush' ) );
    register_deactivation_hook( __FILE__, array($this, 'wpldp_flush_rewrite_rules_on_deactivation' ) );

    register_activation_hook( __FILE__, array($this, 'generate_menu_item') );
    register_deactivation_hook( __FILE__, array($this, 'remove_menu_item' ) );

    // Entry point of the plugin
    add_action('init', array($this, 'wpldp_plugin_update'));
    add_action( 'init', array($this, 'load_translations_file'));
    add_action( 'init', array($this, 'create_ldp_type'));
    add_action( 'init', array($this, 'add_poc_rewrite_rule'));

  }

The code for my wpldp_rewrite_flush method is the following:

 public function wpldp_rewrite_flush() {
    // Register post type to activate associated rewrite rules
    $this->create_ldp_type();
    $this->add_poc_rewrite_rule();

    // Flush rules to be certain of the possibility to access the new CPT
    flush_rewrite_rules( true );
 }

I made sure that this code is executed by doing some debugging. I have been reading a lot of posts which all gives this solution as the best one, but my issue is that my custom post types cannot be accessed (404 error).

The URLs I am trying to access are of type http://localhost/wordpress/ldp/actor/benoit-alessandroni/, actor being a custom taxonomy term (a ldp_container. Using a call to flush_rewrite_rules() right after my call to register_post_type in the create_ldp_type method is working just fine, but I know that's not ok.

So, do you see anything obvious which could explain the issue ?

Thanks in advance,

1

Simple way: In my plugins, register_activation_hook always have a line like delete_option('rewrite_rules'); when I'm creating custom post types. WordPress will properly rebuild them.

I've provided a detailed answer

Also another inspiring answer here

4
  • Testing right now. Will let you know. – Balessan Aug 31 '16 at 14:11
  • Before or after the post type creation ? Doesn't seem to be enoug... Arg. – Balessan Aug 31 '16 at 14:15
  • In theory, it should not matter, since the rewrite_rules option will be rebuilt on the next page request, but this is arguable. Are you sure the error is a flush one? I mean if you manually flush the permalinks from dashboard it will work? – Andrei Aug 31 '16 at 14:37
  • Yes if I flush them manually it is fine. Really weird. I cannot get it to work programmatically :-( – Balessan Aug 31 '16 at 22:22

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.