配置注册哪些插件自定义帖子类型

时间:2013-08-12 作者:Rabbott

我正在编写一个插件,创建自定义帖子类型,但我希望用户能够通过functions.php 文件

我遇到的问题是自定义帖子类型register_post_type 必须在initfunctions.php 在这之后很久才读到。

如何允许用户使用过滤器或操作提供一个数组,以控制哪些帖子类型register_post_type 接到电话了吗?

// 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"));
}
过滤器工作正常,所以我知道它正在被调用,但不管出于什么原因,这些操作都没有被调用。

2 个回复
SO网友:Ben Miller - Remember Monica

您可以让用户在管理员的选项页面上选择自定义帖子类型,而不是让用户通过过滤器或操作打开或关闭自定义帖子类型。当你在数据库中有这些选择时,你的插件可以在你需要的任何时候查看它们。

如果仍要在函数中使用挂钩。php中,您可以设置一个钩子来设置数据库中的选项,而不是创建一个新的选项页面。

SO网友:RRikesh

我遇到的问题是自定义帖子类型register_post_type 必须在initfunctions.php 在这之后很久才读到。

那不是真的。functions.php 之前已读取init 因为您可以在其中声明函数,然后将它们挂接到init.

由于您希望避免设置页面,并且希望用户修改代码(风险自负),因此可以使用以下标志:

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 );
  }

}
未测试,但您可以使用类似的方法来收集帖子类型:

$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
在插件中:

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\');

结束

相关推荐

Display All Non-Used Plugins

我的公司目前拥有大约20个多站点,并且每天都在增长。我们正在尝试通过插件并制定标准。IE,表单使用插件X。然而,我们还没有找到一种单一的方法来检查和系统地显示哪些插件甚至没有被使用。是否有一个功能可以向我们显示已使用或未使用的插件?我试着寻找我能想到的一切,但我一生都找不到答案。