Showing posts with label WordPress. Show all posts
Showing posts with label WordPress. Show all posts

Rename wp content Folder Wordpress

Ever since WordPress version 2.6 you can actually move your wp-content directory to a different location. The wp-content directory will store all your theme files, plugin files and images.

Why Move The wp-content Folder

The best reason to move the wp-content is for security if you move this to an unexpected location any hackers looking to target this area won't be able to find it, or it will make it more difficult to find.
To change the location of the wp-content is quite simple as the location of this folder can be configured in your wp-config.php file.
All you have to do is add a new variable WP_CONTENT_DIR and change the location of your wp-content folder.
define( 'WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/content/wp-content' );
To change the location of the wp-content URL there is another variable you can define in the wp-config.
define( 'WP_CONTENT_URL', 'http://www.paulund.co.uk/blog/content/wp-content' );


Moving Plugin Folder

If you don't want to change the location of the entire wp-content folder but just want to move the plugins folder then you can change this location the same as above.
For the plugin folder location we need to define the variable WP_PLUGIN_DIR.
define( 'WP_PLUGIN_DIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/content/wp-content/plugins' );
The same as the wp-content folder you can change the URL of the plugin folder for the variable WP_PLUGIN_URL.
define( 'WP_PLUGIN_URL', 'http://example/blog/content/wp-content/plugins');
The problem with doing this for plugins is that there is another variable which can be used by some plugin developers...PLUGINDIR. Therefore you need to make sure that you change this too.
define( 'PLUGINDIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/content/wp-content/plugins' );
Continue Reading

Smarter Filtering Width And Height of Images

Whenever you upload an image using the WordPress media library and add this image to your post, WordPress will automatically add the image Width and Height attribute in the image element.
This is fine for most themes, but there are other times when you don't want WordPress to add these attributes. The benefit of not adding these attributes is that you can leave any sort of dimensions to the CSS.
If you want to remove these attributes from the image elements it can be done with a simple regular expression search and replace and attach this to two WordPress filters.
  • post_thumbnail_html - Filter any post thumbnails
  • image_send_to_editor - Filter the HTML when adding a image to the editor
Add the following code to remove the width and height attributes from the image elements.

add_filter( 'get_image_tag', 'remove_width_and_height_attribute', 10 );
add_filter( 'post_thumbnail_html', 'remove_width_and_height_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_and_height_attribute', 10 );
function remove_width_and_height_attribute( $html ) {
   return preg_replace( '/(height|width)="\d*"\s/', "", $html );
}
Continue Reading

Add RSS Feed link to Wordpress

When you have a WordPress site you can assign different posts to categories, this allows you to group your posts together.
For example on this site I have created categories for tutorials and snippets, so if people are looking to create a complete feature you can look in the tutorials category, but if you just want some useful coding tips you can just in the snippets section.
When you create a category you are then able to navigate to this category to view all the posts which are grouped in the category.

When you view a certain category WordPress will display this content using a number of files, which are displayed in the category hierarchy of:
  • category-slug.php
  • category-ID.php
  • category.php
  • archive.php
  • index.php
First WordPress will check to see if you have a file in your theme folder names category-{category-slug}.php, if this exists then it will use this to display your category content. If this doesn't exist then it will search for a file called category-{ID}.php, then category.php, then archive.php, the finally the index.php.
I normally create a category.php file to show the category content useless I want a certain category to look different than all the others.

Add A Category Feed For Certain Category

rss feed

There maybe some categories which are more popular than others, for example you might have a freebie category where you post about free products on each post. This is a perfect candidate for someone to subscribe to just this category RSS feed.
Luckily in WordPress it will create an RSS feed for each category and they make it very easy to create a link to this RSS feed.
First open up your category.php file and add the following code to the top of your content.
<?php
     $category = get_category();
?>
<a href="<?php echo get_category_feed_link( $category[0]->cat_ID ); ?>">Subscribe to the <?php echo $category[0]->name; ?> category RSS feed</a>
Continue Reading

WordPress Auto Translate Plugin

We got this asked a lot. The official WordPress guide about translating is a bit confusing and isn’t focused on plugins, so I decided to write a short guide here. It will use WatuPRO as example but is also applicable to any other properly written and localization-ready WordPress plugin. Let’s go, step by step.

1. Get Poedit

Poedit is free editor for .pot files and de-facto the standard for working with them. Unless you have specific alternative in mind and a good reason to use it, I recommend you downloading Poedit, it’s available for most operating systems.

2. Open the .pot file

Translate Plugin

You actually need to go to File -> New Catalog from POT file. Select the .pot (in WatuPRO this is watupro.pot stored in the root) and double-click on it. Poedit will ask you about project title – you can use the default one. Then you’ll need to save the catalog as .po file. I recommend you properly name the target file right from this moment. The official docs are a bit lacking on this issue, and it’s very important. The name of your .po file should be constructed like this:
plugin-textdomain-with-dashes-locale_Locale.po.
To make it more clear, let’s use WatuPRO example again and a French translation. The proper name is:
watupro-fr_FR.po
How will you know what are the correct parts of this name? The plugin textdomain is usually same as the folder name of the plugin, but to be safe you should seek information from its author OR open the main plugin file and search for “load_plugin_textdomain” call. The first word in the braces is the plugin textdomain. The proper name of your locale can be found at Here
Where to save this file? With WatuPRO you should save it in languages/ folder. Other plugins may require you to save the file in the root plugin folder or a folder with another name – you should look at the plugin documentation for this. If such information is not provided, it’s usually safe to assume the root folder, or again to try finding the info by searching for “load_plugin_textdomain” function in the plugin files.

3. Translate It

Some guides will tell you that you have to translate everything. NO. This is not true. You can omit strings and translate only what you need translated. For example users of plugins like WatuPRO may want only the end-user facing texts translated. In such case you don’t have to spend time translating everything – the plugin will work just fine with only few localized texts. For the rest it will use the non-translated text (usually English).
Important: Don’t translate any %s or %d characters that you see in the texts. They will be replaced with dynamic texts and numbers. Just leave them as is.

4. Set Your Locale in wp-config.php

This is a very important step and without it nothing will work. Unless you already use a translated version of WordPress, it’s possible that your locale is not set in wp-config.php. So open the file and find “WPLANG”. You will see a line like this:
define(‘WPLANG’, ”);
You have to add your locale there. Using the French example, it will be:
define(‘WPLANG’, ‘fr_FR’);
That’s it. Save the file and re-upload it. Don’t worry, any plugins and themes that are not translated will keep working fine in English.

5. Re-activate the plugin

Some users report that even after going through the above steps, nothing happens. So you may need to re-activate the plugin to get it to work (this is not documented anywhere so I am not even sure why it’s sometimes required and sometimes – not).
Continue Reading