Drupal 7 theme_hook_suggestions not working - drupal

How do I add 2 for one view display override?
This is the code I have that works:
function yourthemename_preprocess_html(&$vars) {
if (arg(0) == 'qrule') {
$vars['theme_hook_suggestions'][] = 'html__qrule';
}
}
HTML template page called: html--qrule.tpl.php
This works fine! (thanks #Ionut.A)
But I also want to override the page.tpl.php too with page--qrule.tpl.php but when I add this:
function mythemename_preprocess_html(&$vars) {
if (arg(0) == 'qrule') {
$vars['theme_hook_suggestions'][] = 'html__qrule';
$vars['theme_hook_suggestions'][] = 'page__qrule';
}
}
PAGE template page called: page--qrule.tpl.php
I get this error:
Fatal error: Only variables can be passed by reference in /var/www/vhosts/xxx/public_html/sites/all/themes/themename/page--qrule.tpl.php on line 1
Can anyone see what i'm doing wrong here?
Thanks
C

If you're adding a theme hook suggestion for the page.tpl.php file you'll need to to it in hook_preprocess_page():
function mythemename_preprocess_page(&$vars) {
$vars['theme_hook_suggestions'][] = 'page__qrule';
}

Related

TinyMCE - Custom plugin makes visual editor blank in wordpress

I am trying to add TinyMCE custom plugin in wordpress that change the direction of selected text by using <bdo>. I register the plugin in wordpress:
/*Register Custom TinyMCE plugin*/
add_filter('mce_external_plugins', 'my_tinymce_plugins');
function my_tinymce_plugins() {
$plugins_array = array(
'tiny' => 'tiny.js' //Plugin directory is same as theme's funtion.php
);
return $plugins_array;
}
But it hide visual editor completely & also make text editor, un-editable. What is wrong?
I think you're overwriting all the other plugins, rather than just adding yours. Try
function my_tinymce_plugins($plugin_array) {
$plugin_array['tiny'] = 'tiny.js';
return $plugin_array;
}
instead. You may need to prepend get_stylesheet_directory_uri() to tiny.js to ensure the URL is correct.
Edit
Further to your comment, here's some code I used a few years ago to add a button. I can't say for certain best practices haven't changed, but it worked for me:
add_action('init', 'immo_add_column_button');
function immo_add_column_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
return;
}
if ( get_user_option('rich_editing') == 'true') {
add_filter('mce_external_plugins', 'immo_add_column_tinymce_plugin');
add_filter('mce_buttons', 'immo_register_addcolumn_button');
}
}
function immo_register_addcolumn_button($buttons) {
array_push($buttons, "|", "addcol");
return $buttons;
}
function immo_add_column_tinymce_plugin($plugin_array) {
$plugin_array['addcol'] = get_bloginfo('stylesheet_directory').'/js/immo_column_button.js';
return $plugin_array;
}
add_filter( 'tiny_mce_version', 'immo_refresh_mce');
function immo_refresh_mce($ver) {
// Force refresh of TinyMCE cache by updating the version number
$ver += 3;
return $ver;
}

Drupal 7 Bootstrap page.vars class by content type

I'm trying to modify the page.vars in my bootstrap subtheme to change a class based on content type.
Code as standard in page.vars
elseif (!empty($variables['page']['sidebar_first']) || !empty($variables['page']['sidebar_second'])) {
$variables['content_column_class'] = ' class="col-sm-9"';
}
My modified code:
elseif (!empty($variables['page']['sidebar_first']) || !empty($variables['page']['sidebar_second'])) {
$node = $variables['node'];
if ($node->type == 'standard') {
$variables['content_column_class'] = ' class="col-sm-6"';
}
}
What I'm trying to achieve is putting the class col-sm-6 onto every standard content type page.
It looks like the code you're sharing is correct, but you need to tell your sub-theme to read the page.vars.php file. I just dealt with a similar problem and came up with this solution based on information found here.
What you need to do is add a theme function in your sub-theme's template.php something like:
function SUB_THEME_theme(&$existing, $type, $theme, $path) {
bootstrap_include($theme, 'theme/system/page.vars.php');
$hooks = array();
return $hooks;
}
Then you can add theme/system/page.vars.php in your sub-theme and any functions in the page.vars.php file will be included in the registry and applied after the bootstrap base theme. Once you've done that your above code should work as it is.

Add class to an image field for a specific content type in Drupal 7

Currently I'm using the code as suggested in this answer. Which is the following:
function simalr_preprocess_image(&$variables) {
if ($variables['style_name'] == 'request-background') {
$variables['attributes']['class'][] = 'pixastic';
$variables['attributes']['class'][] = 'pixastic-blurfast(amount=1)';
}
}
This works fine except for the fact that I get the following error message on a page which doesn't have an Image with the 'request-background' style:
Notice: Undefined index: style_name in simalr_preprocess_image() (line 46 of /var/www/vhosts/simalr.com/httpdocs/sites/all/themes/simalr/template.php).
I only want this piece of code used on a specific content type (namely 'request'). In which way do I have to adjust the code in my template.php file in order to just use it on a page which is only of a certain content type?
You can still work with your code but use isset function. This will remove the warning.
If you want to do it just for a specific content type use menu_get_object function in drupal. This function will return the node for you if it's a node page.
Example:
$node = menu_get_object();
if ($node->type == 'story') {
// TODO
}
Hope this helps.

View Template Override that is full page

I need to create a template for a view i've created (which I know how to do) but I don't want the page to be wrapped in the drupal html.php code. I want the resulting page to be only what is in the template file I create.
How do I do this?
I did found this which does what I need for content types lab
function yourthemename_preprocess_html(&$vars) {
if ($node = menu_get_object()) {
if($node->type == "lab") {
$vars['theme_hook_suggestions'][] = 'html__lab';
}
}
}
Then created a file called: html--lab.tpl.php
Can this method be used for my view?
does it have to be if($node->type == "lab")?
can I use something like if($view == "viewname")
Thanks
C
I think when you says "created a view" you mean a view + a page display.
So, when you browse to http://mystite.com/mypage, it will show the content of view.
In this case, you can use your code with some small modifications.
function yourthemename_preprocess_html(&$vars) {
if (arg(0) == 'mypage') {
$vars['theme_hook_suggestions'][] = 'html__mypage';
}
}
You will have to create the right .tpl.php file and clear the cache.

WordPress redirect after successful form validation and submit

I have a custom page template with a form in page-report.php.
I do validation on it, so I need the action to lead to the same form, but I need to redirect to a different page on successful validation.
wp_redirect() is not working, because is spitting out the header() function after the output was started already.
if($_POST['report'])
{
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
I cannot use ob_start() and ob_flush() because the header is not included in this page template.
I tried to put a function in functions.php :
add_action('get_header','redirect_to');
function redirect_to($page){
if($page)
{
wp_redirect('http://www.google.com');
}
}
But that works only if I don't have the conditional if().
If I use it, the wp_redirect() is being spat out after the output was started.
What is my best approach to do this?
Thanks.
I think you have to use the save_post hook:
do_action('save_post', 'custom_add_save');
function custom_add_save($postID){
// called after a post or page is saved
if($_POST['report']) {
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
Also you could just try using a plugin instead of your own code...Gravity Forms and Contact form 7 both work well.
}
I got it...
Since I was doing everything from inside an admin page, the header was fired up before the wp_redirect() as it was explained in the question.
So I ended up making a new function at the top:
add_action('admin_init','redirect_to');
function redirect_to()
{
if ( isset($_REQUEST['action']) && 'adduser' == $_REQUEST['action'] ) {
wp_redirect($redirect);
die();
}
}
}
That is making sure that the redirect_to() function will be fired up before the header (on admin_init). Maybe not the most elegant solution, but it works perfect.
So in the case anybody is looking for "Redirect after post" in wordpress, this is how you do it:
wp_redirect($_SERVER['REQUEST_URI']);
Try this
if( $_POST['report'] ) {
if( $validator->ValidateForm() ) {
header( 'Location: http://thankyou' ) ;
}
}

Resources