Skip to content

Instantly share code, notes, and snippets.

@demogar
Created July 31, 2012 03:41
Show Gist options
  • Save demogar/3213316 to your computer and use it in GitHub Desktop.
Save demogar/3213316 to your computer and use it in GitHub Desktop.
Adding support for title, id, class to icl_link_to_element
<?php
// Add this to template-functions.php
// Disclaimer: I based icl_get_anchor_attributes on the _stringify_attributes function on CodeIgniter framework. I made my own function, but I believe this function is just better (and proved to work fine)
function icl_get_anchor_attributes($attributes) {
if (is_object($attributes) && count($attributes) > 0) {
$attributes = (array) $attributes;
}
if (is_array($attributes)) {
$atts = '';
if (count($attributes) === 0) {
return $atts;
}
foreach ($attributes as $key => $val) {
$atts .= ' '.$key.'="'.$val.'"';
}
return rtrim($atts, ',');
}
elseif (is_string($attributes) && strlen($attributes) > 0) {
return ' '.$attributes;
}
return $attributes;
}
// Then modify icl_link_to_element function.
// find: function icl_link_to_element($element_id, $element_type='post', $link_text='',
// $optional_parameters=array(), $anchor='', $echoit = true,
// $return_original_if_missing = true) {
// replace with:
function icl_link_to_element($element_id, $element_type='post', $link_text='',
$optional_parameters=array(), $anchor='', $echoit = true,
$return_original_if_missing = true, $attributes='') {
// find: if (isset($anchor) && $anchor) {
// add after:
if ($attributes !== '') {
$attributes = icl_get_anchor_attributes($attributes);
}
// Then modify
// find: $link = '<a href="' . $url . '">';
// and change for:
$link = '<a href="' . $url . '" ' . $attributes . '>';
// For using, you only need to send last parameter as an arran, ie:
array("title" => "something")
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment