Drupal Change Menu url - php

With Drupal 7, I have a content type with multiple fields. I then have a view page that takes this content type and displays all the content on it.
So think of it like a blog.. and then a main blog display page.
I have it set so that a menu item is automatically created in the proper place.
I also have Pathauto set up so that it creates a link
www.site.com/blog_anchor_node-title
The individual content page will never be accessed, so I'm not worried about the strange url, however, since hashtags are not supported by pathauto, I used anchor
I need every instance of anchor to be replaced with a # via the template.php file.
This will allow anchor tags to automatically be added to my main menu, the footer, as well as jump menus on the "blog" page it's self.
so far, the closest thing I have is:
function bartik_theme_links($variables) {
$links = $variables['links'];
if (!(strpos($links, "_anchor_") === false)) {
$links = str_replace("http://", '', $links);
$links = str_replace("_anchor_","#",$links);
} }
This doesn't work.

First, your theme_links implementation should not include theme in its function name. And second to quote the documentation page linked before, `$variables['links'] is …
An associative array of links to be themed. The key for each link is used as its CSS class. Each link should be itself an array, with the following elements
Your replacement does not work because you're using strpos on an array.
To make this work go to the API documentation page, copy the code (yes the hole code) and just insert something like the following at the beginning:
function bartik_links($variables) {
$links = $variables['links'];
foreach($links as $key => $l) {
// do your replacements here.
// You may want to print out $l here to make sure
// what you need to replace.
}
//...
}
Also make sure the function is named properly.

To allow me to use the # symbol in a URL, what worked for me was adding the following to my template.php file (before the function above you want to call). You don't have to change anything else besides YOURTHEMENAME to your theme's name:
function YOURTHEMENAME_url_outbound_alter(&$path, &$options, $original_path) {
$alias = drupal_get_path_alias($original_path);
$url = parse_url($alias);
if (isset($url['fragment'])){
//set path without the fragment
$path = $url['path'];
//prevent URL from re-aliasing
$options['alias'] = TRUE;
//set fragment
$options['fragment'] = $url['fragment'];
}
}

Related

giving custom argument vlue in 'q' variable

**also posted on druapl.stackexchange
https://drupal.stackexchange.com/questions/150500/giving-custom-argument-vlue-in-q-variable
**
apologies if question is ambiguous . the scenario is as follows:
in drupal 7 , we want to use a custom template page when a specific value is given for the q variable in url .
for example if we give http://localhost/drupal/?q=xyz/123 , we want to use a custom template page say page-xyz.tpl.php ..
have a hunch that hooks and template.php file may be the key components here but not sure what to exactly do..
any help appreciated.
you could implement theme_preproccess_page() (or node, or html) to control this in your template.php
function YOURTHEME_preproccess_page(&$vars) {
if (isset($_GET['q']) && $_GET['q'] == 'xyz/123') {
$vars['theme_hook_suggestions'][] = 'page_xyz';
}
}
that should work, but I would like to recommend not use the '?q=xyz' solution, but do an preproccess that should work to all your pages, like this.
function YOURTHEME_preproccess_page(&$vars) {
$title = strreplace(' ','_', $vars['node']->title);
//if you use the transliteration module, instead of strreplace
//use transliteration_get($vars['node']->title);
$vars['theme_hook_suggestions'][] = 'page_'.$title;
}
now that should work for every page that you want to make a custom template. Just add the file and clear the chaches. If you don't have the page template to the specific page, it's ok, drupal will use the default.

Replace string in Array in array with truncate string

I'm using CodeIgniter for my website. I'm also using the tumblr API on my site to show posted news.
Because showing the entire text is a bit too much, I want to truncate the body copy to 150 characters, I do this by using the character_limiter function of CI.
The code is as followed in my 'home' controller:
public function index() {
//Title for home page
$data['title'] = "Home - Welcome";
// Obtain an array of posts from the specified blog
// See the config file for a list of settings available
$tumblr_posts = $this->tumblr->read_posts();
foreach($tumblr_posts as $tumblr_post) {
$tumblr_post['body'] = character_limiter($tumblr_post['body'], 150);
}
// Output the posts
$data['tumblr_posts'] = $tumblr_posts;
// Load the template from the views directory
$this->layout->view('home', $data);
}
The problem is, that $tumblr_post['body'] isn't shortened when I echo it on my view page. Doing it like above works in Asp.net (C#) but it doesn't seem to work in php, anyone know why and how to solve it or is there an other way?
Your problem is with the foreach loop. You need to add a & before $tumblr_post to pass it by reference. This makes sure you are actually editing the values in the array. Without the &, you're just editing a local variable and not the array.
Try it like this (notice the &):
foreach($tumblr_posts as &$tumblr_post) {
$tumblr_post['body'] = character_limiter($tumblr_post['body'], 150);
}

How to display a Drupal 6.20 menu in WordPress?

Is is possible to display (via php) the main menu of a Drupal 6.20 site in a WordPress theme template file located in a subdirectory on the same domain?
Right now, I'm displaying the menu by copying the static html from the Drupal site and adding it to header.php in the WordPress template in the site located in mydomain.com/blog/. But of course that's not going to work when another menu item is added to the Drupal site, or the Drupal menu is changed in any way.
So is there a Drupal php function that will pull the menu into the WP file?
Failing that, is there a way with php to parse a Drupal page for the html of the menu (yes, this would be ugly) and display it in WP?
The first part of the challenge is to output only the menu, with as little (or none) of the surrounding HTML as possible, so you have as little work to do in parsing the HTML as possible.
The second part is to take that output from Drupal and actually display it on your WordPress site.
You could add the Drupal database as a secondary database in WordPress using the a new instance of the $wpdb object, write the query to get the right content from the tables, and format the results. That could work, but might be overkill.
An alternative workable option may be to use JSON to format the output of the primary links, using the drupal_json function in Drupal, then consume the JSON feed in Wordpress.
I'm assuming:
you have admin access to login to the Drupal site, which you'll need to create nodes, and clear the theme cache
you want to output the Primary Links menu, which 90%+ of Drupal sites use. This is probably true, but it is possible your site uses custom menus. If so, this is still possible, you'd just write slightly different code in step 3.
The steps would be:
Create a Drupal node (you can call it anything, it's just a placeholder)
Get the node id of your dummy page (ie., node/234). From the node id, create a one-off page template in your Drupal site's themes folder. It should be called page-node-xxxx.tpl.php, with xxxx being your node id
Add this code to page-node-xxxx.tpl.php:
<?php
drupal_json(menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links')));
?>
This will create a JSON feed of your menu items.
Clear the theme cache of your Drupal site by visiting http://yoursite.com/admin/build/themes and visit http://yoursite.com/node/xxxx to see the raw JSON feed.
You should now be able to use a jQuery method like $.getJSON or $.ajax in your Wordpress theme to consume and display the JSON feed, or possibly use json_decode and curl to output your array as HTML.
A good thing about Drupal's drupal_json function is that it already sends the correct JSON headers, so now all you have to do is write the jQuery or PHP that does what you need.
I'm assumed you are more of a Wordpress specialist and have a working knowledge of Drupal but maybe not a lot of familiarity with its inner workings. So, sorry if it seemed too basic (or not basic enough :).
The Drupal theming engine is very modular - you may be able to make an appropriate PHP call into Drupal to get just the menu rendered, then emit that HTML as a part of your WordPress page.
g_thom's answer is very good and if you wish to create a very simple module to output the main navigation you can write something like this:
<?php
function getmenus_help($path, $arg) {
// implementing the help hook ... well, not doing anything with it just now actually
}
function getmenus_all() {
$page_content = '';
$page_content = json_encode(menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links')));
// fill $page_content with the menu html
print $page_content;
return NULL;
}
function getmenus_menu() {
$items = array();
$items['getmenus'] = array(
'title' => 'Get Menus',
'page callback' => 'getmenus_all',
'access arguments' => array('access getmenus'),
'type' => MENU_CALLBACK,
);
return $items;
}
// permissions
function getmenus_perm() {
return array('access getmenus');
}
In your PHP code you can then write something like:
function primary_links() {
$primary_links = file_get_contents(SITE_URL . '/getmenus');
$primary_links = json_decode($primary_links);
$primary_links = (array)$primary_links;
$i = 0;
$last = count($primary_links);
$output = '';
foreach ($primary_links as $pm) {
$href = $pm->href;
if (strpos($pm->href, 'http://') === FALSE) {
if ($pm->href == '<front>') {
$href = SITE_URL . '/';
} else {
$href = SITE_URL . '/' . $pm->href;
}
}
$output .= '
<li>
'.$pm->title.'</li>';
$i++;
}
return $output;
}
I hope this helps!
PS: Make sure you update the module's permissions to allow anonymous users to have access to the path you set in your module - otherwise you will get a 403 Permission Denied.

How to include a custom string in a Wordpress permalink?

I am a bit confused about how WordPress's permalink works, especially beyond Wordpress's own usage. My permalinks are like:
%post_id%-%post_name%
But in single.php I want to put another link to page itself but with different query string. When it is clicked the permalink structure may look like:
%mystring%-%post_id%-%post_name%
I want to get the value from $_GET['action'], so:
$_GET['action'] = %mystring%
my plan is to interpret it like:
if('xx' == $_GET['action']){
//do xx stuff
} else if ('yy'==$_GET['action']){
//do yy stuff
} else {
//show the single post as a single.php always shows
}
that means, I want to parse the $_GET['action'] optionally. If I do not parse it even if it is available in query string, I want the page to be rendered correctly.
So to get this done, where should I actually work? Also how do I form the link for <a> tag? Usually we make link this way:
TEXT
but you already know, I need to add some text before the original permalink of post.
Thanks in advance.
Leave your permalink structure as it was and check out my answer on custom rewrite rules.
You could adapt the code like so;
function my_rewrite_rules($rules)
{
global $wp_rewrite;
// the key is a regular expression
// the value maps matches into a query string
$my_rule = array(
'(.+)/(.+)/?$' => 'index.php?pagename=matches[2]&my_action=$matches[1]'
);
return array_merge($my_rule, $rules);
}
add_filter('page_rewrite_rules', 'my_rewrite_rules');
function my_query_vars($vars)
{
// this value should match the rewrite rule query paramter above
// I recommend using something more unique than 'action', as you
// could collide with other plugins or WordPress core
$my_vars = array('my_action');
return array_merge($my_vars, $vars);
}
add_filter('query_vars', 'my_query_vars');
Now the page my_page should be available at http://example.com/whatever/my_page and http://example.com/my_page.
You can get the value of whatever using get_query_var('my_action').
Disclaimer
This may have undesired effects when viewing children pages or page attachments. You could get around this by passing an identifier in your rewrite, something to the effect of;
http://example.com/my_identifier/whatever/page
Note: You will need to edit the rewrite rule if you wish to do this. Every time you make changes to the code you will need to re-save your permalink structure to 'flush' the rules.

Setting A Header Image for a Wordpress Page and all its children

Wordpress. I've got a handful of pages, each with children -- varying depths.
Let's say it's like this:
+about
-- page1
-- page2
+stuff
-- page1
--- sub-sub-page
-- page2
My problem:
I want to set a graphic header for all the "about" pages, including all its children... and a different graphic header for all "stuff" pages and all its children.
I could do this using a custom field for each individual page, but I don't trust my users to remember to set this field.
I thought about adding to the page.php template and telling the page to find its parent top-level page to determine which "family" it's in, then set the header graphic that way...but I don't want to invest a ton of time (and run-time) resources traversing up the hierarchy.
Any ideas?
Much thanks in advance...
UPDATE:
Taking the code from below, I modified it to use outside of the Wordpress Loop (have not yet integrated the "default" value yet).
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$header_swf = get_post_meta($postid, 'header_swf', true);
if (!$header_swf) {
$parent = get_page($wp_query->post->post_parent); //array of values for this page's parent, directly above it
while (!$header_swf && $parent) {
$header_swf = get_post_meta($parent->ID, 'header_swf', true);
$parent = get_page($parent->post_parent); //new parent
}
}
?>
Note: This answer assumes (perhaps incorrectly) there is no built-in method to do this.
You could write something in the template that looked for a predefined custom field. If none exists, it could ask the parent page for its version of the custom field, and so on.
$val = get_post_custom_values('header_image');
if (!val) {
$parent = get_page($post->post_parent);
while (!$val && $parent) {
$val = get_post_custom_values('header_image', $parent->id);
$parent = get_page($parent->post_parent);
}
if (!$parent && !val) // has failed to find a header image!
$val = 'my default value';
}
Or something like that.....
I should point out that might be really database heavy so it might be worth caching the value for each given URL. I'd personally recommend memcached but there are other methods.
I'd say test for the current page's family. I wouldn't think it'd be too taxing, as it's just a simple short-circuit.
Here's a previous SO thread on how you'd accomplish this.

Categories