plugins - Restrict Custom Post Type to One Item

I'm looking to build a plugin that stores settings for a companion app my Wordpress site works with. I've gott

I'm looking to build a plugin that stores settings for a companion app my Wordpress site works with. I've gotten to the point where I have a custom post type mobile_settings that stores labels and colors unique to the companion app.

I've gotten to the point where I have my new post type and it's meta values stored, as well as created a new admin page. Here's what I can't figure out to do:

  1. On the admin page, navigate the user to the "New Item" view if no mobile_settings have been created.
  2. If a single mobile_settings record has been created, navigate the user to it's edit view.

Once I have those, I know how to hide the custom post type from the UI, so the user will think it's a straight "options" page instead of a custom post type.

We are strictly building this from scratch in order to maintain a database structure that works for our needs. It's actually not as hard as I thought it would be =)

Any tips would be appreciated.

Edit: I guess some code would be appreciated too, huh?

add_menu_page(
    'Mobile Settings',
    'Mobile Settings',
    'manage_options',
    'mobile-settings.php',
    function () {
      $mobile_settings = get_posts(array('post_type' => 'mobile_settings', 'posts_per_page' => 1));
      if (count($mobile_settings) > 0) {
        wp_redirect(get_edit_post_link($mobile_settings[0]->ID) . '&noheader=true');
        exit;
      }
    },
    'dashicons-smartphone',
    5
  );

I'm looking to build a plugin that stores settings for a companion app my Wordpress site works with. I've gotten to the point where I have a custom post type mobile_settings that stores labels and colors unique to the companion app.

I've gotten to the point where I have my new post type and it's meta values stored, as well as created a new admin page. Here's what I can't figure out to do:

  1. On the admin page, navigate the user to the "New Item" view if no mobile_settings have been created.
  2. If a single mobile_settings record has been created, navigate the user to it's edit view.

Once I have those, I know how to hide the custom post type from the UI, so the user will think it's a straight "options" page instead of a custom post type.

We are strictly building this from scratch in order to maintain a database structure that works for our needs. It's actually not as hard as I thought it would be =)

Any tips would be appreciated.

Edit: I guess some code would be appreciated too, huh?

add_menu_page(
    'Mobile Settings',
    'Mobile Settings',
    'manage_options',
    'mobile-settings.php',
    function () {
      $mobile_settings = get_posts(array('post_type' => 'mobile_settings', 'posts_per_page' => 1));
      if (count($mobile_settings) > 0) {
        wp_redirect(get_edit_post_link($mobile_settings[0]->ID) . '&noheader=true');
        exit;
      }
    },
    'dashicons-smartphone',
    5
  );
Share Improve this question edited Mar 26, 2019 at 12:35 EHorodyski asked Mar 26, 2019 at 12:28 EHorodyskiEHorodyski 1012 bronze badges 3
  • Have you considered using the wp_options table and just building an options page, instead of a whole CPT? It seems like that would be easier to tailor to your needs. – WebElaine Commented Mar 26, 2019 at 12:37
  • I haven't -- could you point me to a tutorial? Also, would it be extensible enough to store arrays? – EHorodyski Commented Mar 26, 2019 at 12:42
  • codex.wordpress/Creating_Options_Pages – RiddleMeThis Commented Mar 26, 2019 at 13:37
Add a comment  | 

1 Answer 1

Reset to default 0

Thanks to @WebElaine -- I'm converting my custom post type into a Settings page. Can you tell I'm new to this? =)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745657640a4638639.html

相关推荐

  • plugins - Restrict Custom Post Type to One Item

    I'm looking to build a plugin that stores settings for a companion app my Wordpress site works with. I've gott

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信