Force frontpage language - ajax

I have got a multilingual joomla 2.5 website with sef urls enabled. The page relies heavily on ajax, so every kind of content is pulled from the server after the good old
window.onload
event. To make everything work proper and urls look nice, I want to force Joomla to always respond in the sites default language if the page loaded and the index.php of the template in invoked.
Or to describe the problem diffrent:
If an ajax-call pulls an item which is different from the default language and a page-refresh follows, joomla redirects to the homepage in the language of the last ajax-call, that is what I want to prevent.
Greetings

Got it working! The question itself was wrong! I should have been asking: "How to redirect to frontpage in default language?"
Answer:
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
$languages = JLanguageHelper::getLanguages('lang_code');
$lang = JFactory::getLanguage();
$defaultLang = ( $lang->getTag() == $lang->getDefault() ) ? $lang : JLanguage::getInstance( $lang->getDefault() );
if( ! ( $lang->getTag() == $defaultLang->getTag() ) ){
$app->redirect( JRoute::_( 'index.php?lang='.$languages[ $defaultLang->getTag() ]->sef ), 'hallo', true );
}
Done!
Greetings....

Related

Joomla! - Load editor-xtd plugin layout from specified file in button's iframe handler

I'm working on a Joomla! 2.5/3.x editor-xtd button and I have a problem loading a layout from file on button click.
I have tried this method:
$link = 'plugins/editors-xtd/myplugin/myplugin.layout.php?name='.$name;
$button = new JObject;
$button->modal = true;
$button->class = 'btn';
$button->link = $link;
$button->text = 'Insert something';
$button->name = 'myplugin';
$button->options = "{handler: 'iframe', size: {x: 500, y: 300}}";
... but the full generated link in admin looks like http://my.local.host/mywebsite/administrator/plugins/editor-xtd/link-etc.. and it doesn't work. I also have tried including JURI::base in my $link, but the administrator path is still loaded.
I'm new in plugin dev with Joomla! and I have search a lot but no solution found.
** I also tried a link like this index.php?folder=plugins.editors-xtd.myplugin&file=myplugin.layout.php&name=$name but still nothing.
Is there a workout for this or I'll have to create&use a javascript function to run on button click?
Solution
Modify link variable like this (if application is admin):
$link = '../plugins/editors-xtd/myplugin/myplugin.layout.php?name='.$name;
... and delete button options (this means that file contents will be loaded via ajax inside modal)
Further more, in myplugin.layout.php we can add a little security check and we can import Joomla! framework library and defines so that we can make use of Joomla! framework in our file (things like language load for eg.)
This is my actual header of file:
<?php
// No direct access
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if( ! IS_AJAX) die;
// Include J!Framework for later use
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..'));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE.DS.'includes'.DS.'defines.php');
require_once ( JPATH_BASE.DS.'includes'.DS.'framework.php');
//more magic goes here...
Unfortunately there is a bit of a gotcha here in that the JED checker process requires that ALL php files start with defined('_JEXEC') or die; on the very first line of code so if you want to share it on extensions.joomla.org then you are stymied...
Back on the OP you can detect whether you are in the Admin or Site before generating the link:
$app = JFactory::getApplication();
// ...
if ($app->isAdmin()) {
$root = '../'; // Joomla expects a relative path, leave site folder "administrator"
} else {
$root = '';
}
$button->link = $root.'/plugins/editors-xtd/myplugin/myplugin.layout.php?name='.$name;
Also, as you may already know the $button->name = 'myplugin'; needs to be the name of the icon from the Joomla icomoon set - you can see them here https://ma.tvtmarine.com/en/blog/112-joomla-icomoon-icons-directory
The name needs to be the icon name without the .icon- bit eg:
$button->name = 'warning-2';
code block doesn't seem to be working properly...sorry about the formatting

How to redirect WORDPRESS hotlinked image ( for exp : Image.jpg) to post/page where it is?

Recently google change image search and as a result most of the traffic click over view original image and it send them to direct image file.
I have changed my .htaccess and stop all hot linking and redirect to my homepage if they click over image file.
Recently I've also tried if it is possible to redirect to post or page where image is.
By the following code
<?php
require('../wp-blog-header.php');
$imageURL = strip_tags( $_GET['id'] );
if imageURL!== "") {
$query = "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment'AND post_parent > '0' AND guid = '$imageURL'";
$linkedImage = $wpdb-get_row($query);
$attachmentUrl = get_attachment_link($linkedImage->ID);
if($attachmentUrl !== "" && !is_numeric(stripos($attachmentUrl, "attachment_id=0"))) {
header("HTTP/1.1 302 Found");
header("Location: ". $attachmentUrl);
exit;
}
}
$newUrl = get_site_url() . "/image-not-found";
header("HTTP/1.0 404 Not Found");
header("Location: " . $newUrl);
exit;
?>
But it redirect to http://www.mydomain.com/?attachment_id=
id is nil or can't fetch the proper id
My website in Wordpress CMS version 3.5.1
can anyone help how to redirect to proper attachment page for .jpg file direct request.
Thanks in advance
Your code would only work if there was an ID parameter on the requested URL that would be an exact match with the image URL, for instance http://www.example.com/?id=http://www.example.com/uploads/Some-Image.jpg
Is this the case? Even so, you shouldn't use id since it's a reserved parameter for other things in WP. In your .htaccess, you should redirect http://www.example.com/uploads/Some-Image.jpg to, for example, http://www.example.com/?image_redirect=http://www.example.com/uploads/Some-Image.jpg. Then use $imageURL = $_GET['image_redirect'].
Other notes: strip_tags doesn't make a string safe to use as a SQL query. In WordPress, you have esc_sql($string) for that. Also, you should rather use if ( $imageUrl != '' ), not with !==. And there's an error on line 5, it should say $wpdb->get_row, you currently have a minus sign. I assume these are typos. And finally, you shouldn't use Location: header with a status code 404.

WordPress Excerpt - Javascript - PHP - Ajax?

I've created this script inside my home.php file for my WordPress theme to calculate the value of a div width in relation with a number (1.408). This operation give me the result (inside $chars variable) to establish number of characters to use in the excerpt for Responsive Design. If the div is larger or thinner, I'll have a different number of characters for my WordPress Excerpt.
This is the code I've published on HOME.PHP. Javascript followed by PHP code for excerpt.
I know javascript variable inside PHP is impossible without Ajax (reading on other forums), but I cannot understand what exactly to do. I am not so good with code. Be clear please and if possible with some examples!
<script type='text/javascript'>
jQuery(document).ready(function() {
var $myDiv = jQuery('#last_post_img_text');
var $results = jQuery('#results');
var $chars = jQuery( $myDiv.outerWidth()/1.408 );
</script>
<?php new_excerpt( $chars ); ?>
I would suggest an approach:
Load a page, check conteiner element width and height, then use $.ajax to load elements content, and send some kind of a variable to decide how many characters to use.
If You would like to do it in WordPress way, read about:
add_action('wp_ajax_$handlename', 'function_to_run');
add_action('wp_ajax_nopriv_$handlename', 'function_to_run');
that will reposnd to Yours ajax request.
wp_enqueue_script( 'theme_js', get_bloginfo('template_url') . '/js/jquery-theme.js', array('jquery'), THEME_VERSION, false );
$protocol = isset( $_SERVER["HTTPS"] ) ? 'https://' : 'http://';
$params = array( 'ajaxurl' => admin_url( 'admin-ajax.php', $protocol ) );
wp_localize_script( 'theme_js', 'theme_js', $params );
this might come handy, if You dont know how to send wp-ajax.php localisation to jQuery $.ajax request.
Reloading the comments with Ajax seems like a bad decision. Why would you want to keep reloading content that doesn't really change. I think your best bet is to use a jQuery plugin.
One that seems to do what you want is http://www.bramstein.com/projects/text-overflow/.
Alternatively you can check out the CSS3 property 'text-overflow' (http://www.w3schools.com/cssref/css3_pr_text-overflow.asp).

Where is Itemid used to flag active menu item in Joomla 2.5.6?

Can anyone please help me with the process flow through which the Joomla Itemid parameter ends up being the highlighted menu item?
I have embedded a 3rd party application in Joomla and by temporarily changing the php environment within the application.
I am able to get joomla html and insert the 3rd party html by replacing a token.
Simplified Code:
if ($_SERVER['REQUEST_METHOD'] == 'GET' ) {
$_SERVER['REQUEST_METHOD'] = '';
}
$_SERVER['REQUEST_URI'] = '/joomla/index.php?view=mycom&option=com_mycom&Itemid=103';
$_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = '/joomla/index.php';
$_SERVER['QUERY_STRING'] = 'view=mycom&option=com_mycom&Itemid=103';
ob_start();
require_once '/joomla/index.php';
$joomlaHTML = ob_get_clean();
echo str_replace($replacementToken, $thirdPartyHTML, $joomlaHTML);
In v1.5.x, the menu item with ID 103 is properly highlighted but in v2.5.6, it isn't and the Home item is always highlighted. I think it used to be highlighted correctly in v1.6.x and earlier versions of 2.5.x as well but not sure.
I wanted to find how the process flows (where this is set) so I can see what tweaks I need to make.
DELETED WRONG INFO
Thanks Dayo! you saved my day with this:
// force highlight the external url menu item
$Itemid = JRequest::getVar('Itemid');
$menu = JSite::getMenu();
$menu->setActive($Itemid);
I don't fully understand the breadcrumb part, but I managed to get it working by editing my component's controller.php to read:
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// force highlight the external url menu item
$Itemid = JRequest::getVar('Itemid');
$menu = JSite::getMenu();
$menu->setActive($Itemid);
// force fix the breadcrumb
$app = JFactory::getApplication('site');
$pathway =& $app->getPathway();
$bcrumbs = &JPathway::getInstance('site');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
* MyCom Component Controller
*/
class MyComController extends JController
{
}
Look in the following File
Check the
/modules/mod_menu/mod_menu.php
File and you will see two functions has been called "getActive" and "getDefault"
Which can be find in following file
/libraries/joomla/application/menu.php
I think it can be customized easily now

Joomla - renderModule function strips javascript

please help, I've a problem with Joomla's function renderModule.. I am trying to render module with this function, but it unfortunately strips javascript from the the rendered module.
I use the function in my own module which includes other modules according to current article..
The code is as following:
<?php
$moduleType = "j15html";
$moduleName = "test";
$option = JRequest::getVar( 'option', '' );
$view = JRequest::getVar( 'view', '' );
$id = JRequest::getInt( 'id', 0 );
$moduleName .= $id;
//echo $view;
if ( $option == "com_content" && $view == "article" ) {
//echo $moduleName;
$module = JModuleHelper::getModule($moduleType, $moduleName);
//print_r($module);
if ( ! empty( $module ) ) {
$attribs = array();
echo JModuleHelper::renderModule( $module, $attribs );
}
}
When I set the position of the included module to any position used in my template and set it to displat in particular menu section, it renders properly even with javascript and so on..
Any advices how to make this thing working?
You didn't mention which version of Joomla you're using - but you may want to check out SOURCERER it keeps coding how you put it and does not strip out extra coding.
Make sure you read the how-to so you know how to use it because it can seem a little confusing at first, but it has a button to 'change the tags' from < to << or [ which do not get stripped out by the WYSIWYG editor in Joomla!.
Of course, that could be your issue also, if your WYSIWYG editor is on (by default it is) and you're inputting code - it strips it. An easy way is to just turn it off under global options, then when you save the code doesn't get stripped. Just turning off the WYSIWYG editor is the quick/easy/simple solution - but if you turn it back on and open that module again, the code will be gone. So it can be a tricky solution if others may like using the editor or if you're using lots of custom code around your side. In that case the plugin I mentioned above is a great solution.

Resources