Drupal6: Append query string to primary links - php

I'm trying to add a query string "?device=mobile" onto all primary links programmatically.
In my theme's page.tpl.php file I've tried the following,
<?php if (is_array($primary_links)) : ?>
<?php foreach ($primary_links as $link): ?>
$link['href'] = $link['href'].'?device=mobile';
<?php endforeach; ?>
<?php endif; ?>
However this simply prints out the code onto the page. Currently I'm trying to use hook_menu_link_alter, but so far I haven't been successful. To test my code on just one primary link item I've tried the code below:
myModule_menu_link_alter(&$item, $map){
$items['photo_gallery']['href'] = 'photo_gallery?device=mobile';
}
Unfortunately there was no change in the link. I'm also going to investigate hook_menu_item_link() from my template.php file, but at this point I'd like it if someone could point me in the right direction, and let me know what I've done wrong.
Thanks.

The code you're putting in your page.tpl.php has the right idea, but you're missing a couple of things:
The body of the foreach loop should be surrounded by the PHP tags, so that PHP will interpret and execute the code. This is why you see that Drupal just "prints out the code": because you're putting it out of the PHP "world", so it simply becomes part of your template's HTML.
Even if you correctly execute the code, you will not see any changes, because by default, the $link variable in your foreach loops is a copy of the original item in the array, so doing $link['href'] = 'stuff' won't modify the original. To modify the original, you can use the reference syntax, like: foreach ($primary_links as &$link). (Ampersand prefixed to the variable name, see PHP docs on references).
And finally, even if you fix the previous two issues, it might still not work because the link's HREF attribute is probably going to be processed by theme('links') later, and your "?" and "=" are going to get encoded and it will break the link.
So, fixing those three issues, I'd say you should modify your page.tpl.php code to look like:
<?php if (is_array($primary_links)) : ?>
<?php foreach ($primary_links as &$link): ?>
<?php $link['query'] = array('device' => 'mobile'); ?>
<?php endforeach; ?>
<?php endif; ?>
Or, if it annoys you to have to open/close the PHP on every line, just use a normal block like:
<?php
if (is_array($primary_links)) {
foreach ($primary_links as &$link) {
$link['query'] = array('device' => 'mobile');
}
}
?>
Note 1. The &$link syntax (use reference instead of copy), and 2. The query array key of the $link array, which is one of those "special" array keys that Drupal will search for, and, if found, utilize to build a proper URL query to attach to the final link (see the docs for Drupal's url() function).
Also, remember to clear the caches whenever you see that "nothing changes", especially when working on a theme.

Related

Php inside php to process custom field in wordpress

I have a code which returns gallery number 1 with the set of images.
<?php echo photo_gallery(1); ?>
But I need to change number "1" to number "2", "3" and so on with custom fields in every post using this code
<?php the_field('number'); ?>
inside the first code
I need something like this:
<?php echo photo_gallery( <?php the_field('number'); ?> ); ?>
Result:
<?php echo photo_gallery(2); ?>
or
<?php echo photo_gallery(3); ?>
Something like this?
<?php
echo photo_gallery(the_field('images'));
?>
You don't need nested PHP tags. Omit the php tags inside and your code should run.
<?php
echo photo_gallery(get_field('number'));
?>
Note the difference between get_field and the_field. ACF docs explain which to use in which situation.
Essentially, the functions inside (to the right) get executed first, returning a value which can be used by the outer function (to the left).
In the background, a programming language takes these steps:
execute get_field('one') and return 1.
execute photo_gallery(1) and return (photo).
echo (photo)
There are some courses on PHP, many of which are free - that might help you sort through some of these issues.

HTML inside a PHP variable without losing syntax highlight

I have some large HTML code that I want to put in a PHP variable without losing the syntax highlight. I would also like for when it is put in as a variable, it will be shown as a normal string not HTML code.
Is there a way like:
<?php
$var =
?>
HTML GOES HERE
<?php
;
echo $var;
?>
I know my code here doesn't make sense, but it's just to describe the situation.
If the code is very long - say a menu div or something, you can use include. It won't give you a variable to use, but the result is the same.
<?php
include file_with_my_code.php;
?>
In file_with_my_code.php (doesn't have to be .php, .html works just fine as well) you simply paste the code as if you were creating a standalone html file.
Just found it!
$var = <<<HTML
<HTML GOES HERE>
HTML;

PHP delimiters and WordPress (functions.php in specific)

I am modifying the functions.php file of a child theme in a wordpress installation.
At some point I want a function to produce html and then proceed as a normal function.
The "normal" way for me to code this would be like that:
<?php
/**
* Child Theme
* Author: Seb
**/
function html_output() {
# some php code here
?> // closing php delimiter
// some html code here
<?php } # opening php delimiter to close the function
# more php code here
However, when looking at code from other people online, the last two lines are like this:
<?php } ?> // opening php delimiter to close the function and then closing delimiter
<?php # random (?) extra php delimiter I don't get the meaning of
The editor (Coda and Sublime Text 2) doesn't complain about my code but it doesn't work. Can someone maybe explain to me why this has to be like that in order to function properly?
EDIT:
To make my question more clear, I don't understand how
<?php }
is different to
<?php } ?>
<?php
Though I am not sure about your question, but I got two thing in my mind
Coding Style for each developer differs ( while keeping the code
healthy , and as per the standard being used on that specific
framework)
it is a practice, I have often noted that you always have an OPENED
<?PHP tag in code, the reason what I think I read somewhere was that ,
it helps us avoid HEADERS ALREADY SENT ON LINE..... ERROR
Also, this opened <?php tags denoted that you have a php file and if
you want to add any peice of code after this, you have to follow
PHP standards / rules in mind
Okay
for php point of view
<?php } ?>
<?php
and
<?php }
both are okay, and they are as per php rules, but when it comes to WP, where, you get your final html rendered by including 3 or more than files, sometime, it becomes problematic....think the following situation
header.php
<?php }
and now, contents.php
<?php
//and some php code goes here
say, to load the page, both files are combined, so final output would be something like
<?php }
<?php
//and some php code goes here
just trying to explain you the situation with an example, not specific thoug...

str_replace (or another option) for replacing content located inside a php document

I'm attempting to make a template file for a CMS that I'm making where the template file can contain variables like {username} as regular text that get replaced when the page gets included on the index.php page.
Example:
Index Page:
<?php include('templates/123/index.php'); ?>
templates/123/index.php page
<?php include('header.php'); ?>
Welcome {username}
<?php include('footer.php'); ?>
I've tried several methods; however, always run into problems because the page I'm trying to change the content on includes PHP code. Every method I try either 1) messes up because the opening and closing of PHP tags within the document OR 2) just echoes out the PHP code in the document. Is there any way that I can still achieve this? Maybe even with a class of some kind? I just want to be able to achieve this safely.
I will also be using this to where custom variables like {content1} get replaces with a php code that will be ioncubed that retrieves the data from database for content located in column1, same with {column2} {column3} and {column4}. I'm just trying to make the creation of templates extremely easy. (so I'd like to make the code work for that as well)
My preferred method of doing stuff like this involves starting my code with:
ob_start(function($c) {
$replacements = array(
"username"=>"Kolink",
"rank"=>"Awesome"
);
return preg_replace_callback("/{(\w+)}/",function($m) use ($replacements) {
return isset($replacements[$m[1]]) ? $replacements[$m[1]] : $m[0];
},$c);
});
Two steps I suggest
Load the result of your file "templates/123/index.php" into a variable. see this link for how to do it assign output of execution of PHP script to a variable?
use strtr() function to replace your placeholder i.e {username} with actual values
I think this will server your needs.

Drupal: Print field without markup

Is there a way to print field content without getting all the markup? I'm new to Drupal, but I'm aware of the field.tpl.php, however, I'm just wondering if there's a quicker way to get the content in a node--custom.tpl.php. It would compare to Wordpress's <?php echo get_field('field_name'); ?>
Well, apart from using field.tpl.php, I can think of 2 solutions:
first:
Use a php snippet to strip html tags in your template.php.
in your template.php
function mytheme_strip_html_tags($n_field) {
return preg_replace("/<.*?>/", "", $n_field);
}
then call the function mytheme_strip_html_tags($field_name)
if you use several themes, however, you need to copy this snippet to each one of them.
EDIT: You can make a module and place that snippet inside. This way it works with every theme.
second:
Download the tokens module. Tokens are references to your fields. Tokens module have a output mode that strips html for you. [field_name-raw]
You need to follow instructions in how to add tokens, but is not that difficult.
You have access to the $node variable inside a node.tpl.php, so:
<?php print $node->field_monkey_height; ?>
should work... note that many fields will hide their data inside arrays (for multiple value fields, etc) so you may need to do a bit of:
<?php drupal_set_message(print_r($node->field_monkey_height), 1); ?>
...to figure out the exact path to the data you need.
You simply can use PHP's strip_tags() like so:
<?php print strip_tags($node->field_name[LANGUAGE_NONE][0]['value']); ?>

Categories