Archive for the ‘PHP’ Category

Tabbed Navigation in WordPress

I encountered this problem when I’m developing my first wordpress theme, a very simple problem and I want to share this tutorial to all of you. Hopefully it will give you an basic idea on how to make a tabbed navigation in you wordpress theme.

Now, in our template file where our navigation is located(in my case, header.php). We will paste the code below:

24
25
26
27
28
29
30
31
<ul id="nav">
  <li <?php echo(is_home() ? 'class="current_page_item"' : '');?>>
    <a href="<?php bloginfo('url'); ?>" title="Home">Home</a>
  </li>
  <?php 
     wp_list_pages('sort_column=menu_order&title_li=&depth=-1');
  ?>
</ul>

The style:

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#nav {
  list-style-type: none;
}
#nav li {
  display: inline;
  font: bold 12px arial, sans-serif;
}
#nav li.current_page_item a{
  background: #60a013;
  color: #fff;
}
#nav li a{
  color: #333;
  padding: 4px 10px;
  text-decoration: none;
}
#nav li a:hover{
  background: #60a013;
  color: #fff;
}

The code above will give the result, a simple tabbed navigation with an :active and :hover effect:

wp-tabbed-final

Notice that we add a “Home” link before we call the pages, to determine if the additional link is active we use the conditional tag is_home(). The is_home() returns true when the main blog page is currently being displayed, in our code if it’s true then it well print the class="current_page_item" else it will print nothing. More information about the conditional tags can be found here.

And we display the pages link using the wp_list_pages() template tag. We can customize the output of our links by using the parameters sort_column=menu_order, title_li='' and depth=-1.

The sort_column=menu_order can sort the list of pages in a different way. In our code we will sort the pages by Page Order. Page order can be set by user in the pages administrative panel. Next is title_li='', this will not display headings and the list will not be wrapped with <ul>...</ul>. And last depth=-1, will display the sub-pages as flat, the same as the other pages with no indent form. More information about the wp_list_pages() template tag can be found here

Ta-da-da-da! Operation Reboot

Hello, from niflheim. I just noticed that my blog went hiatus. It’s been awhile since my last post. I’ve been busy lately, busy watching random anime series and busy playing random video games, I even forgot my work. Anyways here are some wallpapers and stuff that I haven’t publish in my blog for these past months:

dsanta-san

D. Santa-san is a wallpaper I created right after my third Gig Poster Design. You could say that it’s the wallpaper version but it has nothing to do with the gig. This is for chanlu’s Christmas update.

Download: D. Santa-san 1440 x 900

see-no-evil

This wallpaper is a collaboration work with Charlotte, The character was done by her and everything except the character were made by me. This wallpaper has a pair, the other is here, a girl.

Download: See No Evil 1440 x 900

twitty

Six months ago, I started liking Twitter, that’s the story. My incomplete twitter API library for PHP called twitty, just a little project and my first time in writing such library. It’s still in development though.

Download: Twitty for PHP

Twit with PHP[updated]

twi^&@#t!While my brain cells are doomed to extinction in writing my pangram, I’ve written a simple tutorial on how to send a twitter updates using PHP. And I think this will be good while I’m waiting for my brain cells to regenerates.

This tutorial requires CURL library installed for PHP, it’s use for communicating with the Twitter API. Here is the demo of the script.

Read at the bottom for updates.

So, let’s begin:
First we declare the following variables.

1
2
3
4
5
$user = 'username';
$pass = 'password';
$twitter = 'http://twitter.com/statuses/update.xml';
$status = 'Set your Status here';
$status = trim(urlencode(stripslashes($status)));

The variable $user and $pass are of course your username and password in your twitter account. The $twitter is the URL of the updates for the Twitter API with the status parameter, $status is where the status/update will be store.

Then, let’s do the curl. We will create a curl session.

6
$curl = curl_init();

Set the necessary options for our session. This is where our variables will be needed.

7
8
9
10
11
12
curl_setopt($curl, CURLOPT_USERPWD, $user.':'.$pass);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, $twitter);
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=".$status);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Expect:"));

The first option, the CURLOPT_USERPWD is where you define your username and password in a format of [username]:[password] to use for our request. Next the CURLOPT_POST, we need to define this as true because the request must be a POST. Then the CURLOPT_URL, is where the URL to do our request. And lastly the CURLOPT_RETURNTRANSFER, it returns the value of our curl execution(later) as a string instead of printing it directly to the browser.

We execute and catch the status of our request.

11
12
13
14
15
16
17
18
19
20
21
22
curl_exec ($curl);
$HttpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
switch($HttpCode):
   case 200:
      echo 'Your status has been updated';
      break;
   case 401:
      echo 'Not Authorized: invalid username or password.';
      break;
   default:
      echo 'An unknown error has occured. Try Again';
endswitch;

Here, after we executed the curl, we would like to get the status of our last request with the function curl_getinfo() with the option CURLINFO_HTTP_CODE. This will let us know what our last HTTP code, since the Twitter API returns the appropriate HTTP Status code for every request.

Lastly, we close our session and frees all resources.

23
curl_close($curl);

And that’s all.

The complete code:
Here is the complete code that are describe above. You can use/modify in any way you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$user = 'username';
$pass = 'password';
$twitter = 'http://twitter.com/statuses/update.xml';
$status = 'Set your Status here';
$status = trim(urlencode(stripslashes($status)));
 
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, $user.':'.$pass);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, $twitter);
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=".$status);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Expect:"));
curl_exec ($curl);
 
$HttpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
switch($HttpCode):
   case 200:
      echo 'Your status has been updated';
      break;
   case 401:
      echo 'Not Authorized: invalid username or password.';
      break;
   default:
      echo 'An unknown error has occured. Try Again';
endswitch;
 
curl_close($curl);

Updates for HTTP/1.1 clients

I’ve check twitter API and there are a lot of changes ever since I write this post, so here are the updates:

Since it is a post operation, we’ve set

10
curl_setopt($curl, CURLOPT_POSTFIELDS, "status=".$status);

And for the HTTP/1.1 clients thats getting an Http response of 100, we’ve overwrite the expectation to empty in our header when sending our request. Link

13
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Expect:"));

Comment out, if your’re having problem.

Download || View Demo

Return top

About

Hello, my name is Rogelio Calamaya a web designer and developer based in Philippines

Elsewhere