WordPress: Can you programmatically create page templates? - wordpress

I am currently building a React/Redux theme for WordPress using the WordPress API. I need to add Page Templates to my theme. I can do this by creating almost empty files like:
<?php
/* Template Name: My Template */
?>
But I would like to create these 'Page Templates' programmatically.
The functionality I required is to be able to select a 'Page Template' inside the WordPress CMS and have this come down on the API. This functions as expected if the 'Page Templates' are created as above.
Is this possible?

This can be achieved using the theme_page_templates filter.
CONST CUSTOM_PAGE_TEMPLATES = array(
array('slug' => 'home', 'label' => 'Home'),
array('slug' => 'content-page', 'label' => 'Content Page'),
array('slug' => 'ordering', 'label' => 'Ordering'),
array('slug' => 'reviews', 'label' => 'Reviews')
);
/**
* Add file-less page templates to the page template dropdown
*/
add_filter('theme_page_templates', function($page_templates, $wp_theme, $post) {
foreach(CUSTOM_PAGE_TEMPLATES as $template) {
// Append if it doesn't already exists
if (!isset($page_templates[$template['slug']])) {
$page_templates[$template['slug']] = $template['label'];
}
}
return $page_templates;
}, PHP_INT_MAX, 3);

Related

Drupal login form customize

Hi i have used this code
<?php
$elements = drupal_get_form("user_login");
$form = drupal_render($elements);
echo $form;
?>
to get the default Drupal login form for my site but I need to customize the HTML, I have found some pages in module/users but did not understand how to customize the structure.
The user login form for Drupal is built by the user_login function in user.module using Drupal Form API. If you need to customize it, you should do it using hook_form_alter() in your module
function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id=='user_login') {
// YOUR CUSTOM CODE FOR THE FORM GOES HERE
}
}
** EDIT, AFTER YOUR COMMENT **
You don't need to call the YOUR_MODULE_NAME_form_alter() function: Drupal does that for you via the hook mechanism everytime it needs to build a form, and, when $form_id=='user_login', it modifies the login form to allow your customization. The way Drupal does that is discussed in detail in drupal.org, just follow the link I wrote at the beginning of this answer.
The user login form is declared this way in user.module:
// Display login form:
$form['name'] = array('#type' => 'textfield',
'#title' => t('Username'),
'#size' => 60,
'#maxlength' => USERNAME_MAX_LENGTH,
'#required' => TRUE,
);
$form['name']['#description'] = t('Enter your #s username.', array('#s' => variable_get('site_name', 'Drupal')));
$form['pass'] = array('#type' => 'password',
'#title' => t('Password'),
'#description' => t('Enter the password that accompanies your username.'),
'#required' => TRUE,
);
$form['#validate'] = user_login_default_validators();
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));
The $form array is passed by reference to your hook_form_alter() before being rendered, allowing for customization. So, let's say that you want to change the label of the textfield for the user name from "Username" to "Name of the User", you write
$form['name']['#title'] = t("Name of the User");
in your custom code. If you want to add another field to the form (a textarea, for example), you do
$form['otherfield'] = array(
'#title' => t('My new custom textarea'),
'#type' => 'textarea',
'#description' => t("A description of what this area is for"),
'#cols' => 10,
'#rows' => 3,
'#weight' => 20,
);
and Drupal will add the field to the user login form.
There are many different kind of fields and properties that you can customize this way: I encourage you to fully read the Form API documentation. This way you let Drupal take care of form generation, translation, rendering, validation and submission, also permitting to other modules to manipulate your form if needed.
I hope it's clear, have a good day.
use this in template.php
function themename_theme() {
$items = array();
$items['user_login'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'corporateclean') . '/templates',
'template' => 'user-login',
);
and create a template folder and within that create a file user-login.tpl.php and in this file you can put your html and could customize drupal login

Add menu bar above the WordPress administration bar

I wish to add our own service menu above the WordPress administration bar in the administration area. I do not wish to hack the WordPress system, but I cannot find a hook.
Is there a method?
You can add extra menu items in your administrator menu bar instead of removing/replacing the menu bar.
Below is an example which will insert one menu item with two sub menu items. Just paste the code in your functions.php and log in to your WordPress as admin. If everything goes right then you can see an extra menu in your administrator bar. To accomplish this, WordPress provided the admin_bar_menu hook:
add_action('admin_bar_menu', 'my_custom_menu', 1000);
function my_custom_menu()
{
global $wp_admin_bar;
if(!is_super_admin() || !is_admin_bar_showing()) return;
// Add Parent Menu
$argsParent=array(
'id' => 'myCustomMenu',
'title' => 'Services',
'href' => false
);
$wp_admin_bar->add_menu($argsParent);
// Add Sub Menus
$argsSub1=array(
'parent' => 'myCustomMenu',
'title' => 'Visit Heera IT',
'href' => 'http://heera.it',
'meta' => array('target' => '_blank')
);
$wp_admin_bar->add_menu($argsSub1);
$argsSub2=array(
'parent' => 'myCustomMenu',
'title' => 'Visit StackOverflow',
'href' => 'http://stackoverflow.com/',
'meta' => array('target' => '_blank')
);
$wp_admin_bar->add_menu($argsSub2);
}
For more details, you can visit Codex.
You can also accomplish this using a plugin that allows you to easily customize the content and appearance of the WordPress Admin Bar. Here are a few plugins to consider:
Plugin #1
Plugin #2
Plugin #3
$wp_admin_bar->add_menu(array
(
"parent" => "bba_booking_bank",
"id" => "bba_booking_bank_location",
"title" => $bba_location_providers_wizard_setup,
"href" => admin_url("admin.php?page=booking_bank"),
));
$wp_admin_bar->add_menu(array
(
"parent" => "bba_booking_bank",
"id" => "bba_booking_bank_calendar",
"title" => $bba_booking_bank_calendar,
"href" => admin_url("admin.php?page=bba_booking_calendar"),
)
);

How to display custom html block in Drupal using theme?

Having some module, defined some url in hook_menu() and need to display there some theme (modules/mymodule/templates/mytheme.tpl.php).
How do I show mytheme.tpl.php content on needed url?
function mymodule_menu(){
$item = array();
$item['somemenu'] = array(
'page callback' => 'somemenu_display',
);
return $item;
}
function somemenu_display(){
return WHAT_IS_THIS_FUNCTION('modules/mymodule/templates/mytheme.tpl.php');
}
And it will be good to display only these contents, without and header/footer.
The function is Theme()
return theme('some_theme_function_template', array('aValues' => $someArray));
You then need to use the theme hook like this:
function my_module_name_theme() {
return array(
'some_theme_function_template' => array(
'template' => 'mytheme',
),
);
}
It now searches for mytheme.tpl.php in the root of your module.

Drupal 7 .10 hook_menu implementation error

I am trying to figure out why hook_menu implementation is not working anymore after upgrade from 7.4 to 7.10 for a custom module Menu links were working properly until update to latest version. after update all custom module links are deleted from table menu_links and menu_router.
After many attempts, I also installed a fresh version for D7.10 and created a simple custom module with one item link only (see code below) for testing purpose only. The link is not implemented once the module is enabled. Tables menu_links and menu_routers are not updated.
I have been looking around many possible errors and solution without success.
Really stacked now. What makes me doubt is that I do not see anybody else having the same issue... Any suggestion? Thank you
function misite_menu() {
$items = array();
$items['a/main'] = array(
'title' => 'main',
'page callback' => 'main',
'description' => t('Main front page'),
'access callback' => TRUE,
);
return $items;
}
function misite_theme() {
return array(
'main' => array
(
'template' => 'main',
'variables' => array('title' => NULL),
),
);
}
function main() {
$path = drupal_get_path('module', 'a');
$title = t('');
$build['mainelement'] = array(
'#theme' => 'main',
'#title' => $title,
);
$output = drupal_render($build);
return $output;
}
From the looks of this line:
$path = drupal_get_path('module', 'a');
Your module is called a.
In Drupal, the convention for hook naming is MODULE_NAME_name_of_hook() (see http://api.drupal.org/api/drupal/includes--module.inc/group/hooks/7).
This is true for both hook_menu() and hook_theme() so in your case if the module is called a your functions should be names a_menu() and a_theme().
If you make changes to any hooks make sure you clear Drupal's cache so the relevant registrys are updated.

Drupal 6: how to display node with its local tasks tabs in menu item

In my case each user has a gallery. Gallery is a node. I'd like to hide default "Create content" menu and add custom menu link that links to user gallery.
function custom_menu() {
$items = array();
$items['galleries/editgallery'] = array(
'title' => 'Edit gallery',
'description' => 'edit gallery',
'page callback' => 'custom_edit_gallery',
'access callback' => 'custom_access_editgallery',
);
return $items;
}
function custom_edit_gallery (){
global $user;
$node = node_load ($user->gallerynid);
return node_page_view ($node);
}
But it doesn't show local tasks tabs(like "Edit" tab).
You would need to add them yourself.
With normal theming, you could create a custom template file or overwrite a theme function etc to add the tabs you want.
You could also do this within hook_menu, by using MENU_LOCAL_TASK and MENU_DEFAULT_LOCAL_TASK, see the api.

Resources