Add new Wordpress page with preselected parent - wordpress

I do not want my clients to select a parent page from the drop down list, when creating a child page in Wordpres, so I would like to now if there is a way to create a link on the dashboard which links to "add new page" - but with a preselected parent page?
If this is not possible, then is there a way to change the default parent from "(no parent)" to a parent of my choice?

Put this script in your plugins folder and it'll give new pages a parent of whatever you set as $parent_id
<?php
function set_parent($content) {
/* >> Begin user-configurable variable >> */
$parent_id = '1'; // ID for parent page >> */
/* >> End user-configurable variable >> */
$pattern = "option value='{$parent_id }'";
$replace = "option value='{$parent_id }' selected=\"selected\"";
$content = str_replace($pattern, $replace, $content);
return $content;
}
if( strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php?post_type=page') ) {
ob_start('set_parent');
}
?>
That should do what you want. And I suppose you could add a dropdown option in somewhere to select parents if a set default parent isn't sufficient like in this example.

This is a tough question to answer sticking with core wordpress ideas. Would you normally have more than one parent page available?
Do you think that about a different way for your client to manage page structure would work?
Admin Column View Plugin is great and allows the user to use drag and drop to reorder pages - http://wordpress.org/plugins/admin-column-view/screenshots/
Hope this helps.

Related

Selecting child pages with a specific parent page doesn't work

I need to add a specific function when child pages of a specific parent page are visualized.
My parent page has ID= 115 and it hase a 7 child pages. I used the following code
global $post; // load details about this page
if(is_page()&&($post->post_parent== '115)) {
echo ' This is a subpage';
}
else {
echo 'This is not a subpage';
}
Though it should work, the output echo, when I visualize one of the child pages, is "This is not a subpage" -> it means that it doesn't recognize it as a child page.
What is wrong? Thank you very much
Want to detect whether a specific Page on a WordPress has children or not?
This function comes in handy, just add it to your functions.php:
function has_subpage() {
global $post;
if($post->post_parent){
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
} else {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
} if ($children) {
echo ' This is a subpage';
} else {
echo ' This is a not subpage';
}
}
You can achieve the desired result by taking the following steps listed below...
Within your child theme, on your page.php first check if your are on a sub page via $post->post_parent.
Then within that, you can return an array of ancestors via get_post_ancestors
Then setup a condition to get the ID of the top level parent page
Once you have the ID, you can easily target the parent page by name. I recommend this so that when your theme travels for your development environment to our live environment, or if you had to manually create pages in any of these environments, the names will be the same.
Finally, enter your code for the sub page that targets its specific parent page location.
if (is_page() && $post->post_parent) :
$parents = get_post_ancestors($post->ID);
$id = ($parents) ? $parents[count($parents) - 1] : $post->ID;
$parent = get_post($id);
$parent_slug = $parent->post_name;
if ($parent_slug == "slug-of-your-specific-parent-page") {
//TEST
echo "this is custom text displaying on my sub page for a specific parent page";
//OR ADD YOUR SPECIFIC FUNCTION
...
}
endif;

How to change the home page title text in navigation menu?

I have created a wordpress site. The home page appears in the navigation menu with the name of "Home".
I want to change this text to some other text so that in the navigation menu, it did not appear as a "Home", instead it should appear as some other text.
How will I change this text?
So, in order to Change the name of Home button go to:
wp-includes/post-template.php
Look for:
// Show Home in the menu
if ( isset($args['show_home']) && ! empty($args['show_home']) ) {
if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] )
$text = __('Home');
Do you see last "Home" text ? There you go, change it to whatever you want.
If we are talking about the default theme "Twenty Twelve":
Open ../twentytwelve/functions.php
Find twentytwelve_page_menu_args function.
Replace
$args['show_home'] = true;
with
$args['show_home'] = "Your name";
Hoooray!!! :)
Try this ...
http://en.forums.wordpress.com/topic/changing-text-of-home-link-to-something-else
It depends on each theme you would like to use.
For Twenty Twelve what you have to do is to set a front end page (static page) and give it a name.
then you copy functions.php to your child theme's folder and then find this:
/**
* Makes our wp_nav_menu() fallback -- wp_page_menu() -- show a home link.
*
* #since Twenty Twelve 1.0
*/
function twentytwelve_page_menu_args( $args ) {
if ( ! isset( $args['show_home'] ) )
$args['show_home'] = true;
return $args;
}
add_filter( 'wp_page_menu_args', 'twentytwelve_page_menu_args' );
and change this single line:
$args['show_home'] = false;
Alternatively,
a simple solution is to go to Menus tab on the left and play with the menu options there, you can add your own if you like.
Several years later since asking this question you can do this from the admin panel. There's a good explanation on this website. Basically you need to go to:
http://yourDomain/wp-admin > Appearance > Menus. Then, under the Menu structure you will find Home - click and change the title to whatever you want. I hope this helps someone ))

Editing menu items on the fly in Drupal?

I have a menu item called "Inbox" on a menu called "dealer-menu". I want to change "Inbox" to "Inbox (1)" or "Inbox (2)" depending on the number of messages the user has in his inbox. How do I change the value ON THE FLY. I.e. on every page refresh?
I ended up solving it like this:
$dealerMenu = menu_navigation_links('menu-dealer-menu');
$menu = theme('links', $dealerMenu);
print str_replace("Inbox", "Inbox (".get_number_of_messages_in_inbox().")", $menu);
If you call hook_menu_link_alter then you can set $item['options']['alter'] = TRUE; on each menu item - you will need to edit the menu item for this hook to be called and the alter permission set to true.
Once this has been set then hook_translated_menu_link_alter will be called before each menu item is rendered allowing you to change the menu item title.
Example code would be:
function MY_MODULE_menu_link_alter(&$item) {
$item['options']['alter'] = TRUE;
}
function MY_MODULE_translated_menu_link_alter(&$item, $map) {
if($item['mlid']==89) {
$item['title'] .= ' ('.get_number_of_messages_in_inbox().')';
}
}
The only limitation of this is that hook_menu_link_alter will mark every link as alterable which is not necessarily desirable (adverse effect on performance) - some additional checks on the $item here would mean you are only marking them menu items you want as alterable.

How to hide Edit | View tabs?

Can I hide the
Edit | View
tabs on top of each node ?
I've searched for this option in theme settings (both global and standard theme but I couldn't find it).
I still want to be able my customer to edit / administer content, so I cannot just remove the permission for it.
thanks
here is a very easy solution for you. (Drupal 7)
Open your page.tpl.php in your current template and search for the $tabs variable.
Remove the render code if you want to hide it completely.
If you want to display it only to administrators use this code
<?php if ($tabs and $is_admin): ?>
<div class="tabs">
<?php print render($tabs); ?>
</div>
The above code checks if the user is administrator. If it is it will render the tabs. If not it wont render them.
This really is a presentational thing, not a functionality thing, so it should be done at the theme level.
The problem with overriding theme_menu_local_tasks() is that you override/take a hatchet to the entire local task display, when you really just want to get in there with a scalpel to remove two specific local tasks. So, you need to get a little more specific.
theme_menu_local_tasks() gets the current page's local tasks and passes them to menu_local_tasks(). Here, two theme functions are used:
theme_menu_item_link(), which gets the link markup for the task
theme_menu_local_task(), which gets the <li> element for the task.
So, you can get rid of the View and Edit local tasks in a really robust way by overriding theme_menu_item_link() and theme_menu_local_task() to include your check for them:
function mytheme_menu_item_link($link) {
// Local tasks for view and edit nodes shouldn't be displayed.
if ($link['type'] & MENU_LOCAL_TASK && ($link['path'] === 'node/%/edit' || $link['path'] === 'node/%/view')) {
return '';
}
else {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
return l($link['title'], $link['href'], $link['localized_options']);
}
}
function mytheme_menu_local_task($link, $active = FALSE) {
// Don't return a <li> element if $link is empty
if ($link === '') {
return '';
}
else {
return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
}
}
This way, you're relying on the menu router path, not modifying the menu router item, and achieving the result you want with minimal changes to core functionality or theming.
On the module side, you could do something that decouples the Edit's menu entry from the local tasks for the node:
function custom_menu_alter(&$items) {
$items['node/%node/edit']['type'] = MENU_CALLBACK;
}
The edit path is still there, but now it is not associated with the View tab. This includes the edit page itself--no View tab there.
there is a module for that: tab tamer allows to hide or disable tabs and rename them as well.
I use the following in template.php by theme (which is perhaps a little hacky, I feel I should be considering unsetting $tabs instead):
function THEME_NAME_menu_local_tasks() {
return '';
}
Or you could ommit:
if ($tabs) echo $tabs;
from your page.tpl.php...
View and Edit are functional features. They have a reason for being there.
The best way to "remove" them, is to "remove" that functionality alltogether. After all: why remove the interface of a piece of functionality, but not the functionality itself?
Besides, simply not printing the tabs, does not remove the url endpoints. In other words: if you don't print the edit tab, people can still access the edit page.
Again: best is to remove that functionality: The fact that you don't want the edit tab, sounds as if you don't want the edit functionality for certain users.
If so, then just remove that permission for that role. That is all. The tabs will be gone.
If, however, you simply wish to display these tabs differently, Drupal is your friends. As you may have noticed, they are called local tasks and not tabs. That is because the theme decides how to render them: The theme is the thing that decides to show them as tabs.
Simply override the theme_menu_local_tasks() to create your own HTML for the "local-tasks". And in your page-tpl, simply move the $tabs variable around to a place, where you want them.
But again: Don't try to change the behavior of the app, by removing interface-elements. That is not the right thing to do: you should change the behavior, in order to change the behavior :)
For all the people stumbling upon this question while looking for a D7 solution: As stated on https://drupal.stackexchange.com/a/77964/15055 it's hook_menu_local_tasks_alter()
/**
* Implements hook_menu_local_tasks_alter() to unset unwanted tabs
*/
function MYMODULE_menu_local_tasks_alter(&$data) {
foreach ($data['tabs'][0]['output'] as $key => $value) {
if ($value['#link']['path'] == 'node/%/view') {
unset($data['tabs'][0]['output'][$key]);
}
}
}
This is not the answer to the question of what the author asked. But somehow it might be useful for others user who facing the similar problem with me. Please let me know if this is not suitable to put in here.
I get the answer from #grayside and modified a bit to hide the view | edit tab from node based on the content type I want.
function MYMODULE_menu_alter(&$items) {
$items['node/%node/view']['access callback'] = 'MYMODULE_disable_node_view';
$items['node/%node/view']['access arguments'] = array(1);
}
function MYMODULE_disable_node_view($node){
if($node->type == 'product'){
return false;
}
}
product is the machine name of my content type, I don't want anywant to access it including root user.
The simplest solution to hide the tabs is to add this class in your theme css
.tabs{ display:none;}
Thanks for the last answer. But be aware of this detail: if you try it as-is it cannot work: literals should be just rounded with " or ', not both of them altogether. So it should be:
/**
* Implements hook_menu_local_tasks_alter() to unset unwanted tabs
*/
function MYMODULE_menu_local_tasks_alter(&$data) {
foreach ($data['tabs'][0]['output'] as $key => $value) {
if ($value['#link']['path'] == "node/%/view") {
unset($data['tabs'][0]['output'][$key]);
}
}
}
Once taken care of it, it works :)
D8 solution: If you want to hide all "local" tabs on certain pages, remember that "Tabs" is listed in the block library: find it in the "Content" region and exclude by content type, page URL or user role.

Is it possible to *optionally* override a theme in Drupal 6?

I want to override the theming of only one (custom) menu. I can do this with phptemplate_menu_tree() but - of course - it overrides the rendering of all menus.
I've tried returning FALSE (an obvious technique IMO) if the menu is not the specific one I want to override - but this doesn't cause the overridden theme function to be called.
My only alternative (when the menu is anything other than the specific one) is to call the overridden function from within phptemplate_menu_tree() - but this seems to defeat the whole point of the override system, since the default rendering function will be hard-coded therein.
I hope the explanation is clear, and any help is greatly appreciated - tks.
UPDATE
For the sake of future reference, I'll explain how I solved this.
First off, the menu rendering starts with this function in menu.module:
function menu_block($op = 'list', $delta = 0) {
$menus = menu_get_menus();
// The Navigation menu is handled by the user module.
unset($menus['navigation']);
if ($op == 'list') {
$blocks = array();
foreach ($menus as $name => $title) {
// Default "Navigation" block is handled by user.module.
$blocks[$name]['info'] = check_plain($title);
// Menu blocks can't be cached because each menu item can have
// a custom access callback. menu.inc manages its own caching.
$blocks[$name]['cache'] = BLOCK_NO_CACHE;
}
return $blocks;
}
else if ($op == 'view') {
$data['subject'] = check_plain($menus[$delta]);
$data['content'] = menu_tree($delta);
return $data;
}
}
If you only want to override how individual item (links) are rendered then you can use the theme system (there are loads of references on how do this) - but if you want complete control on how the entire menu tree is rendered (for example, wrapping the output in nested DIVs so it can be centred on the page) then there is no way to override menu_block().
Therefore, I removed the menu I wanted to render differently from the administer blocks page (site building->blocks) and rendered the menu directly in my page.tpl.php using code something like this: (angle brackets removed)
$m = menu_tree_page_data('my-menu-id');
$o = "DIV";
foreach($m as $k => $v){
$o .= "SPAN {$v['link']['title']} /SPAN";
}
$o .= "/DIV";
echo $o;
I hope this helps.
I've had mixed success doing template.php menu overrides to force CSS classes and ids or HTML into the output.
You could make use of Block Theme when enabling the menu as a block, but I've never tried it.
http://drupal.org/project/blocktheme
If you want to tackle the template way, here are the zen menu override funcitons...
function zen_menu_item_link($link) {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
// If an item is a LOCAL TASK, render it as a tab
if ($link['type'] & MENU_IS_LOCAL_TASK) {
$link['title'] = '<span class="tab">' . check_plain($link['title']) . '</span>';
$link['localized_options']['html'] = TRUE;
}
return l($link['title'], $link['href'], $link['localized_options']);
}
function zen_menu_local_tasks() {
$output = '';
if ($primary = menu_primary_local_tasks()) {
$output .= '<ul class="tabs primary clear-block">' . $primary . '</ul>';
}
if ($secondary = menu_secondary_local_tasks()) {
$output .= '<ul class="tabs secondary clear-block">' . $secondary . '</ul>';
}
return $output;
}
You could use sections module, or look at how it switches theme for certain menu-items.
what I did was register a new theme function in my template.php called primary_links (because I wanted to only customize this menu in certain way) created the function mytheme_primary_links() in my template.php refreshed the cache so Drupal would add my theme function to the system then changed theme function on primary_links from links to my custom theme function primary_links - this allows me to customize only this 1 menu - could you do this and hook into where ever to change the theme function being called for your links?
Chris

Resources