Running your PHP app with pow – The Mae’s an idiot guide

Just because the entire exercise has driven me to madness (almost) where I was wishing I could literally do a table flip and none of my stuff will be broken. At 3AM. I’m afraid my neighbors think someone’s getting slaughtered at my apartment.

Anyhoo! So, you want to run your PHP app locally, eh? You’re in a for a ride! It may include hair pulling and a little bit of screaming – so I suggest you keep your scream jar nearby.


First, I hope you’re pretty familiar with the terminal; and if you haven’t already, you should create a shortcut so you can open sublime through the command line, makes everything so much easier.

Start with this pretty vague tutorial on running pow with apache and know that it may not just stop there.

If that didn’t work for you, time to get down and dirty! Open your apache2 folder in sublime by typing sudo sublime /etc/apache2 it should show all of the files and folders within apache2. If you open your other folder, you should see the zzz_pow.conf file that was created earlier.

Note: if you want to see any changes in your localhost, you should always do a sudo apachectl restart

Moving on, open httpd.conf then go to line 118, uncomment that line to activate the PHP module so it should look like this:

LoadModule php5_module libexec/apache2/libphp5.so

Some tutorials will want you to uncomment Include /private/etc/apache2/extra/httpd-vhosts.conf which will make your virtual hosts file readable by the server, but I prefer to be on the safe side. While doing this darn thing, I had to remember which files I had to edit and what I did to them, so I prefer to have my changes to be in the same folder, in this case it’s all in the other folder. It will all be read anyway! Make sure that your httpd.conf's last line is:

Include /private/etc/apache2/other/*.conf

Create a new file within the other folder called aaa_default.conf (keeping with the original pow tutorial). This will contain your virtual host code(s) that may look something like this:


<VirtualHost *:80>
    ServerName project.dev
    ServerAlias www.project.dev
    DocumentRoot "/Users/username/Sites/project"
    ErrorLog "/private/var/log/apache2/project.com-error_log"
    CustomLog "/private/var/log/apache2/project.com-access_log" common
    ServerAdmin email@domain.com
        <Directory "/Users/username/Sites/project">
            Options Indexes FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
</VirtualHost>

<VirtualHost *:80>
  #no need for settings, use apache defaults.
</VirtualHost>

Replace ‘project’ with your actual project name and username with your account username. Remember that since we already did this, there’s no need for you to create a symlink in your .pow folder. It’s not as nice and simple as the symlink but if it does the job… well…

Next, create another file called php.conf (or php5.conf or whatever) which will contain a piece of code that will tell the server that whatever file that has the extension .php should be executed and not displayed as text.


<IfModule php5_module>
	AddType application/x-httpd-php .php
	AddType application/x-httpd-php-source .phps

  	<IfModule dir_module>
  		DirectoryIndex index.html index.php
  	</IfModule>
</IfModule>

If you got this error:

SQLSTATE[HY000] [2002] No such file or directory

Open your /etc/php.ini in sublime and look for pdo_mysql.default_socket. Mine was located in line 986 and it didn’t have any value. Just append =/tmp/mysql.sock so it will be like this:

pdo_mysql.default_socket=/tmp/mysql.sock

Restart your apache server, hopefully, for the last time and your local PHP app should be available in http://project.dev!

If welder bros still didn’t work, like mine, open your hosts file (/etc/hosts) with sublime (don’t forget to sudo!) and add project.dev and www.project.dev beside localhost, and ta-da! Your PHP is running with pow!

It’s as easy as 10 million steps and lots of bashing! Enjoy! :)

Presenting videos using WordPress

And without using a plugin.

I thought of creating this post when I was on my way home from the recently concluded WordCamp Philippines. There was a woman who was in Karla Redor‘s talk who asked on how she can host the video in her own blog and use WordPress to file it for her. She can use a plugin that will let her upload the videos in her blog or do it the “harder” way. Something that I’m very fond of doing.

Seriously, I’m the type of person who would only resort to using plugins if:

  1. I’m lazy at the time to think of another way without using a plugin
  2. My head is aching and I’m getting frustrated because it just won’t follow what I’m telling it.

The reason is because I am not a developer and it’s really hard to be dependent on a plugin specially if that plugin is what’s keeping your blog alive. Also, if the developer of the plugin suddenly stops supporting the plugin and no one else is willing to take it up from there. What’s worse is if it’s not compatible with the newer version of WordPress. Then I will be lost.

So anyway, on to the “how”.

  1. Convert you video to a .swf file, you can use Hey!Watch, it has a fee though but considering how it will make your life easier, I think it’s worth it. Besides it will convert a video for $0.10 so I don’t think that’s too much.
  2. Next in your code (you’d want to add this in your single.php page), call for the video custom field (which you’ll be adding later on). Note that this should be within the loop. Also, note that the snippet is taken from a WordPress support forum and changed to suit the topic.
    php $video = get_post_meta($post->ID, 'video', true); ?>
    
    
    
     

    What does this mean? The first line sets the $video variable to the value of the video key (of the custom field). Next it checks if the video has a value attached to it (in that particular post). If it does, it inserts the value in the data and value properties of object and param. If the video doesn’t have any values then it won’t display anything.

    This way, it saves you the time to copy and paste the entire code for the video. Now if you want the lazier way, you will have to adhere to some restrictions. For example, you can set the value of data and value to:

    php bloginfo('url') ?>/videos/

    This means that you will only be able to upload videos within the videos folder within your root. That way, you’ll only put the filename of the video in the custom field value.

  3. Once you’ve uploaded your video to your server (within the specified folder of course), go to the admin panel of your blog and then create a post (or a page) in your custom fields area, do what’s illustrated in the image below:

    As you can see, I decided that I want to have another folder within my videos folder. My videos will be categorized according to shows, so the value of my video key is unang_hirit/episode_100.swf. Once published, the post’s HTML will appear like this:

    <object type="application/x-shockwave-flash" data="http://domain.com/videos/unang_hirit/episode_100.swf" width="400" height="300">
      <param name="movie" value="http://domain.com/videos/unang_hirit/episode_100.swf" />
    </object>

Featured Video

If for some reason, you want to create a section that is called the “Featured Video” section on your sidebar, you can call it by using the following lines of code:

php query_posts('category_name=Featured Videos&showposts=1'); ?>
  
    ID, 'video', true); ?>
    
    
    
      

If you’d notice, we recycled the code from above. The only difference is the query_posts tag before the loop. What the query_posts tag does is that it takes a single, recently added post (showposts=1) from a category called “Featured Videos”. And it’s the same business as what was discussed above.

Further reading:

Appending the title of the post in “Read more” links for WordPress

One of the guidelines in usability is that you should provide only one way that will lead them to a certain action. For example, if someone sees 2 option buttons, chances are that they might think that each option button does separate tasks when in fact their functions are the same. Also, we have to make sure that the links are descriptive enough and if not, then we should provide a title for the link so when a user hovers over it then they’ll know where the link will take them.

On a similar note, accessibility advocates encourages people to use unique name for every link that we use on a page (kinda like what I was saying before only have a different reason), meaning countless “Read more of this entry” is not really the best practice because you’re using the same text over and over and these texts are leading the user to different pages.

I was looking for a way to add that in <?php the_content(); ?> tag in wordpress before but I couldn’t do it. What I did then was append the the_title(); within the_content(); so it looked like this —

<?php the_content('Read more of ' . the_title() . ' & raquo;' ?>

The problem though is that it does echo the “Read more of” but the title is no where in sight! I was so ready to find a way to make it show by hacking away through function.php, thankfully I remembered to check the_content page in the codex first.

What I should’ve done in the first place is to add parameters to the the_title() tag so it will show like this —

<?php the_content('Read more of ' . the_title('', '', false)) ?>

This follows the same parameter as the the_title(); template tag.

Parameters

before
(string) Text to place before the title. Defaults to ”.

after
(string) Text to place after the title. Defaults to ”.

display
(Boolean) Display the title (TRUE) or return it for use in PHP (FALSE). Defaults to TRUE.

So you may use it like this:

<?php the_content('Read more of ' . the_title('&ldquo ;', '&rdquo ; » ;', false)) ?>

So it will echo as Read more of “Title of the Post” ».

When ‘is_home()’ doesn’t work

Note: This has been written a long time ago, some of the stuff here might not work anymore. I’d recommend using <?php is_frontpage(); > when using static frontpage instead. You can also visit the WordPress Codex for more information or contact me and I’ll try my best to help you.

You hacked your WordPress homepage because you don’t intend to make a blog out of it and then all of a sudden the very useful conditional tag <?php if(is_home()) ?> no longer works. It happened to me twice and for that 2 occurrences, I couldn’t find a solution until a couple of days ago.

I’m working on this project where I don’t need to show my posts anywhere aside from the category archive and single, not even in the homepage. What the client wants is that the home page should show all of the categories in the site, no matter if it’s empty or not and a certain page content must be pulled into it. Because I’m lazy in creating a page template just for that and assigning that page as the home page, I modified the main index template instead.

Those things are actually easy to do. All I have to do is to write this within the main index template:

<?php wp_list_categories('order_by=name&hide_empty=0&title_li='); ?>

so it’ll show all of the categories ordered by the category name whether it has posts or not. And for the page to be pulled into the main index, query_post is the answer:

<?php query_posts('page_id=ID'); while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); endwhile; ?>

so it’ll show the title and the contents of the page ID.

It worked just as expected and I was so proud of myself until the client asked that the sidebar for the home page be different from the rest of the site. That’s when I knew that the is_home() doesn’t work.

I searched high and low for in the codex and support forum but couldn’t find anything. I can’t tell the client that I can’t fix this so I tried my hunch if it works. I’m not the type who is a big fan of plugins because I’m afraid that if the site rely heavily on plugins and and if and when that certain plugins’s developer goes AWOL, the site might go berserk due to incompatibility and all I could do was search again for a plugin that could replace it.

I thought that the home page is being treated by wordpress as if it’s a page because I did pull the contents of a page into it. Turns out that I was right!

When I put <?php if(is_page('ID')) { ?> in the sidebar, it worked just as I wanted it to.

So now, I realized that once you’ve changed the content of your main index template, know that you are also taking out it being your “home” because it will start acting like a page (if you pulled a page into it).