Skip Navigation

[Résolu] Disable the Add new post capability

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to limit a custom post type so that only one post of that post type can be created.

Solution:
The solution doesn't strictly prohibit the creation of additional posts, it simply hides the UI for adding new posts when a post of that type already exists.

Add the following to the theme's functions.php file, remembering to update the id of the existing post of that type and the slug of the custom post type, which in the example code is global-setting.

/**
 * Custom code to hide UI to create >1 global-setting posts
 */
function disable_new_posts() {
  
    $global_settings = get_posts( 'post_type=global-setting' );
 
    if ( count($global_settings) != 0 ) {
      // Remove sidebar link
      global $submenu;
      unset($submenu['edit.php?post_type=global-setting'][10]);
 
      // Hide Add new button with CSS
      // * change post id to id of existing post on next line *
      if ((isset($_GET['post_type']) && $_GET['post_type'] == 'global-setting') || $_GET['post'] == 273) {
          echo '%MINIFYHTMLb8a8432e92a03a7d02f4061d76fed79384%';
      }
    } 
}
add_action('admin_menu', 'disable_new_posts');

Relevant Documentation:

This support ticket is created Il y a 7 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 7 réponses, has 2 voix.

Last updated by Nigel Il y a 7 années et 10 mois.

Assisted by: Nigel.

Auteur
Publications
#406072
Screen Shot 2016-06-10 at 8.34.06 PM.png

Dear Sir/Madam,

I research from Internet how I can disable the capability of adding new post from http://stackoverflow.com/questions/3235257/wordpress-disable-add-new-on-custom-post-type

I also read the article from https://codex.wordpress.org/Function_Reference/register_post_type

What is my $post_type if I have a custom post type "global-setting" as post slug?

I want to limit the number of this post type to only one, than I can call api to get the field value as global setting via out the whole site.

I enclosed the screenshot for your reference. Some of my custom post is in Traditional Chinese name in plural form.

#406118

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Kelvin

I'm not entirely sure about what you intend to do once you have limited the custom post type to just one, but let me discuss some of the issues which may hopefully be helpful.

Firstly, if you created a custom post type with a slug of "global-setting" then your post_type == "global-setting".

As an alternative to allowing just one of the custom post type, have you considered using Access so that only the site admin has the capability to make and edit the post type? All other users can be granted read access only (or no access at all)? https://toolset.com/documentation/user-guides/setting-access-control/

If it is important that even the site admin can only ever create one of this CPT, then you are going to need to use some custom PHP coding. The links you added will help. Bear in mind that the solutions which involve changing how the custom post type are registered won't help (you would never be able to create a post in the first place) and in any case they are registered by Types where you don't have the option you would require.

You would need a solution that uses get_posts for your global-setting CPT and if the number of posts returned is anything other than zero it should use something like this answer: http://stackoverflow.com/a/16377281/1180189

Your code would look something like this. I haven't tested it, and please note we can't provide support for it, but it should get you started:

function disable_new_posts() {

    $global_settings = get_posts( 'post_type=global_settings' );

    if ( count($global_settings) != 0 ) {

      // Hide sidebar link
      global $submenu;
      unset($submenu['edit.php?post_type=global_settings'][10]);

      // Hide link on listing page
      if (isset($_GET['post_type']) && $_GET['post_type'] == 'global_settings') {
          echo '<style type="text/css">
          #favorite-actions, .add-new-h2, .tablenav { display:none; }
          </style>';
      }
    } 
}
add_action('admin_menu', 'disable_new_posts');
#406392

Dear Nigel,

Thanks for your reply. I did consider the Access but it only set whether user can edit or read the post, no option about create post.

I create the custom post "global-setting" because I only want to have one post to contain the global variable via the whole site. I only query the first record and retrieve the variable.

I tried your code but no luck even I change the global_setting to global-setting (my custom slug for this post). I inspect the Dashboard but there is no #favorite-actions element.

I refer to the http://stackoverflow.com/questions/3235257/wordpress-disable-add-new-on-custom-post-type, may I have your advice what my custom_post_type_name should be?

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => 'do_not_allow', // false < WP 4.5, credit @Ewout
  ),
  'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
#406553

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Screen Shot 2016-06-13 at 11.22.29.png

With Access you have the option to restrict who can publish posts (which is to create new posts).

See the screenshot.

The code snippet you included in your last reply doesn't help you. You are not registering the CPT, the CPT is registered by Types already.

I checked the code I suggested earlier. The Stack Overflow article you linked to before appears to be out of date, I amended the CSS used to hide the "Add New" button, and I also corrected "global_settings" to "global-setting". I tested the following and it worked, removing "Add new" from the left navigation sidebar and the button when viewing global settings posts whenever there is > 0.

/**
 * Custom code to hide UI to create >1 global-setting posts
 */
function disable_new_posts() {
 
    $global_settings = get_posts( 'post_type=global-setting' );

    if ( count($global_settings) != 0 ) {
      // Remove sidebar link
      global $submenu;
      unset($submenu['edit.php?post_type=global-setting'][10]);

      // Hide Add new button with CSS
      if (isset($_GET['post_type']) && $_GET['post_type'] == 'global-setting') {
          echo '<style type="text/css">
          .page-title-action { display:none; }
          </style>';
      }
    } 
}
add_action('admin_menu', 'disable_new_posts');
#406693
Screen Shot 2016-06-13 at 10.19.28 PM.png
Screen Shot 2016-06-13 at 10.17.56 PM.png
Screen Shot 2016-06-13 at 10.14.41 PM.png

Dear Nigel,

Many thanks for your code, yes it helps to hid the submenu but I also need to add below code to hide the "Add New" to the edit page wp-admin/post.php?post=273&action=edit

if ((isset($_GET['post_type']) && $_GET['post_type'] == 'global-setting') || $_GET['post'] == 273) {
          echo '<style type="text/css">
          .page-title-action { display:none; }
          </style>';
      }

I try not to use the code and only update the right for Editor from Access, see screenshot, I uncheck the delete and publish for the Global-setting, but I still can see the "Add New" button, in fact the Access can allow Editor to create new post but cannot publish to public only.

#406712

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

You are correct, if you use Access to prevent users from publishing, it does not actually prevent them from writing the posts, it just prevents the posts from being published, they will remain as drafts.

And well spotted, I had missed the Add new button when editing the existing global-setting post.

#406719

Dear Nigel,

No problem, you helps a lot. Now I can have an area to define the global setting for whole site by using the custom post. I would like to recommend Toolset to consider having the global setting feature. It is very useful to site developer to predefine the variable and reuse it anywhere.

If we have this then we can design our own setting such as allowing client to select the font-family from predefined font list, change the color, etc.

Could you help to forward this request to dev team? You can close this thread if you read it.

Best regards,

Kelvin.

#406729

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

Timezone: Europe/London (GMT+01:00)

Hi Kelvin

I will discuss this with our developers and if they think it is something they can implement I will create a formal feature request linked to this ticket.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.