Drupal PHP block visibility rules - drupal

In a Drupal block's Page Visibility Settings I'd like to prevent a certain block from showing if the second value in the path is a number. This does not seem to be working for me. Cheers.
Show block ONLY when arguments are:
domain.com/video/one (arg 0 is 'video' and arg 1 is present and NOT a number)
Don't show:
domain.com/video
domain.com/video/1
<?php
if (arg(0) == 'video' && is_nan(arg(1)) && empty(arg(2))) {
return TRUE;
}
else {
return FALSE;
}
?>

I'm assuming this is in a hook_block/hook_block_view function? You could try a different approach:
if (preg_match('/^video\/[0-9]+$/', $_GET['q'])) {
// Path has matched, don't show the block. Are you sure you should be returning TRUE here?
return TRUE;
}
else {
// Path has matched, go ahead and show the block
return FALSE;
}

You can simply use the following code:
<?php
$arg1 = arg(1);
$arg2 = arg(2);
// Check arg(1) is not empty, or is_numeric() returns TRUE for NULL.
return (arg(0) == 'video' && !empty($arg1) && !is_numeric($arg1) && empty($arg2));
?>
As KingCrunch already said, is_nan() doesn't return TRUE when its argument is a number.
The code you reported contains another error too: empty() can be used only with variables, as reported in the PHP documentation.
empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
The code I reported shows the block for paths like "video/video1"; if you want to show the block also for paths such as "video/video1/edit", then the following code should be used.
<?php
$arg1 = arg(1);
return (arg(0) == 'video' && !empty($arg1) && !is_numeric($arg1));
?>
Using arg() doesn't work if the path you are looking for is a path alias. Suppose that "video/video1" is a path alias for "node/10"; in that case arg(0) will return "node," and arg(1) will return "10." The same is true for $_GET['q'] that will be equal to "node/10."
This happens because Drupal, during its bootstrap, initialize $_GET['q'] with the following code:
// Drupal 6.
if (!empty($_GET['q'])) {
$_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
}
else {
$_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
}
// Drupal 7.
if (!empty($_GET['q'])) {
$_GET['q'] = drupal_get_normal_path($_GET['q']);
}
else {
$_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
}
If you what you are checking is a path alias, then you should use the following code:
// Drupal 6.
$arg = explode('/', drupal_get_path_alias($_GET['q']);
return (arg[0] == 'video' && !empty($arg[1]) && !is_numeric(arg[1]) && empty($arg[2]));
// Drupal 7.
$arg = explode('/', drupal_get_path_alias();
return (arg[0] == 'video' && !empty($arg[1]) && !is_numeric(arg[1]) && empty($arg[2]));

Dont know, what your arguments looks like, but I assume you mixed up two kinds of types. is_nan() only works with numbers. If you want to test, if a value is a number,
var_dump(is_numeric(arg(1));
is_nan() tests, if a "numeric" value is a concrete value or "not a number" like "infinite" or the result of "0/0" or such.

Related

$_GET is empty after defining a rewrite rule

Hello Wordpress ninjas,
I am new to wordpress develpoment. I define a rewrite rule (with a tag), similar to what you can find in examples here and here.
$wp_rewrite->add_rewrite_tag('%filterx_location%', '([^&/]+)', 'filterx_location=');
$wp_rewrite->add_rule('property-location/(.+)$','index.php?post_type=property&filterx_location=$matches[1]', 'top');
Now, the problem is, that when the query is completed (the page is shown), the filterx_location paramater is not set. In fact, var_dump($_GET) gives me an array(0) { }.
I have a black out or something, seems like something simple I miss here, just cannot figure it out :-/
Any help is much appreciated!
UPDATE
what is even stranger is that when I generate rewrite rules with:
$wp_rewrite->add_rewrite_tag('%filterx_location%', '([^/]+)', 'filterx_location=');
$permalink_prefix = 'property-location';
$permalink_structure = '%filterx_location%/';
$rewrite_rules = $wp_rewrite->generate_rewrite_rules($permalink_prefix.'/'.$permalink_structure, EP_ALL, true, true, true, true, true);
I see a bunch of generated urls matches and redirects, if I print them. One of them is:
property-location/([^/]+)/page/?([0-9]{1,})/?$ => index.php?filterx_location=$matches[1]&paged=$matches[2]&post_type=property
I add all the generated urls with:
foreach($rewrite_rules as $regex => $redirect) {
if(strpos($redirect, 'attachment=') === false) {
//add the post_type to the rewrite rule
$redirect .= '&post_type=property';
}
//turn all of the $1, $2,... variables in the matching regex into $matches[] form
if(0 < preg_match_all('#\$([0-9])#', $redirect, $matches)) {
for($i = 0; $i < count($matches[0]); $i++) {
$redirect = str_replace($matches[0][$i], '$matches['.$matches[1][$i].']', $redirect);
}
}
//add the rewrite rule to wp_rewrite
$wp_rewrite->add_rule($regex, $redirect, 'top');
}
And If I go to the URL /property-location/madrid/page/2/ then the query_vars correctly do have ["paged"]=> int(2). But the filterx_location is totally ignored!
Ok, I got it working, in a strange turn of events. First I replaced
$wp_rewrite->add_rewrite_tag('%filterx_location%', '([^/]+)', 'filterx_location=');
with:
add_rewrite_tag('%filterx_location%', '([^/]+)', 'filterx_location=');
I noticed those are two different things. I took a look into the wordpress code of add_rewrite_tag:
function add_rewrite_tag($tagname, $regex) {
//validation
if ( strlen($tagname) < 3 || $tagname[0] != '%' || $tagname[strlen($tagname)-1] != '%' )
return;
$qv = trim($tagname, '%');
global $wp_rewrite, $wp;
$wp->add_query_var($qv);
$wp_rewrite->add_rewrite_tag($tagname, $regex, $qv . '=');
}
I thought the "add_query_var" part might be essential.
But it still did not work!
So what I did finally was add another call to add_query_var() after the add_rewrite_tag() call:
add_rewrite_tag('%filterx_location%', '([^/]+)', 'filterx_location=');
$wp->add_query_var('filterx_location');
and only then everything started to work. Initially I assumed that $_GET should be holding my variable, but then even calls to get_query_var('filterx_location') were empty. Now it get the right stuff.

Wordpress - Advanced Category Excluder - Undefined Variable Error

Trying to use the Advanced Category Excluder plugin and everything works great except on my 404 page I'm getting all of these "Undefined variable" errors on line 446
Try it here:
http://bolivares.com/test
Line 446 reads return $filter;
In full context:
function ace_get_section()
{
global $wp_query, $ace_targets;
if (is_array($ace_targets))
{
foreach ($ace_targets as $key=>$val)
{
if (!empty($wp_query->$key) && $wp_query->$key == 1) $filter = $key;
}
}
return $filter;
}
Plugin Page
http://wordpress.org/extend/plugins/advanced-category-excluder/
Anyone know how this can be resolved?
That $filter variable is defined only when some conditions are met but when conditions are not met the variable is undefined as the notice says.
So try to add $filter=""; in the beginning of the function like this:
function ace_get_section()
{
global $wp_query, $ace_targets;
$filter="";
if (is_array($ace_targets))
{
foreach ($ace_targets as $key=>$val)
{
if (!empty($wp_query->$key) && $wp_query->$key == 1) $filter = $key;
}
}
return $filter;
}

Navigating through Wordpress sibling pages using functions.php

I'm trying to find a way to get through sibling pages on my wordpress hosted site. After a bit of searching I found this to put into my functions.php:
function siblings($link) {
global $post;
$siblings = get_pages('child_of='.$post->post_parent.'&parent='.$post->post_parent);
foreach ($siblings as $key=>$sibling){
if ($post->ID == $sibling->ID){
$ID = $key;
}
}
$closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID));
if ($link == 'before' || $link == 'after') { echo $closest[$link]; } else { return $closest; } }
It's almost perfect for what I need. The only problems are:
It sorts them alphabetically and I need them sorted by the Order Number
I need it to loop (so the last pages links to the first) and not just end
It automatically says "Previous of Next" and I would like to get rid of that "of"
If anyone has input on any of the three issues, I would greatly appreciate it. I'm doing some tinkering myself but I figured that most of you are probably a lot better at this than I am. That, and I've gotten "500 Server Errors" one too many times.
Thanks
Here is your modified piece of code. It solves your first two requests. The last one is not part of provided code. This code does not generate either of mentined words. It only echoes or returns permalinks.
function siblings($link) {
global $post;
$siblings = get_pages('child_of='.$post->post_parent.'&parent='.$post->post_parent.'&sort_column=menu_order');
foreach ($siblings as $key=>$sibling){
if ($post->ID == $sibling->ID){
$ID = $key;
}
}
if( $ID == 0 ){
$closest = array('before'=>get_permalink($siblings[count($siblings)-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID));
}elseif( $ID == count($siblings)-1 ){
$closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[0]->ID));
}else{
$closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID));
}
if ($link == 'before' || $link == 'after') { echo $closest[$link]; } else { return $closest; } }
EDIT: Added &sort_column=menu_order to get_pages arguments to solve first request

Drupal Page Template based on url alias

I want to create an drupal page Template depending on the url alias.
Her my current situation:
I create a page named test, the url alias is test, too.
The page template, based on this docu - http://drupal.org/node/1089656 is: page--test.tpl.php.
I cleaned the drupal them cache, but there is still the default page template shown for this page.
What could be the error?
page--test.tpl.php doesn't work because Drupal is using the real path of page--node--#.tpl.php. To get Drupal to recognize aliased paths, you have to add the aliased path as part of the theme suggestions like so:
function MYMODULE_preprocess_page(&$vars, $hook) {
// only do this for page-type nodes and only if Path module exists
if (module_exists('path') && isset($vars['node']) && $vars['node']->type == 'page') {
// look up the alias from the url_alias table
$source = 'node/' .$vars['node']->nid;
$alias = db_query("SELECT alias FROM {url_alias} WHERE source = '$source'")->fetchField();
if ($alias != '') {
// build a suggestion for every possibility
$parts = explode('/', $alias);
$suggestion = '';
foreach ($parts as $part) {
if ($suggestion == '') {
// first suggestion gets prefaced with 'page--'
$suggestion .= "page--$part";
} else {
// subsequent suggestions get appended
$suggestion .= "__$part";
}
// add the suggestion to the array
$vars['theme_hook_suggestions'][] = $suggestion;
}
}
}
}
Source: http://groups.drupal.org/node/130944#comment-425189

drupal module, check if node type

As a more specific take on this question:
drupal jQuery 1.4 on specific pages
How do I check, inside a module, whether or not a node is a certain type to be able to do certain things to the node.
Thanks
The context:
I'm trying to adapt this code so that rather than working on 'my_page' it works on a node type.
function MYMODULE_preprocess_page(&$variables, $arg = 'my_page', $delta=0) {
// I needed a one hit wonder. Can be altered to use function arguments
// to increase it's flexibility.
if(arg($delta) == $arg) {
$scripts = drupal_add_js();
$css = drupal_add_css();
// Only do this for pages that have JavaScript on them.
if (!empty($variables['scripts'])) {
$path = drupal_get_path('module', 'admin_menu');
unset($scripts['module'][$path . '/admin_menu.js']);
$variables['scripts'] = drupal_get_js('header', $scripts);
}
// Similar process for CSS but there are 2 Css realted variables.
// $variables['css'] and $variables['styles'] are both used.
if (!empty($variables['css'])) {
$path = drupal_get_path('module', 'admin_menu');
unset($css['all']['module'][$path . '/admin_menu.css']);
unset($css['all']['module'][$path . '/admin_menu.color.css']);
$variables['styles'] = drupal_get_css($css);
}
}
}
Thanks.
Inside of a module, you can do this:
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'edit') {
if (!($node)) {
$node = node_load(arg(1));
}
if ($node->type == 'page') {
// some code here
}
}
That will load a node object given the current node page (if not available). Since I don't know the context of code you are working with, this is kind of a rough example, but you can always see properties of a node by doing node_load(node_id). But, depending on the Drupal API function, it may already be loaded for you.
For example, hook_nodeapi.
http://api.drupal.org/api/function/hook_nodeapi
You could do:
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
// some code here
}
}
Try this:-
function MyModule_preprocess_node(&$vars) {
if ($vars['type'] == 'this_type') {
// do some stuff
}
}
Try this:-
$node = node_load(arg(1));
$node =$node->type;
if($node == 'node_type'){
//do something
}

Resources