Tag: HOWTOs

Helpful WordPress wp-config.php Settings

wp-config

wp-config.php – one most important WordPress files, yet often overlooked. There are a number of great configuration gems you can add to your wp-config.php file to help with debugging, performance, and maintenance that I would recommend looking at if you use WordPress (there is surely something for everyone). All settings are documented: in the WordPress Codex but here are some of my favorite.

  • Specify the Number of Post Revisions
    define( 'WP_POST_REVISIONS', 3 );

    The default is set to 5 but a lower number can decrease database clutter.

  • Empty the Trash
    define( 'EMPTY_TRASH_DAYS', 15 ); // 15 days

    The default is set to 30 but if you are looking to make things tidier – you can easily lower the time between deleting items marked as trash.

  • Automatic Database Optimizing
    define( 'WP_ALLOW_REPAIR', true );

    The default is set not to optimize the database – so putting this in the wp-config will enable optimization.

  • Disable the Plugin and Theme Editor
    define( 'DISALLOW_FILE_EDIT', true );

    The default is set to allow editing. Users want the ability to edit theme and plugin files, but developers know better and know it is just a matter of time before save goes bad. Disallow it.

  • Save Queries for Analysis
    define( 'SAVEQUERIES', true );

    The default is not to save queries. As mentioned before in an earlier post this is a great way to debug and optimize WordPress.

  • Redirect Nonexistent Blogs
    define( 'NOBLOGREDIRECT', 'http://example.com' );

    If this is set when using WordPress Multisite – you can set up a redirect for the browser if the visitor tries to access a nonexistent blog so you can better control traffic.

Overall – there are a number of helpful settings available for you to explore by adding a line or two to to the wp-config.php file. It is well worth your time reviewing your site performance, needs, and data every so often to make sure your site’s performance is the best it can be. Take a few minutes to check out all the options available to you!

Include or Exclude Category for RSS Feed or Search

There are certain times when you need to include or exclude certain categories in the feed or search results. The easiest way to take care of this is to add the following to your functions.php file.

function sdac_remove_from_feed( $query ) {
	if ( $query->is_feed ) {
		$query->set( 'cat',-1 );
	}
        return $query;
}
add_filter( 'pre_get_posts','sdac_remove_from_feed' );

The code above will exclude all posts that are in the category with ID 1 from your feed. If you wanted to exclude everything from the search results – you would change one line:
$query->is_feed to $query->is_search

If you wanted to only show items from the category with ID 1 – you would just need to remove the “-” before the category number.

Note: You can include/exclude items tagged, etc by changing the ‘cat’ to whatever else you would normally use in the query.

Conditionally Show Only On Home Page (Front Page)

There a are two functions that are used quite a bit to conditionally show content on the very front page (home page or page 1 of your blog). First – we had the function is_home(), but in WordPress version 2.5 – a new function was introduced: is_front_page().

You might think both would conditionally only display content on the home page (front page, page 1 of your blog) – but you actually need to use the following code to accomplish showing something ONLY on the front page of your paged blog:


<p>Show this text only on the home page</p>

So – if you want to easily show something on what I would consider the front page – use the code above to successfully achieve that. (This is particularly helpful if you have ads that run speficically on the home page and others that are ROS (run of site).

Further reading:

Locked Out of vBulletin?

Recently I ran into an issue where I could not log into vBulletin (after changing the forum URL from (example.com/forums/ to forums.example.com). Lesson learned – you need to clear the cookie path in the admin before changing over to the new URL.

If it is too late though, you can download a file called “tools.php” that is in the do_not_upload file of the standard vBulletin folder. Upload that to the admincp folder along with the “install” folder and then go to your forum’s URL /admincp/tools.php. You will be prompted for your customer ID, and then you can reset your cookie settings.

Once you are done, make sure to delete the file and install folder. Login should once again work!

WordPress Logic: If Is Logged In

There are several tidbits of code that I have collected over the past few years that make it easier and easier to turn a simple WordPress installation into a very functional content management system (CMS). There are many times when it would be nice to show logged in members certain bits of information (certain categories, posts, or just a simple “Welcome back!”) and of course there is a simple way of doing this:

<?php if ( is_user_logged_in() ) echo 'Welcome back!';?>

That bit of code will allow you to do something like this which will allow you to show the logged in user’s preferred user name (selected in the User preferences), and then a list of member only pages (private page parent is page ID 20 – I am showing all sub pages of the members-only pages marked as private). If the user is not logged in, they will get a Welcome visitor! greeting :

<?php
if ( is_user_logged_in() ) :
  global $current_user;
  get_currentuserinfo();
?>	
  <p>Welcome back <?php echo esc_html( $current_user->display_name );?></p>
  <p>Here is a list of private pages only viewable by Members:</p>
  <ul>
  <?php wp_list_pages('post_status=publish,private&child_of=20');?>		
  </ul>
<?php else : ?>
  <p>Welcome, visitor!</p>
<?php endif;?>

You can make it as simple as a change in greeting for members and non-members, or put in specific logic like I did with showing the pages. The potential is limitless.

Search Specific Category in WordPress

There are some times when you want to limit your search to a particular category, or perhaps multiple categories. This is relatively simple to do in WordPress by adding a hidden field to your search code. (See example below)

<form method="get" id="search form" action="/">
<div>
<input type="text" value="" name="s" id="s" />
<input type="hidden" value="22" name="cat" id="scat" />
<input type="submit" id="search_submit" name="Search" value="Search"/>
</div>
</form>

You can see I added my hidden input field on the third line. When I add this in, it then adds onto the query used to search. Your search will go from something like http://jzelazny.wpengine.com/?s=WordPress to http://jzelazny.wpengine.com/?s=WordPress&cat=22 and will only return posts in the category ID you choose.

Updating From WordPress 2.2 to 2.3

I have been using WordPress for almost 4 years and have collected a number of database tables, plugin specific directories, and miscellaneous junk along the way on a number of my sites. I wanted to take some time and really do some spring cleaning. After a few hours – I was very pleased with the results and I wanted to share what worked for me.

Here are the steps I recommend to really clean out a WordPress installation:

  1. Make a backup of everything. You never know what you are going to get into – and a backup is very important incase things go bad.
  2. Make a test installation. I always use a test server when I know I am going to make several changes. This way, jappler.com was not down while I was testing and cleaning up. With your backup files, import your database on the test server, and grab the latest version of WordPress (hopefully using subversion)
  3. Remove old or unused plugin database tables. I then went into the WordPress database and removed (dropped) any tables that were created by any of my old or unused plugins that were still hanging around.
  4. Weed out old, unused plugins. I went through my plugins and deleted any old or unused plugins. I had a few “I should really use this” plugins that were never used – and to tidy up – I deleted them before uploading them to the test server environment. Make sure to also remove any files/folders that old plugins might have used in the root folder and or wp-content.
  5. Move your theme over. Upload the theme file over to the test server.
  6. Move your uploads over/miscellaneous files. Upload the uploads folder (wp-content) so you will have all your old uploads.
  7. Update WordPress. Run through the upgrade process (/wp-admin/upgrade.php).
  8. Debug your plugins. Log in, create a test post to make sure you can write to the database without any errors. I ran into a few problems with this, so I deactivated my plugins and activated them one by one. I found I had a few plugins that were not compatible with WordPress 2.3, but there were updates available, so I updated my plugins and moved on.
  9. Import Tags (if you have them). I used Ultimate Tag Warrior and WordPress has an import option for that (Manage > Import) for that.
  10. Display your WordPress tags in your WordPress theme. Now that tagging is available within WordPress without any plugins, you can easily display tags by using: You can also use a tag cloud (see Codex documentation)
  11. Make another backup. I always make another backup of my sites when I am happy with the end result – just incase something happened – I would not lose all my hard work.
  12. Enjoy your updated installation. Hey – you did it! A clean, lean, updated web site. Nice work. Now go and enjoy it!

Highlighted Navigation with WordPress

Ever wonder how to automatically highlight your tabs or navigation depending on the category, page, or post you are on? Wonder no more! To make this work, there are three bits of code you will need:

  • The header code to control the body ID (and to tell us what page we are on)
  • The navigation code
  • The CSS to control the highlight

For this example, I will use this web site as an example.

The header code

ID;
$parent = 1;
while($parent) {
$page_query = $wpdb-&gt;get_row("SELECT post_name, post_parent FROM $wpdb-&gt;posts WHERE ID = '$current_page'");
$parent = $current_page = $page_query-&gt;post_parent;
if(!$parent)
$parent_name = $page_query-&gt;post_name;
}
?&gt;

&lt;body id=&quot;"&gt;

The XHTML code (navigation)

<div>
<ul>
<li><a href="/index.php">Home</a></li>
<li><a href="/blog/">SDAC Blog</a></li>
<li><a href="/web_solutions/">Web Solutions</a></li>
<li><a href="/network_solutions/">Network Solutions</a></li><li><a href="/user_interface_solutions/">User Interface Solutions</a></li>
<li><a href="/custom_solutions/">Custom Solutions</a></li>
<li><a href="/search/">Search</a></li>
</ul>
</div>

The CSS code

#home #nav-home, #about_us #nav-home, #contactus #nav-home, #faqs #nav-home, 
#disclaimer #nav-home, #search #nav-search, 
#blog #nav-blog, #web_solutions #nav-websolutions, #network_solutions #nav-networksolutions, 
#user_interface_solutions #nav-userinterfacesolutions, 
#custom_solutions #nav-customsolutions, #tos #nav-home, #sitemap #nav-home {
	  background:url("images/tabs/active_tab_bg.gif") repeat-x;
      border-right:1px solid #676e78;
      }
          
#tabs li:hover, #tabs li:hover a {
	background: url("images/tabs/active_tab_bg.gif") repeat-x;
    color:#212933;
    }

General information
When you view the code for a page, you will notice that the header code will find out if the page viewed is infact a page. If it is a page, it gets the page parent (I use parent and child pages in my web site organization). The tabs are created with unique li IDs. So that a combination of #home (the body ID) and the #nav-home (li ID) will show as a highlighted tab. With this example, this case will only occur when we are on the home page because #home is a page (the home page) so the home tab will be highlighted. When we click on “SDAC Blog”, the body ID will then be #blog, so now the #blog #nav-blog combination will force the “SDAC Blog” tab to be highlighted. Take a look at the CSS to see where to define the “cases”.

Hopefully this took some of the mystery out of working with WordPress, navigation, and how to put everything together.

Common WordPress Problems and Their Solutions

Ever since we have been listed on the WordPress Consultants list, I have received a number of phone calls from panicked blog/web site owners regarding WordPress errors. Since I have seen a trend in the past month, I thought I would document some of the problems and their solutions.

Error establishing a database connection

Solution: The most common solution is to look and make sure you have typed in the correct user name/password/database name in your wp-config.php file. How can you check? Log into cPanel or Plesk, or directly to mysqladmin. Make sure that the user has the correct privileges to the database and that the password is correct. If you are unsure about the password, you can always delete the user and re-add them (remember to give the newly created user the correct privileges). If you are still having an issue, and you have a high traffic blog/web site, call your web host and see if you are maxing out the server connections – you/they might need to adjust their Apache/MySQL settings or you might need to start using WP-Cache to help control the connection issues. More than likely – you just entered in your user name/password/database incorrectly. This is by far the most common issue I have seen so far.

The default theme appears instead of your chosen custom theme when making CSS changes.

Solution: This is a known issue and hopefully will be addressed soon. If this happens, log into the WordPress Admin and choose your custom theme under “Presentation”.

You are not able to edit your widgets’ content using Safari

Solution: Grab the widget content that you want and drop it below or above another widget content box in the Widgets area in the WordPress Admin. Once you move it, the edit option will appear. Edit the content, and drag it back to it’s original location.