Jump to content
xisto Community
pyost

Tips For Modifying Wordpress Code How to make it work the way you like

Recommended Posts

Some time ago I decided to convert my current web site (done in Joomla! CMS) to WordPress. Mostly because it is less bulky when compared to Joomla! CMS and has exactly the functions I need, unlike Joomla, where I found numerous options which I didn't need. Of course, as every professional web master (yes, I like to see myself as pro :P), I wasn't completely satisfied with the way WordPress delivers content, and so I decided to modify its code in order to make it perfect.

 

Unfortunately, as it always is, I encountered many problems, and spent even more hours trying to find a solution. That's why I decided to compile this list of useful modifications - or at least I found them useful.

 

Before I deliver the list, here are some warning/information notes:

All these modifications have been tried out on WordPress 2.1.3

The blog I am working on uses SEF URIs, so some modifications may be of no use to people not using custom permalinks.

I expect you know at least something about WordPress, since I will be using WP-specific terms without explaining them.

Always make a backup of the files you are editing

If something doesn't work, or messes up your blog, I am not to blame :)

What you see at the moment isn't the final list. I will be adding things every time I figure out something new.

Removing the annoying category base ("category/")

 

Be careful when using this modification, as it will break your post pages (links), and it isn't so easy to fix. Check the part starting with a row of dashes for an explanation.

 

As we all know, by default, WordPress uses "ugly" URIs, with a lot of question marks and ampersands (&). That's why people usually decide to use search engine friendly URIs by changing the permalink structure. However, there is one thing that can't be removed, and that is the category base. Of course, there is a simple explanation for this - if you give WordPress /news/, how can it know whether this is a page or a category? That's why it uses /category_base/news/ for categories, and just /news/ for pages. This category base can be anything you like, but it can't be empty, as this will result in using the default, /category/. However, someone (like me) might run a blog that has no pages named the same way as categories. So why would he/she have to deal with the same problem?

 

On the Internet there are several solutions to this problem, and some suggest entering just /. in to the "category base" box. From my experience, this does not work. But there is a solution that works, and it requires some file editing :ph34r:

 

The only file you will have to edit is /wp-includes/rewrite.php, since it deals with all the rewrite rules. So, open the file and find the line saying

 

$this->category_structure = $this->front . 'category/';
Replace it with

 

$this->category_structure = $this->front . '/';
Now all you have to do is go to the administration panel and update the permalink structure in order for the changes to take effect - the rewrite.php is used only when you update the structure, because the result ends up being saved in the database. Also, be sure that the category base field on the permalink page is empty.

 

You might wonder why we have done this, when WordPress recognizes a category even without the prefix (when it's not modified). That's true, you can use /news/ to display a category (if there isn't a page called the same), but what happens when you want to go to the next page? /news/page/2/ won't work! By modifying the rewrite file, we have overcome this problem.

 

------------ Now that we have removed the category base, practicaly anything is a category link. So, if your structure is /%category%/%post_id%/, /news/13/ won't be a post, but a category page for 13. which is a subcategory for news! Our only option is to change the way rewritten URIs are being parsed, and for that we will need to alter /wp-includes/classes.php. We will be editing the WP class which contains an array called query_vars. It has many keys, but you will need to alter only several, depending on your permalink structure. I will show you how I solved the problem with my structure, and you can figure out approximately what I've done. First of all, we will need to find this part

 

if ( isset($error) )			$this->query_vars['error'] = $error;
After it we should insert the code for altering the query. In my case, /news/13/ is treated as a category path consisting of category names, but it's actually a post number. That's why I will be messing with query_vars['category_name'] and query_vars['p']. At the moment, ['category_name'] contains "news/13" (notice how there are no slashes at the beginning/end) and ['p'] is empty (this should be an integer). Now we will do some simple string operations in order to extract the post ID from category_name. As I already said, I can do so because I know the permalink structure, and that's why this solution won't work for others. Anyway, this is the code I needed:

 

$length = strlen($this->query_vars['category_name']);  // we need the length for the counter$category = $this->query_vars['category_name'];  // we don't want to destroy the original, in case it is not a post$ending = '';  // this will be the ID, and now it's empty$length--;$ch = $category{$length};  // reading the last character of category_namewhile ( is_numeric($ch) )  // while this character is a digit, we add it to the ID ($ending) and read the next character	$ending = $ch . $ending;	$length--;	$ch = $category{$length};}if ( ($ending != '') && (is_numeric($ending)) && ($ch == '/') ) {  // if this really is a post $ending won't be empty, it will be a number, and the next character will be a slash	$this->query_vars['category_name'] = '';  // we aren't looking at a category, right?	$this->query_vars['p'] = (int) $ending;  // the post ID needs to be an integer!}
I haven't checked if this messes up subcategories, but I'm sure it does if the subcategory's name is a number :P

 

Oh, and if you don't know which keys does this array have, and therefore don't know what to change for your permalink structure, insert the following code anywhere in your template and it will display all the keys with their current values.

 

$test = $wp_query->query_vars;foreach ($test as $key => $value) echo $key.' -> '.$value.'<br />';

Changing the pagination style to suit your language

 

As I mentioned in the previous paragraph, WordPress uses /page/x/ for paginated results, when x is some number. I didn't find this solution suitable, as I wanted my blog to be completely in my language. I somehow needed to change the /page/ part to something else, but how?

 

You might have guessed - we need to edit the /wp-includes/rewrite.php file again, but /wp-includes/link-tempate.php, too. Let's deal with the rewrite file first. Find the following line

 

$pageregex = 'page/?([0-9]{1,})/?;
And replace it with

 

$pageregex = 'something/?([0-9]{1,})/?;

This "something" can be anything you like. Just like in the previous modification, you will have to update the permalink structure in order for the changes to take effect. The pagination is now working perfectly, but if you are using Wordpress template tags for display the next/previous page link, they will be delivering the old structure, because these functions don't care what the rewrite rules say. So on to editing the /wp-includes/link-tempate.php file. Find the following lines

 

$page_modstring = "page/";	$page_modregex = "page/?";
Obviously, you need to do this

 

$page_modstring = "something/";	$page_modregex = "something/?";

Also you need to replace this

 

$qstr = str_replace('page/1/', '', $qstr); // for mod_rewrite style
With this

 

$qstr = str_replace('something/1/', '', $qstr); // for mod_rewrite style

And now you have a WordPress blog with link in you own language :P

Share this post


Link to post
Share on other sites

change the

Tips For Modifying Wordpress Code

 

Hi, I want to change the 'page' word to 'mykeyword' in the following paging url: domainname.Com/category-base/categoryname/page/2/

 

I am using wordpress 2.5 - the code you have given above is not found in "link-template.Php"!

 

Can you please help me on this.

Thanks,

Kulandai.

 

 

-reply by Kulandai

Share this post


Link to post
Share on other sites

Nice post! Really useful. Hope to read more tips on the list soon.By the way, would you explain:

[acronym="Search Engine Friendly"]SEF[/acronym] [acronym="Uniform Resource Identifier"]URI[/acronym]s

Sorry, I totally have no idea-- what is this? how to implement?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.