Archive for the ‘Wordpress’ Category

Wordpress – How to Get the Post ID

Saturday, October 24th, 2009

There are various locations throughout your theme where you might need to get the post ID. For instance you may have a widget in the sidebar that needs the ID or some place in the Header or the Footer that needs the ID. The following is a list of different ways to get the ID.

Inside the loop:
This is a Wordpress function for use in the Loop:

the_ID()

or, You can build a function and put in in functions.php

function getthe_loop_postID() {
     global $post;
     $thePostID = $post->ID;
}

In single.php outside of the loop:
You can build a function and put it in functions.php with

function getthe_single_postID() {
     global $wp_query;
     $thePostID = $wp_query->post->ID;
}

or you can use post->ID within the single.php template

If you are going to use post->ID within the header or the footer but want it to display on the single post page. You can use:

if is_single() {
     echo post-ID;
}

or what ever it is you want to do with it.

You can use other SQL commands with the post->ID query as well.

For Example:
If you want to see if the post is a particular post use:

post->ID WHERE ID = 17

For the most recent post use:

$post->ID ORDER BY post_date ASC LIMIT 1

For the oldest post use:

$post->ID ORDER BY post_date DESC LIMIT 1

Wordpress – Unregister all Widgets

Tuesday, October 20th, 2009

In my investigations to fix a problem with a clients site. I found the need to disable a single widget.

The function below will unregister all of your widgets. Use the following code in your functions.php file and refresh or load a page from your site, after that remove the function from functions.php and your finished.

<?php
     update_option( 'sidebars_widgets', $null );
?>

Wordpress – How to Determine the Installed Version and the Database Version

Tuesday, October 20th, 2009

There are a couple of ways to tell what version of Wordpress is installed on your domain. The one I like to use is www.yourdomain.com/readme.html, if your wordpress is installed at the root of your domain. If your Wordpress is installed in a subdirectory you need to instert that in the URL. This read me page will display towards the top the Installed Wordpress version, if you replace all of the files at the same time. You can also log into your Admin section see the version of Wordpress in the bottom right corner of the footer.

To determine the database version you will need to view your database contents. Navigate to the wp_options table (the prefix wp_ may differ in your database) and locate the field for db_version.

Here’s the list for up to version 2.8.4:

2.8.4 = 11548
2.8.3 = 11548
2.8.2 = 11548
2.8.1 = 11548
2.8 = 11548
2.7.1 = 9872
2.7 = 9872
2.6.5 = 8204
2.6.3 = 8204
2.6.2 = 8204
2.6.1 = 8204
2.6 = 8201
2.5.1 = 7796
2.5 = 7558
2.3.3 = 6124
2.3.2 = 6124
2.3.1 = 6124
2.3 = 6124
2.2.3 = 5183
2.2.x = 5183
2.2 = 5183
2.1.3 = 4773
2.1.x = 4773
2.1 = 4772
2.0.11 = 3441
2.0.x = 3441
2.0 = 3441
1.5.x = 2541
1.x = 2540

See the Installation FAQ on Wordpress.com for further information.

Wordpress – Unavailable Widget Control in Admin Panel – Fix

Thursday, October 8th, 2009

As you saw from my early post, Wordpress – Unavailable Widget Control in Admin Panel. I had a client who had an issue with the widget control panel not displaying correctly.

This was a 2 part problem. Some how a widget ended up broken and was causing the page to not completely display all of the widgets nor the sidebars and their content widgets on the right side of the control panel. The second part of the problem came from the screen option on the widget control page. Some how the option was deactivated and this made the page work like the java script was disabled in the browser or just not loading correctly.

I do recall clicking on the screen option while the page was broken and nothing changing, I’m not completely sure if this caused the option to be deactivated, but none the less this problem was a headache and is now resolved.

So to fix a messed up widget control panels. Disable any widgets that are ran by plugins or any custom widgets that may cause any issue of the page not completely loading. Second check to make sure your screen option is active.

Wordpress – Unavailable Widget Control in Admin Panel – Problem

Friday, October 2nd, 2009

I ran into a serious problem tonight, a recent change that was made to a clients site caused a traffic lose and so they restored the site back to a previous database and theme. When you change the theme with the widgets enabled there is a chance you will not gain access to the sidebar widget control from the Wordpress Dashboard.

So not knowing how to get the sidebars back under control from the dashboard. I dove into the database to figure out how the active widgets are stored in the database. It didn’t take long for me to find that the Options Table has a record for all of the widgets and which ones are active and which sidebar they belong to.

Record number 82 in the Options Table is where this data is stored in my database. Here is an image of that record:

wordpress options table widget record

Once I found the record I clicked on the pencil to edit that record. In there I found all the data I needed to disable the widgets I needed. Here is the data that was in the options_value field of record 82.

a:4:s:19:”wp_inactive_widgets”;a:9:{i:0;s:7:”pages-2″;i:1;s:10:”calendar-2″;i:2;s:10:”archives-2″;i:3;s:7:”links-2″;i:4;s:6:”meta-2″;i:5;s:6:”text-2″;i:6;s:17:”recent-comments-2″;i:7;s:5:”rss-2″;i:8;s:11:”tag_cloud-2″;}s:9:”sidebar-1″;a:2:{i:0;s:20:”categories-436616501″;i:1;s:14:”recent-posts-2″;}s:9:”sidebar-2″;a:2:{i:0;s:11:”adsense-ad2″;i:1;s:11:”adsense-ad1″;}s:13:”array_version”;i:3;}

The registered sidebars are identified by sidebar-1 and sidebar-2. Immediately following the sidebar identifier is an a:2 for both of my sidebars. The number 2 identifies how many widgets are in that sidebar. The widgets that are in the sidebar are contained between the {}, brackets.

So to remove all of the widgets make the widget count to 0 and remove all data between the {}, brackets:
“sidebar-1″;a:0:{}

To remove only one widget from the sidebar, reduce the widget count by 1 and remove all of the data for the preceding i all the way to the ;
“sidebar-1″;a:1:{i:1;s:14:”recent-posts-2″;}
*notice I changed the widget count from 2 to a 1 and I removed the categories widget from inside the brackets

The complete code for the options_value field with no active widgets for both registered sidebars would look like this:

a:4:{s:19:”wp_inactive_widgets”;a:9:{i:0;s:7:”pages-2″;i:1;s:10:”calendar-2″;i:2;s:10:”archives-2″;i:3;s:7:”links-2″;i:4;s:6:”meta-2″;i:5;s:6:”text-2″;i:6;s:17:”recent-comments-2″;i:7;s:5:”rss-2″;i:8;s:11:”tag_cloud-2″;}s:9:“sidebar-1″;a:0:{}s:9:“sidebar-2″;a:0:{}s:13:”array_version”;i:3;}

I made some mistakes when editing the data in this record and when I did so. Wordpress created a new sidebars_widgets record. This will cause you to not see any further changes to the original record. You will need to navigate to the last record in the options table and remove the new sidebars_widgets record then make the proper corrections to the original record.

The problem with the Admin Control of the widgets is not resolved at the moment. And the solution is unknown. All I was able to do for my client was remove the unwanted widget which surfaced this problem in the first place. I now have a better understanding of how the widget control is stored in the database and I hope to sort this issue out soon.

Look for a future post on the resolution of this problem or drop me a line if you have any info or feedback.