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);
}
Related
I'm completely new and not a programmer, I'm just experimenting with the functions.php file (in a child theme) to see if I can get a function to work.
My goal: I am using a multisite wordpress setup for a website on models. When admin creates a profile for each model, their name is used on SITE 1 for their unique url AND SITE 2 to say 'welcome model name'
SITE 1: public site for everyone to see
SITE 2: private models only site to view and update personal details
On SITE 1, I have a complete list of all the models on a page for the public to browse through. Each model has their own unique profile URL that contains their first and last name. E.g. http://www.domain.com/models/samantha-rebecca
On SITE 2, when the models log in to the site, I need the homepage to show 2 name uses. One is their display name using a shortcode so I can say 'Welcome Samantha Rebecca' (which I have got working using this code).
/* username in page content using [current_user] shortcode */
function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->display_name;
$output = ob_get_clean();
return $output;
}
add_shortcode('current_user', 'custom_shortcode_func');
The other is for a button on the same private page they can press to go straight to their 'public' profile page. This is what I need help with please.
In an ideal world, I would like to take their first and last name (all profiles are admin created only) and put a dash in between so I can use http://www.domain.com/models/[url_user] for which I did try and use the following:
/* username in url using [url_user] shortcode */
function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo $current_user->user_firstname . '-' . $current_user->user_lastname;
$output = ob_get_clean();
return $output;
}
add_shortcode('url_user', 'custom_shortcode_func');
But I can't seem to get them to both work at the same time in the functions file without an error coming up such as this
Fatal error: Cannot redeclare custom_shortcode_func() (previously declared in /home/domain/public_html/wp-content/themes/child-theme/functions.php:47) in /home/domain/public_html/wp-content/themes/child-theme/functions.php on line 65
Please can someone help me get this to work as one efficient function. Thank you.
i got your issue is in both short code you have used same function name please change your second function name like below code.
/* username in url using [url_user] shortcode */
function custom_shortcode_func_url_username() {
ob_start();
$current_user = wp_get_current_user();
$output = $current_user->user_firstname . '-' . $current_user->user_lastname;
return $output;
}
add_shortcode('url_user', 'custom_shortcode_func_url_username');
change your second function name like above code your problem will solved. you are getting error because for both shortcode your function name is same. function name should be unique. same name multiple time is not allowed.
more about used define function name rules. More about used define functions
**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.
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'];
}
}
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.
I have a website where drupal manage the contents, but another application handle the e-commerce (my customer doesnt like to change his own e-commerce)
I dont like to have the e-commerce to looks differently from the rest of the websites, so i've made a drupal Page node with php code in the body, that simply include the external app and initialize it.
It works well, but the problem is the other link the e-commerce generate:
http://example.com/shop #The Page node i've created, this work
http://example.com/shop/catalog/fruit/ #here comes the trouble!
The external app handle the url by its own, so what i need is to tell drupal to redirect all the url that begin with shop to his shop Page node... something like
http://example.com/shop/* => load http://example.com/shop
What is the best practices to do that?
If you create a module rather than a node this will be quite easy.
use hook_menu() to match the URL string
function example_menu() {
$menu = array()
$menu['shop'] = array(
'page callback' = 'example_callback';
)
}
function example_callback() {
// use arg() to get arguments.
return shop_php();
}
Creating a callback with hook menu allows you to call your own code, the value returned by the callback will be displayed in the page. When drupal sees a URL which matches shop* it will call the function example_callback. In this function you can put the code you currently have in your page node. And return the content you wish to display in the page.
After googlin around, i found the Drupal custom_url_rewrite_inbound that does exactly what i need.
I inserted the function in my /sites/default/settings.php:
function custom_url_rewrite_inbound(&$result, $path, $path_language) {
if(preg_match("/^shop(\/)/", $path, $matches)) {
$result = 'node/XX'; //XX is the ID of my Page Node with the ecommerce code.
}
}
It works like a charm!