Tips For Search.php and Its Searchform Script in WordPress

The more you twiddle with WordPress the more you will learn about it. Makes sense, yes? Read on for a couple tips on coding and styling the WP search function.

Check out these search function tricks for your WP blog:

  • Use the right path in the call to the searchform.php. While you want to search all the important text on your blog, you don’t want your visitors to search your entire server. Some pages may have <?php echo $_SERVER ['PHP_SELF']; ?> as the value for the action attribute of the search form and that could enable a search of the entire server. Restrict the search to the content of your blog by using <?php bloginfo ('home'); ?> instead.
  • Modify search.php to allow for personalization or styling of the search output. Wrap a div around the output to be able to stylize the content with a new id or class. For instance, the following code assigns the value of the variable $s to the value of the search term with any special characters stripped from it. The code also specifies the leading text that appears when search results are returned to the screen. Try searching for a term in this blog to see the output.

<?php $s = wp_specialchars($s, 1); ?>
<div id="search-phrase">

<?php _e('We found the following posts that matched your search criteria: ' . "$s"); ?>
</div>

  • Target the search results in style.css with the new id or class. Applying an id to the wrapping div lets you target the leading text of the search results in your style.css stylesheet. In the example above the stylesheet could be used to style div#search-phrase to draw the reader’s attention.
  • Use wp_specialchars() when calling for variables returned by searchform.php. Echoing <?php echo $s; ?> is a bad practice because it may allow malicious code injection. When returning values from any database make sure to use wp_specialchars() to first strip any “special characters” before outputting to the screen.

Keep reading and learning!

Read, Write and Execute Permission Settings for Apache Servers

In the Unix and Apache world three settings will allow or disallow scripts from working. The right permissions must be set on any script or file that needs server help to be read, written to or executed. The files to which scripts may be writing need correct permissions to allow the script access.

Files may be readable, writable and/or executable. The permission settings are always referred to in this order, read-write-execute, or r-w-x.

Each of the three permission’s settings can be applied to three sets of users, namely owner, group and world. So, if you see a directory listing of a cgi script, for example, you will see three sets of permissions that represent the capabilities of each type of visitor — owner, group and world, in that order — with respect to that file.

For example, say that my file, test.cgi, has the following line in a directory listing:

- rwxr-xr-x 1 jen users 2134 Sep 12 9:42 test.cgi

The first set of rwx refers to the owner permissions, the second set of r-x refers to the group permissions, and the third r-x refers to the world permissions. We say that the permissions for the file test.cgi has permissions of 755.

How did we get to 755 from rwxr-xr-x ? Each permission has a number associated with it like so, r = 4, w = 2, and x = 1. In this example the owner has read-write-execute permission, and 4 + 2 + 1 = 7. The group and world settings do not include write permissions so both of those permissions add up to 5, for a file permission’s setting of 755.

Changing the permissions of a file is easy with your FTP client. Right-click on a file name and select change permissions or CHMOD, which stands for “change mode” or change file permissions. Then it is a simple matter of selecting your desired read-execute, r-x, or 755.

Theme Description for WordPress Blog Lies in the Stylesheet

So, you’ve embarked on creating your first WordPress theme. Lots of questions arise, like “Where the heck do I put a description for my new theme?

Open up the stylesheet, style.css, and insert several commented lines at the beginning like so:

/*
Theme Name: My First WordPress Theme
Theme URI: https://computeraxe.com/
Description: This is my first wordpress theme.
Version: 0.1
Author: LizzyFin
Author URI:
https://computeraxe.com/
*/

Everything after the “:” is cataloged by WordPress as theme metadata, or information about your theme.

The theme name, description and version will appear in the Admin/Presentation/Themes panel to describe the WP theme. A separate screenshot of the functioning theme completes this picture.

The author’s name and URI, as well as the theme URI where one can see the theme in action, are important details to include at the top of every WordPress stylesheet.

Permalinks Totally Screwed Up When Changing From Default Structure

Permalinks are the URLs that refer to your WordPress pages, posts and categories. Essentially, the links to your blog pages and posts are permanent links or permalinks.

Each post or page created in WordPress is identified with an ID number. Default URLs use the ID number to differentiate between individual pages and posts. An example URL would be something like https://computeraxe.com/?p=123 for the post or page that has ID number 123.

The problem with this default permalink structure is that nobody can remember which posts the numbers represent. To make your blog URLs more meaningful you can change the permalink structure from the default structure to a more useful one.

This is a great tip for anyone interested search engine optimization for their blog.

In your admin area go to Options/Permalinks. At the top of the page you’ll see the default ID permalink style selected. Choose another style that you like and then click on Update Permalink Structure to make the changes stick. Now, if your blog is fresh out of the box and you haven’t already made posts with it, then you’re set to go.

I changed my permalinks to a custom structure like so, /%post-name%/ , and when I went back to view my site, all the posts and pages were gone!

What happened here? The server could not find my existing pages and posts because it was looking for them with the old numerical names. Instead of recreating the content, changing page names, or reverting back to the old permalink structure, there is an easier way to fix this issue.

One way to solve this problem of disappearing posts or missing pages is to make the following entry in your htaccess file in the public_html folder of your domain.

Options +Followsymlinks

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Be aware that htaccess files can exist in any directory on your site.

You want to allow the server to write to your htaccess file as updates are made to your blog. Set permissions or chmod the htaccess file to 666.