Using Custom Fields in Comments

For a private wordpress site I have been using a custom comments table. It just has an added row where people rate the business. Everything is included in the comments table where I don’t need to make a custom table for the ip address, the name, blah blah blah. So now with wordpress 2.9 they have added custom fields for comments so this eliminates the use of me having to do this, YAAAA!

Using custom fields for comments is just like using custom fields for wordpress.

So instead of using: update_post_meta('postid', 'customfield', 'customfieldvalue');

We now use: update_comment_meta('postid', 'customfield', 'customfieldvalue');

So in your plugin or functions.php file you can just do this:

add_action('comment_post','comment_rating');
function comment_rating($comment_id) {
global $wpdb, $user_identity, $user_ID;
$star = $_POST['rating'];
update_comment_meta('postid', 'rating', $star);
}

Again you need to add a rating drop down in your comments.php file (example): <input type=”radio” name=”rating” id=”vote1″ value=”1″ />

Posted in PHP, Wordpress | 3 Comments

Removing first image from the_content

I recently had a client want to put the_title below the first image. Without making it to complicated by adding custom fields or anything like that. I thought that I could grab the first image from the content with catch_that_image and then display the title and then display the_content. But if I did it that way, it would display the first image twice. So I had to come up with a way to remove the first image from the_content here comes filters!

// This is the filter that gets added to the content
add_filter( 'the_content', 'remove_first_image' );

// This is the function name
function remove_first_image($content) {
global $post, $posts;

// This is the pre replace that removes the first image
$content = preg_replace('//i',$matches,$post->post_content,1);
return $content;
}

You can see a working example at Barack Obama. The first image is called via catch_that_image and then the_title is displayed and then the_content after that.

This way Casey just keeps doing what he is doing without having to change any of the posts around that he already has done.

Posted in Wordpress | Tagged , , , | 4 Comments

Add Custom Field to Register Form

this is gonna be a quick one, cause I need to finish website. But I got this working today. This is for function.php file or in a plugin will work. This will add two forms to the register page in your wordpress, First Name and Last Name.

add_action(‘register_form’,'show_first_name_field’);
add_action(‘register_post’,'check_fields’,10,3);
add_action(‘user_register’, ‘register_extra_fields’);

function show_first_name_field(){
?>
<p>
<label>First Name<br/>
<input id=”user_email” type=”text” tabindex=”20″ size=”25″ value=”<?php echo $_POST['first']; ?>” name=”first”/>
</label>
</p>
<p>
<label>Last Name<br/>
<input id=”user_email” type=”text” tabindex=”20″ size=”25″ value=”<?php echo $_POST['last']; ?>” name=”last”/>
</label>
</p>
<?php
}

function check_fields($login, $email, $errors) {
global $firstname, $lastname;
if ($_POST['first'] == ”) {
$errors->add(‘empty_realname’, “<strong>ERROR</strong>: Please Enter in First Name”);
} else {
$firstname = $_POST['first'];
}
if ($_POST['last'] == ”) {
$errors->add(‘empty_realname’, “<strong>ERROR</strong>: Please Enter in Last Name”);
} else {
$firstname = $_POST['last'];
}
}
function register_extra_fields($user_id, $password=”", $meta=array())  {

$userdata = array();
$userdata['ID'] = $user_id;
$userdata['first_name'] = $_POST['first'];
$userdata['last_name'] = $_POST['last'];
wp_update_user($userdata);
}

Posted in Wordpress | 24 Comments

Why Include a Logout Button?

So I am making a site where users can create an account on this site. It is not an important site where multiple users are going to be using the site, where there is a need for a logout button. So I am not going to include a logout button anywhere on the site. I just don’t see a use for it at all, unless someone will tell me different.

Posted in Clients, Wordpress | 1 Comment

Check If Logged in WordPress

I put this file just below the header.php in my themes for sites that require the user to be logged in to do anything

<?php
if (is_user_logged_in()){
    echo "Welcome, registered user!";
}
else {
    echo "Welcome, visitor!";
};
?>
Posted in Wordpress | Leave a comment

Page Last Edited By….

I wanted to show when a post was last edited and who it was last edited by. If there s a function for this, I don’t know, but I found this on the wordpress forums and it works great.

<?php

$post_ID = get_the_ID(); 

if ( $last_id = get_post_meta($post_ID, '_edit_last', true) ) {
	$last_user = get_userdata($last_id);
	printf(__('Last edited by %1$s on %2$s at %3$s'), wp_specialchars( $last_user->display_name ), mysql2date(get_option('date_format'), $post->post_modified), mysql2date(get_option('time_format'), 

$post->post_modified));
};

?>
Posted in PHP, Wordpress | Leave a comment

Putting the_content into PHP

With this code you have the ability to remove images or remove the text and only show the images, or a flash embed code, with preg replace or however you want to.

<?php $content = get_the_content() ?>

Below will remove all images from your post and only display text:

<?php $no_images_content = preg_replace('/<img[^>]+./','', $content); ?>

And then you just echo out the php variable and no images will be displayed:

<?php echo $no_images_content; ?>
Posted in PHP, Wordpress | Leave a comment

When Upgrading WordPress Always Backup

This happened to me today, And thank the gods I randomly had a backup of the custom theme folder. I had updated some function calls to make it load a bit faster, and I lost those when I decided to upgrade to the latest version of WordPress 2.8.1.

I had manually installed WordPress 2.8 (which I thought was the latest version that I downloaded), but was not. After that upgrade the site worked perfectly and I wasn’t thinking and I clicked on Upgrade to 2.8.1. Whoops, lost my themes folder.

But always backup when upgrading, It will save you a lot of headaches in the future. And just always backup randomly. You never know, and it doesn’t hurt.

Posted in Wordpress | Tagged , | Leave a comment

Idea; Use WordPress as RSVP System

So I had a friend who was looking for a simple to use RSVP system. I spent half today looking into ways to use custom fields as  a way to track names and emails. Thats a good way if you don’t need to count plusses. But I needed a way to count plusses and assosiate the name with an email address. So using custom fields is out of the question.

So then I decided that instead of using custom fields, why not just use the comment system and edit the comment box to be a pull down menu.

So how it works as of now, is everytime there is a new post in the event category. The comment field is changed to a drop down list of plusses one to three. And we don’t want the rsvp’s to be shown to regular users and to only the admin, so I added this line to view comments, so only comments are shown to the admin:

<?php if ($current_user->ID == 1){ ?>
show link or text
<?php } ?>

I am making a backend that will show the name and rsvp’s in alphabetical order.

Just an idea.

Posted in Wordpress | Tagged , , , | Leave a comment

Client Tip, Do the small things first

I work with a lot of people and I am not the best person to talk to. I usually work best when the client sends me a list of things to do. So I can just check them off. So when I am talking on the phone or in person with a Client, I make a handwritten list of things to do. So I just cross them off of the list.

But every once and awhile I want to wait till the very end to do something because it is time consuming and will take me awhile to do, or they are generally small thing and not a high priority on my list.

Well when a client says something, that means just do it. And I am starting to generally do things in the order that the client tells them to me. They usually tell me what they want changed first for a reason. Because it bothers them the most!

So Client tip #1, Do the things in order that the client asks them to be done. This will make them happy, and thats all we want is a happy client.

Posted in Clients | Tagged , , | Leave a comment