Posts Tagged ‘php’

Custom Post Types

Okay, if you’re not impressed with WordPress yet, you’ve got to take alook at 3.0. The development team has really made some significant updates for power users. Those who love to develop wordpress sites, like we do, you’re gonna love having the ability to create your won custom post types.

To implement this most excellent feature, just paste the following into your themes functions.php file and tweak to your delight.

register_post_type('products', array(
	'label' => __('Products'),
	'singular_label' => __('Products'),
	'public' => true,
	'show_ui' => true,
	'capability_type' => 'post',
	'hierarchical' => false,
	'rewrite' => false,
	'query_var' => false,
	'supports' => array('title', 'editor', 'author')
));

Can’t get much easier than that. Having this ability will allow us, as Wrodpress developers, to create more custom user interfaces for the content management systems we produce. Have fun with it!

PHP Query String Help

Query strings play important role in building web applications, especially if you want to make nice urls without question mark and many key, value pairs, actually it is not so easy to design application with nice urls however it is always worth doing so, because it not only looks very proffessional, such URLs are search engine friendly, which means that they will get indexed faster but also it happens that search engines have problems with indexing pages with more then 3 key=value pairs in query string.

However, one place where using nice URLs is not neccessary are all kinds of admin panels, there are usually only few admins and more over admin panel never gets indexed so it doesn’t make any sense to make those URLs search engine friendly.

Query string can be accessed thru global array $_SERVER, more specific $_SERVER[‘QUERY_STRING’] is the actual variable where query string is written, this variable contains all data that is inputed after question mark in the URL. For example if we have URL which looks like this:
http://someurl.com/page.php?a=1&b=2&c=3

Then echo $_SERVER[‘QUERY_STRING’] will display: a=1&b=2&c=3, such data is no use to us, we need to parse it, or get it thru global array $_GET, in our case we could write:

echo $_GET['a'];
echo $_GET['b'];
echo $_GET['c'];

which would output:

1
2
3

Two things i have to mention here:
– i do not recommend displaying variables passed by user withut checking if they contain potentially dangerous code.
– make sure you have register_globals set to off in your ini file. If you do not have access to ini file, you can change this setting in your .htaccess file (if you have htaccess files working on your server). To do this just add following line php_flag register_globals off.

Using $_GET array to access variables from query string is pretty simple, however we would want to transform our URL to make it look like this:
http://someurl.com/1/2/3
This is very search engine friendly approach, however like always there are no roses without thorns. First problem is that $_SERVER[‘QUERY_STRING’] variable is empty now, so $_GET array is empty as well.

But first thing first, we can’t just input this URL in web browser address bar, hit enter and wait for a page to load. Probably it would end with 404 Error. To avoid this we need to create .htaccess file in the same directory as page.php file. Then open .htaccess file and write in it:

RewriteEngine on
RewriteRule .* page.php

Now all requests to directory which contains file page.php will be redirected to page.php file. Pretty simple and we can now use URL without question mark or any other unwanted signs i provided earlier (e.g. http://someurl.com/1/2/3).

So as i said earlier now our $_GET array is empty, but fortunately we can still check what the URL looks like by writing this code:

echo $_SERVER['REQUEST_URI'];
// output /1/2/3

Well, maybe URL looks pretty search engine friendly however id does not provide a lot of info as it did earlier. Unfortunately solution to this problem is beyond the scope of this article and i will give you only few guidlines: first you need to parse URL wit explode() to get array, then it will be good to assign some keys to each of the variable, but you always need to remember who is who, do not change order of parameters in the URL.

I covered only two types passing arguments to application with query string, there are other solutions as well, but i think that this two are the most common used and are actually the best when it comes to PHP.

Free PHP Form Generator

For those of you that get bitten by the lazy bug from time to time, here is a great tool to use to generate php based forms:

phpFormGenerator

Its a little tricky to get the hang of, but a nice little tool to play with when you have a lot of projects happening at once and need to free up some time by not coding your own form pocessing with validation.  I would recommend placing the entire form directory on your site and call the form using an iFrame.  This seems to be the cleanest way to do things and will save you about 3 hours of configuration if you try to pull everything apart and disect it.  One other thing worth noting; the phpFormGenerator system does not save your form so you can’t go back and edit it.  I would recommend keeping a browser tab open with your form generator (with generated form) in it until you’ve completely finished with the integration of the form into your site and all is tested and working properly.  If not, you’ll find that you’ll have to remake the entire form scratch.

Enjoy it, but be careful not to cause yourself more work by not paying attention…

How to Hide Other Users' Posts in the Admin Panel of WordPress

The following post is a little outdated, the latest versions of WordPress do this for you.  We’re gonna keep this around just in case some folks are using older versions of WordPress, but we recommend upgrading for security purposes and, of course, all the cool new features!!

I am working on another WordPress-based system currently, and have faced a problem when I have many users who can log into WordPress admin panel to post their blog entries. But doing so, they all can see each others posts, even though not being able to edit any post but their own’s.

From users point of view, it is not the best thing in the world to look for your own posts in the list, especially if there are lots of users in the system.

So, here’s a simple solution for the problem – after logging into the system, non-admin users will be able to only see their own posts in the Admin -> Manage panel. The only thing is that I seems no plugin solultion is possible so you have to add 3 lines to one file.

The file you have to add these lines is located in /wp-admin/edit.php file in your WordPress installation. You have to go to line 150 (or near it, where it says:

if ($posts) {
$bgcolor = '';
foreach ($posts as $post)…..

..etc

Now, just add the following code ABOVE the one mentioned above:

if ($userdata->user_level<10) {
    $posts = query_posts("author=".$userdata->ID);
}

So, in the end you will get something like that:

<?php

if ($userdata->user_level<10) {
    $posts = query_posts("author=".$userdata->ID);
}

if ($posts) {
$bgcolor = '';
foreach ($posts as $post) { start_wp();
$class = ('alternate' == $class) ? '' : 'alternate';

You’re done! Now all non-admin users will see only their own posts :) Simple and easy. Have fun.

PHP and Classic ASP Includes

PHP

<?php include("menu.php"); ?>

Classic ASP

<!--#include virtual="menu.asp"-->