Remove sub menus from the admin view for a plugin that registers a custom post type - wordpress

I know there are 357,982 other posts about this BUT they all kinda lack something ie. an actual example that works for those of us who don't write 4,594,334 line of code every day.
SO - As it stands the scenerio is:
A plugin that registers a custom post type
A sub-menu you want to hide
What next?

The best solution I have found is actually pretty easy and requires a bit of inspection of the source and a good understanding of what to look for.
In this example woocommerce is registering the custom post type 'product' with a sub-menu that appears as 'Product Options'. We want to hide this for non-admin users.
Doing an inspection of the menu items we find that the hyperlink for the parent menu is 'edit.php?post_type=event_ticket' - looking a little further we see that the hyperlink for the sub-menu is 'https://websitename.com/wp-admin/edit.php?post_type=product&page=product_attributes'
We will use the 'add_action' hook as shown below. Please note that we are using the url for the parent menu however we are ONLY using the page parameter for the child.
add_action('admin_menu', 'remove_menu_pages', 999);
function remove_menu_pages()
{
if (current_user_can('manage_options') == false)
{
//1st parameter is parent URL | second is the 'page' parameter from the child url
remove_submenu_page('edit.php?post_type=product', 'product_attributes');
}
}
Add this to your functions.php and then login as a non-admin user and the submenu should now be hidden.

Related

How to set custom post type as parent of page?

I've found plenty of info on how to set a page as the parent of a custom post type, but not the other way round - I want to set a custom post type as the parent of my page.
I've found the following hook, originally intended to add drafts to the list of available parents for a page. It works for adding drafts, but if I use it to chaneg the retrieved post type, the parent pulldown on the page edit screen disappears. Any ideas?
add_filter( 'page_attributes_dropdown_pages_args', 'so_3538267_enable_drafts_parents' );
add_filter( 'quick_edit_dropdown_pages_args', 'so_3538267_enable_drafts_parents' );
function so_3538267_enable_drafts_parents( $args )
{
//$args['post_status'] = 'draft,publish,pending';//works
$args['post_type'] = 'campaign';//custom post type name - doesn't work
return $args;
}
I've got it working. The code above is good, I just needed to set my custom post type to be hierarchical.
Edit:
I spoke too soon. The above change allowed me to select the custom post type as the parent of the page, but now the child page is inaccessible on the frontend. I get a 404 error. I've tried flushing permalinks as usual.
The URL it's trying to use for the page:
http://www.example.com/sample-campaign-6/about-campaign/
the first part is the slug for the parent custom post type.
Can anyone tell me how to get this working?
Thanks.

How to disable page's title in wp-admin from being edited?

I have a wp-network installed with users that can create pages in each site.
Each of those pages get a place in the primary menu, and only one user have permission to create all this menu.
I want to create a user only to be able to edit the content of the pages, but not the title.
How can I disable the title of the page to be edited from the admin menu for a specific user, or (far better) for a capability?
I thought only a possibility, that's editing admin css to hide the title textbox, but I have two problems:
I don't like to css-hide things.
I don't know where is the admin css.
I know php, but don't know how to add a css hide to an element for a capability.
You should definitely use CSS to hide the div#titlediv. You'll want the title to show in the markup so the form submission, validation, etc continues to operate smoothly.
Some elements you'll need to know to implement this solution:
current_user_can() is a boolean function that tests if the current logged in user has a capability or role.
You can add style in line via the admin_head action, or using wp_enqueue_style if you'd like to store it in a separate CSS file.
Here is a code snippet that will do the job, place it where you find fit, functions.php in your theme works. I'd put it inside a network activated plugin if you're using different themes in your network:
<?php
add_action('admin_head', 'maybe_modify_admin_css');
function maybe_modify_admin_css() {
if (current_user_can('specific_capability')) {
?>
<style>
div#titlediv {
display: none;
}
</style>
<?php
}
}
?>
I resolved the problem, just if someone comes here using a search engine, I post the solution.
Doing some research, I found the part of the code where the title textbox gets inserted, and I found a function to know if a user has a certain capability.
The file where the title textbox gets added is /wp-admin/edit-form-advanced.php. This is the line before the textbox
if ( post_type_supports($post_type, 'title') )
I changed it to this
if ( post_type_supports($post_type, 'title') and current_user_can('edit_title') )
That way, the textbox is only added when the user has the capability called "edit_title"
When this IF block ends few lines after, I added:
else echo "<h2>".esc_attr( htmlspecialchars( $post->post_title ) )."</h2>";
To see the page title but not to edit it, when the user hasn't got "edit_title" capability.
Then I had already installed a plugin to edit user capabilities and roles, wich help me to create a new capability (edit_title) and assign it to the role I want.

Wordpress show sub menu items on index page

I am using wordpress and on the page "sidebar.php" I have the following code:
<?php wp_list_pages('post_type=wiki&depth=1'); ?>
It works great but what I need to do it that what I click on a Menu item on the sidebar I need the sub items to display on the main page.
UPDATE:
What I basically need to do is to have the first level items on the left (As it currently is), and when those links are clicked then the sub items of those items will be listed on the index.php (main page).
I am using the wp-wiki plugin to display the pages as wiki pages but the actual but the list is the same, just showing as a different type:
post_type=wiki
Can anyone help please?
Thanks
<?php
wp_list_pages('sort_column=menu_order&title_li=&child_of='.$post->ID.'&depth=0');
?>
There's several ways you can accomplish this. WordPress actually has an example of exactly what you're looking to do in the Codex. (link - the last example in that section, right above the "List subpages even if on a subpage" heading)
There's several ways to do this though - that's just one example. But that code above, you just pop into your sidebar.php file. You can also create a widget out of it. And as someone else mentioned, you can use the new menu system for WordPress (but you can indeed, make it dynamic.) But the above example in the Codex is the simplest method.
Actually, an easier method would be just to list all subpages and use CSS to hide and show the child items based on what page you're on. The classes are already set up for you. Just view your source code and you can see it. So you'd do something like:
`li ul.children {display:none; }
li.current_page ul.children { display:block; }`
and variations thereof.
What about using the new menu function in wp 3?
That way you could include your type with any sub page you want (won't be dynamic though) and then add your favorite javascript or good css to show those sub-pages.

Making wordpress menu remain highlighted (class="current-page-item" in the menu) for a category listing (not a page)

I have a site that I have completely customized the theme.
Unfortunately the theme and the way it's set up doesn't like you to view the actual page permalink for both the blog and the portfolio to view its content.
Regular pages work fine:
site.com/about
site.com/contact
However, if you go to /portfolio or /blog the pages show up blank
As a result, I had to use appearance>menu to make a custom menu that links to:
/category/portfolio/#all
/category/blog
This makes the content now show up, the only problem is when they click on the portfolio or the blog, wordpress thinks that it's not actually viewing that page, so the tab in the main menu doesnt remain highlighted (to show you what page youre currently on)
Does anyone have any idea how to fix this?
http://eastcoastefx.com/ggqq
Thank you :)
I have done this before. It's not pretty, but it works if both items are WordPress pages.
// hack the queried_object_id for wp_list_pages
global $wp_query;
$queried_object_id = $wp_query->queried_object_id;
if ($season_page) {
// ensure season pages have Season highlighed in nav
$wp_query->queried_object_id = 22;
}
wp_list_pages($args);
// set queried_object_id back to the original
$queried_object_id = $queried_object_id;
I'd be interested to know if you found anything better on the WordPress forum per Mike Schinkel's comment as the above is clearly a hack.

Get a node to show up underneath a View's menu item

So I've got a content type of "News" and then a View which shows a list of News nodes as a menu item.
Is there any way to highlight the News View in the menu tree when you're viewing a News node?
I'm sort of aware of why this doesn't work, and why doing this might be hacky, but there's got to be a way to have a "default" menu item or something that a node can show up under.
Actually, I found a module that does basically what the template.php answer above does, by allowing you to assign a default menu item by content type.
It's called Menu Trails. Here is an excerpt from its project page:
... adds some common-sense usability to Drupal's menu system
Menu Trails implements primary/secondary links which keep the current menu trail "active" or highlighted. A handy snippet ready to go into your template.php is included.
The module provides a means of broadly categorizing nodes (by type or taxonomy) as falling "under" a known menu item. These nodes are not added to the menu tree (keeping the menu admin system sane) but they will trigger the functionality above -- preserving navigation state for the user -- when viewed.
New for 6.0: Menu Trails can also set breadcrumbs for nodes, keeping them in sync with the trail.
New for 6.0: Menu Trails is now Organic Groups aware, so nodes can be designated to fall "under" the first group node they belong to.
New for 6.0: A token is exposed to pathauto (and other token-aware modules) allowing for the menu trail to be used in automatic path alias creation.
Assuming the answer to my above comment is "yes," this is something you can accomplish through the template.php file in your theme folder
The final solution will depend on your settings, but generally most of the action will happen inside phptemplate_preprocess_page
// Simplified, and untested in this form
function phptemplate_preprocess_page(&$vars) {
if (isset($vars['node'])) { // if we're looking at a node
$body_classes[] = (($vars['node']->type == 'News') || (arg(0) == 'News')) ? 'news_page' : ''; // Page is news page
}
else if (arg(0) == 'taxonomy') {
// ... Special handling for news taxonomy pages, if you want
} else {
$body_classes[] = "page-type-" . arg(0);
}
$body_classes = array_filter($body_classes); // Out! Out! Damn empty elements!
$vars['body_classes'] = implode(' ', $body_classes);
}
This will give the you the body.news_page style class to work with, and you can combine that with the style of your news menu block to make it appear how you like.
if Menutrails doesn't work (for me it didn't) you can do it yourself by something like:
$somenode = menu_get_item('node/5');
menu_set_item($somenode);
I have found you have more control if you do something like this in hook_nodeapi().
// this code worked for me
$somenode = menu_get_item();
$somenode['href'] = 'node/5';
menu_set_item(NULL, $somenode);
You may need to set the weight of your custom module to be heavier than core modules: at least 1

Resources