When you are running a CMS, or for example a Blog you might also have SEO url's enabled through .htaccess rewrites. A common problem with these rewrites can be that you are no longer able to install a second application in a subfolder since those foldernames are included in the rewrites and can cause 404 errors.
Showing posts with label code snippets. Show all posts
Showing posts with label code snippets. Show all posts
Monday, October 18, 2010
Joomla Snippet - Strict valid You Tube movies in your articles
If you want to put movies in your articles on a site with a strict doctype you will run into problems with the embed tag that is used by the default code YouTube gives you for example. Embed is actually not XHTML strict valid and this is because embed has been deprecated from XHTML 1.0.
Joomla Snippet - layout switching
Here's some code you can use in your template index file to switch between different layouts based on the menu tree -> menu item ID. You can do all kinds of funky stuff with this.
Using JPane and laoding templates from within a template
Well, let us assume our template is called form.php. My template holds this code to create a pane and to display some content in the pane:
<?php
jimport('joomla.html.pane');
/* creating the pane */
$pane =& JPane::getInstance('tabs'); // change "tabs" to "sliders" or other way round to get the different behaviour
/* creating a content area */
echo $pane->startPane("somecontent-area");
echo $pane->startPanel("panel-name-one","panel1_id");
echo $this->loadTemplate("some-content");
echo $pane->endPanel();
echo $pane->startPanel("panel-name-two","panel2_id");
echo "some content for panel two";
echo $pane->endPanel();
echo $pane->endPane();
?>
The "difficult" thing to remember is, if you want to load a template from within a template... your template to be loaded has to be named like "your-current-template"_"template-to-be-loaded.php"
e.g.
<?php
jimport('joomla.html.pane');
/* creating the pane */
$pane =& JPane::getInstance('tabs'); // change "tabs" to "sliders" or other way round to get the different behaviour
/* creating a content area */
echo $pane->startPane("somecontent-area");
echo $pane->startPanel("panel-name-one","panel1_id");
echo $this->loadTemplate("some-content");
echo $pane->endPanel();
echo $pane->startPanel("panel-name-two","panel2_id");
echo "some content for panel two";
echo $pane->endPanel();
echo $pane->endPane();
?>
The "difficult" thing to remember is, if you want to load a template from within a template... your template to be loaded has to be named like "your-current-template"_"template-to-be-loaded.php"
e.g.
Friday, October 15, 2010
How to disable ?tp=1 in Joomla
2 ways to disable ?tp=1 in joomla:
1. Using the .htaccess file
2. By a core hack
1. Using the .htaccess file
The first and easy method is using the .htraccess file. Most of you may have known that there is a file called htaccess.txt is in your Joomla root. Rename it to the .htaccess and open in favorite text editor .Add the following code :
1. Using the .htaccess file
2. By a core hack
1. Using the .htaccess file
The first and easy method is using the .htraccess file. Most of you may have known that there is a file called htaccess.txt is in your Joomla root. Rename it to the .htaccess and open in favorite text editor .Add the following code :
##### Start ?tp=1 prevention ######
RewriteCond %{QUERY_STRING} tp=(.*)
RewriteRule ^(.*)$ index.php [F,L]
##### End ?tp=1 prevention ###### Please note that not every hosting is not supporting .htaceess and there is a chance that your site may goes down after incluiding this and also this trick will not work for all cases.Thursday, October 14, 2010
Joomla Add External CSS / Style
JDocument/addStyleSheet
Syntax
void addStyleSheet( $url, $type, $media, $attribs )where:
| Argument | Data type | Description | Default |
|---|---|---|---|
| $url | string | URL of stylesheet. | |
| $type | string | MIME type of script. | 'text/css' |
| $media | string | Media type that the stylesheet applies to. | null |
| $attribs | array | Array of attributes. |
Example 1
To add a link to some style sheet at a specific URL, you could use:$doc =& JFactory::getDocument();
$doc->addStyleSheet( 'http://www.example.com/css/mystylesheet.css' );
<link rel="stylesheet" href="http://www.example.com/css/mystylesheet.css" type="text/css" />
Example 2
From a template, you can add a link to a style sheet using a relative URL, like this:$this->addStyleSheet( 'templates/' . $this->template . '/css/mystylesheet.css' );
$this object will be an object of type JDocumentHTML when called from within a template.Joomla Add Style Decleration
Adds an internal stylesheet to the document object. The stylesheet is appended to the document objects' internal stylesheet buffer for the specified type.
where:
Syntax
void addStyleDeclaration( $content, $type )where:
| Argument | Data type | Description | Default |
|---|---|---|---|
| $content | string | Stylesheet content. | |
| $type | string | MIME type of stylesheet. | 'text/css' |
Example
To add a CSS style to the document, you could use:$doc =& JFactory::getDocument();
$style = 'BODY {'
. 'background: #00ff00;'
. 'color: rgb(0,0,255);'
. '}';
$doc->addStyleDeclaration( $style );
source: http://docs.joomla.org/JDocument/addStyleDeclaration
Wednesday, October 13, 2010
Using CSS to Wrap Text Around Images
Last night a friend asked me about floating images in his posts. I gave him a quick answer with the promise of a full followup today.
When you're writing a post you basically have two ways of floating an image. The first is pure HTML and can be done quickly without any advance setup. While it's quick - it's also dirty. Using HTML to float an image can lead to unexpected results in many browsers and, in extreme cases, may cause your theme to fall apart completely when viewed by some visitors.
The alternative to an HTML implementation is CSS. By adding two lines of code to your stylesheet you can setup your theme to display images, inline, in any post easily. I'll cover both methods but I highly recommend using CSS as a first choice.
Floating an image with HTML, as I mentioned, is quick and dirty. You can certainly do it but you'll find that your document doesn't validate well and may ultimately behave in unexpected matters in some browsers.
To float an image using pure HTML simply add the "align" attribute to your image tag. To float an image to the left of your paragraphs just add align="left" to the image tag before you close it. The image floating to the left of these paragraphs uses this method.
Obviously it's not perfect at first glance. Without margins the text looks cramped next to the picture and (if you're using certain browsers) it may look pretty awful. You can prettify this a bit by adding a margin but that would require some CSS work. Why not just do it all at once with CSS?
The code for the image above looks like this: <img src="Image URL" align="left" />
With CSS you have two choices. The first is similar to the one above while the second requires the use of an external stylesheet. If you can edit your stylesheet I definitely recommend using the second method.
To add some CSS styling directly to your post simply add the style attribute and a little CSS. Assuming you want to float the image to the left of your text simply add "float:left." I also recommend adding a margin as well. If you're floating an image to the left of text you'd add the margin on the right side (as a buffer against the text.) The code for the image to the left looks like this:
The section of my stylesheet that handles this is shown below:
When I create an image I simply add a class attribute with the appropriate selector. If I use this system the code for the image above would look like this:
Disclosure: The two images used in this post are the covers of two books that I personally own and recommend for anyone interested in CSS. The CSS Pocket Reference is a quick guide I've found helpful for refreshing my memory and The Zen of CSS Design: Visual Enlightenment for the Web is a great companion for learning about CSS and getting some ideas for theme design.
If you found this post helpful please rate it using the SpotBack widget below or leave a comment. I look forward to your feedback.
Source: http://bill2me.com/2007/04/18/using-css-to-wrap-text-around-images/
When you're writing a post you basically have two ways of floating an image. The first is pure HTML and can be done quickly without any advance setup. While it's quick - it's also dirty. Using HTML to float an image can lead to unexpected results in many browsers and, in extreme cases, may cause your theme to fall apart completely when viewed by some visitors.
The alternative to an HTML implementation is CSS. By adding two lines of code to your stylesheet you can setup your theme to display images, inline, in any post easily. I'll cover both methods but I highly recommend using CSS as a first choice.
Creating Inline Images with HTML
Floating an image with HTML, as I mentioned, is quick and dirty. You can certainly do it but you'll find that your document doesn't validate well and may ultimately behave in unexpected matters in some browsers.To float an image using pure HTML simply add the "align" attribute to your image tag. To float an image to the left of your paragraphs just add align="left" to the image tag before you close it. The image floating to the left of these paragraphs uses this method.
Obviously it's not perfect at first glance. Without margins the text looks cramped next to the picture and (if you're using certain browsers) it may look pretty awful. You can prettify this a bit by adding a margin but that would require some CSS work. Why not just do it all at once with CSS?
The code for the image above looks like this: <img src="Image URL" align="left" />
Floating Images with CSS
With CSS you have two choices. The first is similar to the one above while the second requires the use of an external stylesheet. If you can edit your stylesheet I definitely recommend using the second method.To add some CSS styling directly to your post simply add the style attribute and a little CSS. Assuming you want to float the image to the left of your text simply add "float:left." I also recommend adding a margin as well. If you're floating an image to the left of text you'd add the margin on the right side (as a buffer against the text.) The code for the image to the left looks like this:
<img src="Image URL" style="float:left; margin-right: 5px;" />Adding CSS to an External Stylesheet
The absolute ideal option for setting up inline images is to setup up special class selectors in your external stylesheet, for most WordPress installations this would be the style.css file in your theme directory. In my style sheet I have two classes setup, one for aligning left and one for aligning right.The section of my stylesheet that handles this is shown below:
.alignLeft { float:left; margin-right: 1em; }
.alignRight { float:right; margin-left: 1em; }When I create an image I simply add a class attribute with the appropriate selector. If I use this system the code for the image above would look like this:
<img src="ImageURL" class="alignLeft" />Notes on CSS and Inline Text
While I concentrate on wrapping text around images in this post you can use the same ideas to wrap text around other items like advertisements and special text blocks. The "alignLeft" and "alignRight" classes that I setup are actually used throughout this theme (and in others I've authored) to float just about everything that needs floating. Because CSS is versatile you're limited only by your imagination.Disclosure: The two images used in this post are the covers of two books that I personally own and recommend for anyone interested in CSS. The CSS Pocket Reference is a quick guide I've found helpful for refreshing my memory and The Zen of CSS Design: Visual Enlightenment for the Web is a great companion for learning about CSS and getting some ideas for theme design.
If you found this post helpful please rate it using the SpotBack widget below or leave a comment. I look forward to your feedback.
Source: http://bill2me.com/2007/04/18/using-css-to-wrap-text-around-images/
Getting img src from a string using php
<?php
function get_img_src($field_value = "", $default_img_thumbnail = ""){
$pattern = '/<img[^>]+src[\\s=\'"]';
$pattern .= '+([^"\'>\\s]+)/is';
if(preg_match( $pattern, $field_value, $match)){
$img_src = $match[1];
// return img src (img path)
return $img_src;
}else{
// return a blank img
$default_img_thumbnail = "images/blank.png";
return $default_img_thumbnail;
}
}
$string= "<b>Text here</b> <img src='image-folder/image.jpg' title='image'><b>Text here</b> ";
$img_src = get_img_src($string);
echo $img_src;
?>
Output:
image-folder/image.jpg
-----------------------------------------
function get_img_src($field_value = "", $default_img_thumbnail = ""){
$pattern = '/<img[^>]+src[\\s=\'"]';
$pattern .= '+([^"\'>\\s]+)/is';
if(preg_match( $pattern, $field_value, $match)){
$img_src = $match[1];
// return img src (img path)
return $img_src;
}else{
// return a blank img
$default_img_thumbnail = "images/blank.png";
return $default_img_thumbnail;
}
}
$string= "<b>Text here</b> <img src='image-folder/image.jpg' title='image'><b>Text here</b> ";
$img_src = get_img_src($string);
echo $img_src;
?>
Output:
image-folder/image.jpg
-----------------------------------------
Useful if you need to extract src value from a chunk of html codes.
Adding meta data manually by codes in joomla
JDocument/setMetaData
From Joomla! Documentation
Jump to: navigation, search
Sets the value of a meta data item.
where:
| Argument | Data type | Description | Default |
|---|---|---|---|
| $name | string | Name of meta data item. If $name is 'description' or 'generator' then the effect is the same as calling setDescription or setGenerator respectively. Meta data names are always folded to lowercase. | |
| $content | string | Content of meta data item. If there is already a meta data item with the same value of $name (case independent) then this value will replace it. | |
| $http-equiv | Boolean | True if meta data item is equivalent to an HTTP header field. | false |
Example 1
$doc =& JFactory::getDocument();
$doc->setMetaData( 'tag-name', 'tag-content' );
<meta name="tag-name" content="tag-content" />
Example 2
$doc =& JFactory::getDocument();
$doc->setMetaData( 'tag-name', 'tag-content', true );
<meta http-equiv="tag-name" content="tag-content" />
See also
Retrieved from "http://docs.joomla.org/JDocument/setMetaData"
Increasing Number of Characters in Search Box in joomla
1. changed line 62-65 in administrator/components/com_search/helpers/search.php:
Code:
// limit searchword to 20 characters
if ( JString::strlen( $searchword ) > 50 ) {
$searchword = JString::substr( $searchword, 0, 49 );
$restriction = true;
if ( JString::strlen( $searchword ) > 50 ) {
$searchword = JString::substr( $searchword, 0, 49 );
$restriction = true;
Removing the Joomla Generator Tag
A common request from Joomla web masters is the ability to remove the generator meta tag from the source output of the site. This is usually for security reasons to make it less obvious that the site is running Joomla. The generator value is really easy to modify and remove with one line of PHP code.
Fire up your favourite editor and load the
There are several other files that could be loaded by Joomla in the template. You will also need to do this to the
This is just another example of how flexible the Joomla templating engine is and why it's a makes Joomla a great choice to power your web site ... anonymously.
source: http://www.theartofjoomla.com/home/19-designer/64-removing-the-joomla-generator-tag.html
Fire up your favourite editor and load the
index.php file of the default template on your site. Most templates should have a block of PHP at the top of the file starting with <?php and closing with ?>. Find this block and just before the closing PHP brace, insert the following lines of code:// Remove the generator meta tagWhat we have done here is told the template (that's what
$this->setGenerator(null);
?>
$this is) to set the value of the meta generator tag (that's what setGenerator does) to nothing (that's what null means). When you do this, refresh your web page and view the source of the output. Scan down from the top of the file to find the meta generator tag.<meta name="generator" content="" />You can see it's obviously still there but the value is empty, giving you no clues as to what CMS is running the web site. Nothing is probably the safest value, but you could set it to anything you like if you really wanted to.
There are several other files that could be loaded by Joomla in the template. You will also need to do this to the
component.php (that supports the Joomla Print View) and if you have a custom Error or Offline page, you will need to add the line of code to error.php and offline.php respectively. You can find out more about these additional template files in the Template section of the Art of Joomla Developer Reference.This is just another example of how flexible the Joomla templating engine is and why it's a makes Joomla a great choice to power your web site ... anonymously.
source: http://www.theartofjoomla.com/home/19-designer/64-removing-the-joomla-generator-tag.html
CSS Centering Elements
How do you center a containing block on a page using CSS? There are two main methods and the choice should be made based on whether your page layout is liquid (will move in and out depending on the size of the browser window) or fixed width.
Centering liquid layouts
Liquid layouts are easy to center using margins on each side of the containing box. The margins can be set with em, pixel or percentage units. div#container
{
margin-left: 10%;
margin-right: 10%;
}Centering fixed-width layouts
Theoretically, you should be able to apply auto margins to the left and right of the containing block and it should center on the page. The W3C Visual formatting model states: “If both ‘margin-left’ and ‘margin-right’ are ‘auto’, their used values are equal. This horizontally centers the element with respect to the edges of the containing block.”
So, a containing block should be able to be centered using the following rules:
div#container
{
margin-left: auto;
margin-right: auto;
width: 50em;
}However, some browsers do not center the containing blocks using this method as they ignore the auto margins. These browsers include: - NN4 (Mac and Win)
- Win/IE4
- Win/IE5
- Win/IE5.5
- Win/IE6 (when in quirks-mode)
1. Center the body
While these browsers ignore auto margins, they will follow “text-align: center”. If this is applied to the body, the containing block will center correctly. So, a new rule is added: body
{
text-align: center;
}
div#container
{
margin-left: auto;
margin-right: auto;
width: 50em;
}2. Resetting text-align
The only problem with this new rule is that all content on the page is now center-aligned. To overcome the center alignment problem, a new declaration is added within the containing block rule set – “text-align: left”. The final CSS code is: body
{
text-align: center;
}
div#container
{
margin-left: auto;
margin-right: auto;
width: 50em;
text-align: left;
}
http://www.maxdesign.com.au/articles/center/Adding External Javascript in joomla
JDocument/addScript
Adds the definition of a linked external script to the document object. Duplicates are ignored.Syntax
void addScript( $url, $type )where:
| Argument | Data type | Description | Default |
|---|---|---|---|
| $url | string | URL of script. | |
| $type | string | MIME type of script. | 'text/javascript' |
Example
To add a link to some JavaScript code at a specific URL, you could use:$doc =& JFactory::getDocument();
$doc->addScript("http://www.example.com/js/myscript.js");
produce following link in the HTML HEAD section:
<script type="text/javascript" src="http://www.example.com/js/myscript.js"></script>
source: http://docs.joomla.org/JDocument/addScriptDeclaration
Adding script Decleration in joomla
Adds an internal script to the document object. The script is appended to the document objects' internal script buffer for the specified type.
source:
http://docs.joomla.org/JDocument/addScriptDeclaration
Syntax
void addScriptDeclaration( $content, $type )| Argument | Data type | Description | Default |
|---|---|---|---|
| $content | string | Script content. | |
| $type | string | MIME type of script. | 'text/javascript' |
Example 1
To add a Hello World alert in JavaScript, you could use:$content = 'alert( \'Hello Joomla!\' )';
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration( $content );
Example 2
Adding a slightly more realistic piece of JavaScript:function getJavaScript($message) {
$javascript .= 'if(window.addEventListener){ // Mozilla, Netscape, Firefox' . "\n";
$javascript .= ' window.addEventListener("load", function(){ alert("' . $message . '");}, false);' . "\n";
$javascript .= '} else { // IE' . "\n";
$javascript .= ' window.attachEvent("onload", function(){ alert("' . $message . '");});' . "\n";
$javascript .= '}';
return $javascript;
}
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration( getJavaScript( 'This will appear in an alert box after the page loads.' ) );
source:
http://docs.joomla.org/JDocument/addScriptDeclaration
Tuesday, October 12, 2010
Joomla as a PHP Framework
Sample Codes:
<?php
/* Initialize Joomla framework */
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* To use Joomla's Database Class */
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
/* Create the Application */
$mainframe =& JFactory::getApplication('site');
/**************************************************/
// Your code starts here...
/**************************************************/
?>
<?php
/* Initialize Joomla framework */
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* To use Joomla's Database Class */
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
/* Create the Application */
$mainframe =& JFactory::getApplication('site');
/**************************************************/
// Your code starts here...
/**************************************************/
?>
Subscribe to:
Posts (Atom)