Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  yordan : (14 April 2014 - 05:28 PM) By The Way, This Could Be An Interesting Subject For A Topic, What About Posting This Question? Let's See If Other People Have The Same Feeling Concerning Bootlists!
@  yordan : (13 April 2014 - 09:36 AM) Boot Order : Cd, [Usb,] Hard Drive :D
@  yordan : (11 April 2014 - 07:23 PM) I Simply Let The Bios Do That
@  Ritesh : (11 April 2014 - 10:23 AM) Is It Possible To Launch Fedora Live Cd Or Installation Disk From Hard Drive On Windows Platform Using Grub Mbr File.
@  Ritesh : (11 April 2014 - 10:21 AM) No U Are Not.. Btw.. I Have Question For You.
@  yordan : (10 April 2014 - 08:02 AM) You Are Partially Right.
I Was Not.
Nevertheless, I Am Again :)
@  Ritesh : (09 April 2014 - 07:33 PM) :P
@  Ritesh : (09 April 2014 - 07:33 PM) I Think U R Not..
@  yordan : (09 April 2014 - 09:28 AM) I'm The Master Of The Shoutbox!
@  yordan : (05 April 2014 - 10:32 PM) He-He
@  Ritesh : (04 April 2014 - 06:59 PM) Ha Ha Ha ....
@  yordan : (04 April 2014 - 11:15 AM) Welcome Back, Starscream!
@  yordan : (03 April 2014 - 02:31 PM) And I Hope That He Will Come Back Soon :)
@  yordan : (01 April 2014 - 02:53 PM) Nice, Ritesh Came, I'm Not Home Alone Today.
@  Ritesh : (01 April 2014 - 08:51 AM) Oh!!! Poor Dear Yordan..
@  yordan : (31 March 2014 - 10:02 AM) I'm A Poor Lonesome Cow-Boy
@  yordan : (27 March 2014 - 02:22 PM) He Is Unpatient Due To His Patients!
@  Ritesh : (27 March 2014 - 10:46 AM) :(
@  Ritesh : (27 March 2014 - 10:46 AM) He Is Busy With His Patients.
@  yordan : (26 March 2014 - 08:12 PM) Ahsani, Where Are You?

Photo

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


2 replies to this topic

#1 pyost

pyost

    Way Out Of Control - You need a life :)

  • Members
  • 1,090 posts
  • Gender:Male
  • Location:Vancouver, British Columbia
  • myCENTs:67.69

Posted 20 April 2007 - 10:32 AM

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_name

while ( 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

#2 Guest_FeedBacker_*

Guest_FeedBacker_*
  • Guests

Posted 25 April 2008 - 05:41 AM

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

#3 imjjss

imjjss

    Advanced Member

  • Members
  • 162 posts

Posted 29 November 2010 - 04:41 PM

Nice post! Really useful. Hope to read more tips on the list soon.
By the way, would you explain:
[codebox]SEF URIs[/codebox]

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



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users