Category: ExpressionEngine

Search function for the User Groups module for ExpressionEngine

I wrote a quick search function for the User Groups module for ExpressionEngine. It searches groups (names and descriptions) as well as topics and topic replies. It respects group types as well - results from private groups will not show up in the topic or topic replies search results. You can download it here or see it here.

It’s very simple and can probably be optimized, but it works.

Let me know if you have any questions or comments. I’ll probably end up updating it as new versions of the User Groups module come out.

The search form I use with it is below:

<form id="group_search" method="post" action="{homepage}/groups/search/">
<
input type="text" id="group_search_box" name="group_search_box" />
<
input type="hidden" id="member_id" name="member_id" value="{member_id}" />

<
input type="radio" name="search" value="search_discussions" id="search_discussions" checked>Discussions
<input type="radio" name="search" value="search_groups" id="search_groups"Groups<br>
<
input type="submit" value="Submit"/>
</
form

Posted on Nov 19, 2009 - 07:15 PM

How to create categories from the frontend in ExpressionEngine

I figured out a nifty way to let users on the frontend create categories in the backend. (This isn’t currently possible with the SAEF.) It’s a little bit of a hack, but it works - if you have improvements upon it, please let me know.

You’ll need two templates: one with the form that the user submits with the category name and description (I’ll call this the index template), and one with PHP enabled (parsed on input, NOT output), which I’ll call the create template. In this instance, I’m creating a groups equivalent for users based on categories.     

Here’s the entire contents of my index template:

<form action="{homepage}/groups/create/" method="post" name="group_create">
<
p>Group Name
<input type="text" name="group_name">
<
br />
Group Description
<
input type="text" name="group_description">
</
p>
<
input type="submit" value="Submit" />
</
form

And here’s what I have in the create template:

<?php

global $DB$REGX;

$_POST $REGX->xss_clean$_POST );

$cat_query $DB->query("SELECT AUTO_INCREMENT FROM information_schema.tables WHERE 
table_name='exp_categories' AND TABLE_SCHEMA='your_db_name' ORDER BY Auto_increment DESC LIMIT 1"
);
foreach(
$cat_query->result as $row){}

$cat_id 
$row['AUTO_INCREMENT'];
$site_id "1";
$group_id "4";
$parent_id "0";
$cat_name=$_POST['group_name'];
$cat_title $REGX->create_url_title($cat_name);
$cat_description =$_POST['group_description'];
$cat_image "";
$cat_order "0";

//Creates the SQL query: see http://expressionengine.com/docs/development/usage/database.html
$data = array('cat_id' => $cat_id'site_id' => $site_id'group_id' => $group_id,
 
'parent_id' => $parent_id'cat_name' => $cat_name'cat_url_title' => $cat_title,
 
'cat_description' => $cat_description'cat_image' => $cat_image'cat_order' =>
 
$cat_order);
 
$sql $DB->insert_string('exp_categories'$data);

//echoes the SQL query instead of inserting it - uncomment $DB->query($sql); to make it live.
echo $sql;
//$DB->query($sql);
?> 

A couple of notes: I hard-coded the group_id, site_id and parent_id as well as a couple of other fields, because that’s how I wanted the categories created by users to be. Also, I’m certain there’s a better way to get the next cat_id for the query than using this ridiculous SQL query:

SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name=‘exp_categories’ AND TABLE_SCHEMA=‘your_db_name’ ORDER BY Auto_increment DESC LIMIT 1”);

If you know of a better way, let me know - I’m all ears. In the query, be sure to replace “your_db_name” with your actual database name.

Needless to say, be careful where you use this: you don’t want a spam bot or malicious user coming through and adding a bunch of junk to your categories table.


Posted on Nov 14, 2009 - 07:24 PM

A Quick Comment Spam Avoidance Technique for ExpressionEngine

I’ve been noticing some slightly strange search engine keywords used to find this website in my Google Analytics account. People are reaching my website by searching for things like “please enter the word you see in the image below”, “remember my personal information”, and “notify me of follow-up comments”.

These searches come from bots using Google to look for pages with comment forms on them in order to leave comment spam. Phrases like “notify me of follow-up comments” are likely to be on a comment page, particularly because that exact phrase is part of ExpressionEngine’s documentation for the Comment Entry tag.

So - and you should note that I haven’t (yet) done this myself - a simple way to cut down on random spam comments would be to change the wording on your comment pages. Make it be something just as easy to understand, but just don’t echo the generic EE-specific wording used in the documentation.

EDIT 9/17/09:

I made the comment form changes when I got a spam comment on an entry I had posted just half an hour ago. Checking my server’s logs, I found that he found my site by searching for

family “remember my personal information” “notify me of follow-up comments”

My site was on the top page of results.

EDIT 2/17/2010:

I’ve actually decided to disable comments on this particular weblog entry, because it has all the keywords for spammers.


Posted on Jul 01, 2009 - 06:20 PM

Weblog Entry Date Offset in ExpressionEngine

I recently had a client ask me to have their “Blog Archive” page display all of their previous weblog entries - except for entries posted in the last week.

That seems like a pretty reasonable request, right? Weblog entry display dates should be able to be sliced and diced however the user should want, with a minimum of work. Unfortunately, looking through EllisLab’s extensive weblog entry tag parameters documentation, I couldn’t find anything like a date offset function. The closest exp:weblog:entries paramaters I could find were the start_on and stop_before parameters. The problem is, the stop_before parameter would have to be dynamic: it would have to be consistently set to seven days before the current date.

I was able to solve that problem with a little PHP - read on for the code.

Here we go: this is the opening exp:weblog:entries tag I used to start the weblog entry display.

{exp:weblog:entries weblog="blog" orderby="date" pagination="bottom"
start_on="2009-04-26 00:00 " stop_before="<?php $datesubtract = time() - (60*60*24*7);
$datemin1 = date('Y-m-d H:i', $datesubtract); echo $datemin1; ?>" 
status="open"

This displays all weblog entries from the start date - April 26th, in this case - and seven days before the current date. To change the date range, change the “7” in the PHP to the number of days you want.

It’s that simple. Make sure to enable PHP for the template you put this in, and make sure that PHP for the template is set to be parsed on input, instead of the default output.

Let me know in the comments if you have any questions!


Posted on Jun 02, 2009 - 01:51 PM

One More Thing to Try if Removing index.php Doesn’t Work

If you’ve just moved your ExpressionEngine installation to a new server and suddenly find that your .htaccess index.php removal doesn’t work, here’s one more thing to try.

Just add a question mark at the end of “index.php” on the last line of your .htaccess file. (If you already have a question mark there, try removing it.)

The last line of your .htaccess file should look like

RewriteRule ^(.*)$ /index.php?/$1 [L] 
if it looked like
RewriteRule ^(.*)$ /index.php/$1 [L] 

before.

Removing/adding the question mark should work regardless of whether or not you’re using the include or exclude method to remove index.php.


Posted on Mar 22, 2009 - 11:45 AM

ExpressionEngine Code Snippet: How many weblog entries have been posted in the past 24 hours?

This is a relatively simple question: how can you display how many weblog entries have been posted in the last 24 hours? It turns out there isn’t a pre-made ExpressionEngine way to do this, but if you copy and paste the code below in to your template, you can do it yourself.

{exp:query sql="SELECT COUNT(entry_date) AS entry_count FROM exp_weblog_titles WHERE (NOW() - INTERVAL 24 HOUR) < FROM_UNIXTIME(entry_date) AND status='open'"}
                
<p>Welcome to MySite.com&hellip{if entry_count == '1'}There has{if:else}There have{/if} been <a href="{path="blogs"}"><strong style="color: #FF0000;">{entry_count} new</strongblog {if entry_count == '1'}entry{if:else}entries{/if}</aposted in the last 24 hours.</p>{/exp:query} 

To prevent certain weblogs from being included in the count, add “AND weblog_id !=‘36’” - replacing “36” with your weblog_id number.

The conditionals change the wording to make sure that the sentence remains proper English grammar.

This code when viewed in a browser will result in: “Welcome to MySite.com… There have been 22 new blog entries posted in the last 24 hours.”


Posted on Mar 04, 2009 - 11:15 PM

New ExpressionEngine Subreddit

I’ve started a new ExpressionEngine subreddit. If you’re a Reddit user, go to http://www.reddit.com/r/expressionengine to subscribe and to submit content. Considering the state of the PHP subreddit, I’m not particularly expecting /r/expressionengine to really take off… but I do think it could be useful to some people.


Posted on Jan 19, 2009 - 03:52 PM