0

I am writing a plugin that creates custom post types, but i want users to be able to turn them on and off via a filter in the functions.php file.

The issue i'm having is that custom post types register_post_type has to be called during init and functions.php is read far after this.

How can I allow users to provide an array using a filter or action that will control which post types register_post_type gets called?

// currently the custom post type constructor has this
if(!post_type_exists($this->name)) {
  add_action("init", array($this, "register_post_type"));
  add_action("init", array($this, "configure_meta_boxes"));
  add_action("save_post", array($this, "save"));

  add_filter('myplugin_get_shortcodes', array($this, "configure_shortcodes"));
}

The filter works just fine so I know it's being called, but for whatever reason the actions aren't.

0

Instead of having the users turn on and off the custom post types via a filter or action, you could have them select custom post types on an options page in the admin. When you have these selections in the database, your plugin can look at them at any point you need.

If you still want to use hooks in functions.php, you could set up a hook that would set the option in the database, instead of creating a new options page.

  • yea, the plugin is really for personal use and for use within themes so i was trying to avoid a settings page. – Rabbott Aug 12 '13 at 4:26
0

The issue i'm having is that custom post types register_post_type has to be called during init and functions.php is read far after this.

That's not true. functions.php is read before init because you can declare your functions in there and then hook them to init.

Since you want to avoid a settings page and you want your users to modify the code (at your own risk), you could use some flags:

add_action('init', 'wpse_109719_post_types');

function wpse_109719_post_types(){

  $post_type1 = true;
  $post_type2 = true;
  $post_type3 = false; // your users don't want this one

  if( $post_type1 ){
    register_post_type( 'your_post_type_1', $args );
  }

  if( $post_type2 ){
    register_post_type( 'your_post_type_2', $args );
  }

  if( $post_type3 ){
    // won't be registered since $post_type3 was declared as false
    register_post_type( 'your_post_type_3', $args );
  }

}


Not tested, but you can use something like that to collect the post types:

$val = array ();
$options_array = apply_filters( 'my_unique_filter', $val );
print_r( $options_array );
// make a loop and register_post_type here. don't forget to hook at init

In your plugins:

function filter1( $val ){
  $val[] = 'post_type1';
  return $val;
}

function filter2( $val ){
  $val[] = 'post_type2';
  return $val;
}

function filter3( $val ){
  $val[] = 'post_type3';
  return $val;
}


add_filter('my_unique_filter', 'filter1');
add_filter('my_unique_filter', 'filter2');
add_filter('my_unique_filter', 'filter3');
  • Well, the idea would be that they would use add_filter or add_theme_support rather than modifying the plugins code. – Rabbott Aug 12 '13 at 13:21
  • updated my answer – RRikesh Aug 12 '13 at 13:33
  • I've tried that, but it doesnt work.. it is my understanding that I should be using add_action("init", array($this, "register_post_type")); within my custom post type, where the method in the custom post type 'register_post_type' actually calls the wp register_post_type.. no? – Rabbott Aug 12 '13 at 13:51
  • Based on wp.tutsplus.com/tutorials/creative-coding/… – Rabbott Aug 12 '13 at 13:52
  • Just updated the question a bit... – Rabbott Aug 12 '13 at 14:04

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.