Nov 22, 2009

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

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Software > Content Management Systems (CMS) & Blogs > WordPress

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

pyost
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 tongue.gif), 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 biggrin.gif
  • 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 smile.gif

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

CODE
$this->category_structure = $this->front . 'category/';

Replace it with

CODE
$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

CODE
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:

CODE
$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 wink.gif

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.

CODE
$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

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

And replace it with

CODE
$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

CODE
$page_modstring = "page/";
    $page_modregex = "page/?";

Obviously, you need to do this

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


Also you need to replace this

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

With this

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


And now you have a WordPress blog with link in you own language smile.gif

 

 

 


Comment/Reply (w/o sign-up)

FeedBacker
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

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : tips, modifying, wordpress, code, make, work

  1. Twitter-like Wordpress Theme
    (0)
  2. Afraid Of Updating My Wordpress
    (2)
    currently i have a wordpress version 2.3.1, but wordpress currently release version 2.3.2. but i
    dont know how to update it, im simply afraid of updating it it might be i'll be loosing my posts
    and comments when i update it. i've already searched a lot of articles about updating wordpress
    but still i can't follow their instructions they're not clear at all. could someone give me
    clear instructions to update my worpress... please thanks .......
  3. Best Wordpress Skin You Can Find.....
    The Free WordPRess Skins (2)
    Theme 1 (This mac like skin has to be the best I've seen so far.)
    http://perishablepress.com/press/2006/01/09/apathy-theme/
    http://wpdesign.downloadyour.com/index.php...ol+Elegant+Blue
    http://templates.arcsin.se/wp-demo/fluid-solution/
    http://perishablepress.com/press/wp-conten.../garbage_Fx.gif
    http://www.blogohblog.com/wordpress-theme-silver-light/ http://tarskitheme.com/
    http://wpthemesplugin.com/mezzo-3-column-wordpress-theme/ http://naturalparenting.wsnw.net/blog/
    http://kathymoore.daynote.com/blog It is actually a modified version of the....
  4. How Do I Customize Wordpress
    to look like an ordinary website (6)
    how do i? nuff said....
  5. All New Wordpress Plug-ins & Widgets By M^e
    (0)
    A bit of self advertising /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0"
    alt="biggrin.gif" /> I've been dabbling in WordPress plug-ins for a while now and have
    successively come up with quite a few. Here's the list so far. If you find them useful, feel
    free to grab them off my site and use in your own blog. Some of them are Sidebar Widgets , which
    means you'll need a widget enabled theme for WordPress. The others are simple plug-ins meant to
    perform other tasks. Here's the list... GARR (Google AdSense Referral Rotator) - A sideb....
  6. Wordpress Widgets Drag/drop Bug ..
    (3)
    For those of you new to widgets like me, I downloaded a new version of the widgets plugin from
    automattic.com >> HERE Problem: Everything seems to be installed fine according to the
    readme, the plugin activated, the scriptaculous files are in wp-include/js/scriptaculous. But when
    you go to the plugins page You cannot drag/drop the widgets. All that happens is you get the cross
    cursor but the widget box gets highlighted (selected ) when u click and try to drag the widgets.
    Solution: Find this in widgets.php (at line 203): wp_enqueue_script( &#....
  7. Wordpress 2.0.6
    Latest Release (8)
    There has been another release of WordPress after the Wordpress Ronan version, or Wordpress 2.0.5
    version. Quoting from wordpress.org: QUOTE We have a pretty important release available for
    everyone, it includes an important security fix and it’s recommended that everyone upgrade. This is
    the latest release in our stable 2.0 line, which we’ve committed to maintaining for several more
    years. Here’s what’s new: * The aforementioned security fixes. * HTML quicktags now work
    in Safari browsers. * Comments are filtered to prevent them from messing up your ....
  8. Wordpress: Google Adsense Referral Rotator Plug-in
    (7)
    Preamble: Google AdSense Referrals are pretty cool in a way as they contribute well in topping
    up on your daily AdSense earnings. Moreover, they're quite unobtrusive and can be placed
    anywhere on the page without interfering with the content much. According to me they're best
    placed in the side-bar and since I'm using a widget enabled theme I got down to searching
    for a widget plug-in that would let me to do just that. If you don't know how a AdSense
    Referral looks like, here's a snapshot… Anyway, I found plenty of AdSens....
  9. Wordpress Url Display Problem
    The domain name does not get reflected (2)
    OK. I have a AstaHost account here: http://kmaheshbhat.astahost.com I also have a domain name
    parked: http://www.WiseTome.com On the root public_html, I have installed Drupal, and it is
    running fine. Now, I installed a WordPress engine in public_html/cao directory. And it works out
    fine. When I type http://www.WiseTome.com/cao it properly goes to the blog. But after it goes
    there it shows it as http://kmaheshbhat.astahost.com/cao That is annoying. It should show it as
    http://www.wisetome.com/cao itself! I even went to the admin login of WordPress and chang....
  10. Wordpress Theme Help
    (3)
    Aight .. I liked this wordpress theme called DFire, and m trying to use it on my Blog. In its
    default state DFire looks good (visually) but the reading part kinda looks odd. I thought of
    changing the font and color etc .. but since i am no good at CSS .. lol .. i thought maybe someone
    here could help .. DFire Theme Download Current Style.css : CODE body {     font-family:
    verdana;     font-size: 12px;     color: #FFFFFF;     text-align: center;     background: #E1B26B
    url("images/bg.gif");     margin: 0;     padding: 0;     } /* Design Containers and Specifi....
  11. WordPress Theme Help Needed
    (0)
    I want to put this wordpress theme >>LINK . I can manage everything else except when it comes to
    PHP and CSS. This is a 1 Column theme. Could someone help me make this a 2 Column theme ? Go to the
    link and click test run. Now Lets say i shift the entire writing area to my left .. could i get a
    line/divider and the right could be made as a 2nd column with links and stuff ? Regards Dhanesh. ....
  12. Wordpress 2
    Have you already upgraded? (4)
    I did the upgrading today... and man was it worth it. First of all the upgrading went really
    smoothly and I had no problem getting my theme and plugins to work with it. The new admin panel is
    just awesome. Writing there is like working with a text processor. I have a lot of categories and
    keep always adding more and it's really handy that I can add them while I write the post, not
    needing to save as draft and go to the categories page to add a new one and then edit the post
    again. Same goes for files. ....
  13. Wordpress Cannot Find Db
    (6)
    Hy! I try to install Wordpress 1.5 on my new astahost account, but the installation always says...
    "We were able to connect to the database server (which means your username and password is okay)
    but not able to select the rockfm_db database. Are you sure it exists? On some systems the name of
    your database is prefixed with your username, so it would be like username_wordpress. Could that be
    the problem? If you don't know how to setup a database you should contact your host. If all
    else fails you may find help at the WordPress Support Forums." I tried to edit ....
  14. Wordpress Rss
    i need to understand (1)
    could someone help me understand the way wordpress creates rss feeds. i need to figure out what the
    address will be to my feeds so that i can put them up on the website so that visitors who are not
    very tech can easily access them. my experimental feed is here . is the address needed by a
    feeder - thundebird for example - just that address + /feed.bla or something. i tried /atom.xml but
    that didnt work and i sort of got stuck. thanks in advance....

    1. Looking for tips, modifying, wordpress, code, make, work

See Also,

*SIMILAR VIDEOS*
Searching Video's for tips, modifying, wordpress, code, make, work
advertisement



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

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com