Display content only to logged in users

by Terran on April 24, 2010

This is something I just did for one of my sites that I think is really neat, so hopefully you will find it useful.

Like almost all my customizations, this one is done in the Thesis Framework. With the exception of the hook to place the content at the bottom of the post or page this should work in pretty much the same way for any wordpress theme. It’ll just be a bit harder.

Basically it adds content to the bottom of a post or page based on your input in a custom field, but only for logged in users. It also displays a message telling non-logged-in users that they’re missing out on something.

Here’s the code which goes in custom_functions.php (usually just functions.php in other themes):

function add_loggedin_content () {
global $post;
$loggedinmessage=get_post_meta($post->ID, 'logged_in_content', TRUE);
if ($loggedinmessage) {
if ( is_user_logged_in() ) {
echo do_shortcode( $loggedinmessage );
}
else {
?>

This page contains content that only logged in users can view. If you are registered click here to get access. To register click here.

}
}
}

My application is to display a contact form only to logged in users. Since I'm using contact form 7, I wanted to be able to insert a shortcode in the custom field. If you wanted to insert regular content just change:

echo do_shortcode( $loggedinmessage );

to:

echo $loggedinmessage;

To use this function just go to the post/page, scroll down to "Custom Fields" and click on "Enter New." In the "name" field enter:

logged_in_content

Then enter whatever shortcode or content you want in the "Value" field.

So what uses can you think of to put this code to use for?

{ 0 comments }

Creating a Separate Newsletter Page

by Terran on March 5, 2010

Ever wanted to create a separate “newsletter” page for your site that’s segregated from your main blog? This tutorial will walk you through doing just that for your wordpress site running the Thesis Framework. It should be similar in most other themes too.

This post comes to you courtesy of Angelique at AfMarCom.com who I worked with to figure this out.

Step 1: Create Your First Newsletter

This part will be easy for anyone who’s used wordpress for any length of time. Just create a post as you normally would by clicking the posts link along the left hand side of your admin panel and then select “Add New.”

Maybe you’ll right about some time sensitive topic you really want people to read now. Maybe you want to offer excerpts from your weeks posts. Or as Angelique chose to do maybe you want to pair highlighted posts from the week with special newsletter only content.

So you’ve written your newsletter, and you’re ready to push that pretty blue “Publish” button. Don’t do it just yet. Wait for the next step.

Step 2: Create a Newsletter Category

There are other ways to do this (such as in the categories control panel), but you may as well do it while you’re creating your post. Just look for the “Categories” box along the right site of “Add New Post” page. Click “+ Add New Category,” name your category something like “Newsletter,” and click “Add.” Make sure the category is checked before moving on.

Ok, now you can go ahead and publish your post.

Step 3: Exclude your Newsletter from your blog page

Here comes the “tricky” part. First we need to find out what the category ID number of your new category is. Click the posts link along the left side as you did when creating a post, but this time select the “Categories” link.

Now find the Newsletter category you just created and hover your most over the link. In your browser’s status bar (usually in the lower left) you’ll see the URL this link will take you to. It’ll look something like this http://pressmy.biz/wp-admin/categories.php?action=edit&cat_ID=5. That number at the end is your category ID. Write it down for the next step.

Next we’re going to add some code to your custom_functions.php file. If you’re using a theme other than Thesis you’ll probably just add this to your themes function.php file. You’ll find custom_functions file in the custom folder of your main thesis folder. Open it in your favorite text editor and add this code anywhere after the comments section:

function exclude_category($query) {
if ( $query->is_feed || $query->is_home ) {
$query->set('cat', '-1');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');

Before you go accusing me of being a php genius here’s where I found that code

You will need to make one little change to that code. See the part that says $query->set(‘cat’, ‘-1′);? Just change that number 1 to the category ID number you wrote down earlier.

Finish off this step by uploading custom_functions.php to your server using your favorite FTP program. Check your blog page and make sure the post you just created isn’t showing up.

Step 4: “Create” your Newsletter Page

You may have noticed that “create” is in quotes up there. This is because we’re just going to use the archive system built into WordPress. First lets make sure your newsletter post is showing up on the category archive page.

The URL should be something like http://pressmy.biz/category/newsletter where “newsletter” is the name you gave your new category. Unless you’ve already changed the archive display options you’ll probably just see the title of each post in the category. If you like this then you can skip the rest of this step.

Customizing the look of your archives pages is one place that Thesis really shines. Just visit the “Thesis Options” panel and find “Archives” under “Display Options” along the right had column. You’ll find four options that should be pretty self-explanatory, but here’s a brief explanation:

  • Titles only: This is probably currently select. It will display only your post title as a link to the post
  • Everything’s a teaser!: Your archives pages will appear with a double column of post excerpt with a title, published date and read more link.
  • Same as your home page: Your archives will look just like your home page. Nothing more to be said here.
  • Post excerpts: This will look much like “Everything’s a teaser!” except each excerpt will take up the whole column

Just pick one and click the save button. Play around until you find the option you like.

Step 5: Add your Newsletter Page to Your Navigation Menu

Here’s another area where thesis is a step above the rest, at least until wordpress 3.0 comes out with its killer new nav menu.

Visit your “Thesis Options” page again and this time find “Include these category pages in nav menu” under “Navigation Menu.” Select your newsletter category from the list and click save. If you would like to include more than one category in the navigation menu just hold down the “ctrl” button on your keyboard while making your selections.

Step 6 (optional): Remove “From the category archives”

If you’re like me and you think the “From the category archives” text at the top of your Newsletter page shouldn’t be there, with a quick addition to your custom.css file we can fix that. Open Custom.css found in the custom folder of the main thesis folder and add the following code:

.cat_newsletter #archive_info p {
display:none;
}

Be sure to change “newsletter” in .cat_newsletter to the name of your category. If you would prefer to remove this text from all your archive pages simply remove the .cat_newsletter class from the above code

You’re Done!

There you have it. Now you have a newsletter page for your WordPress site where you can post special content, time sensitive posts, or summaries your blogging activity for the week or anything else.

What else can you think of to use a “newsletter” page for?

{ 2 comments }

Why WordPress is Perfect for your Business

February 21, 2010

If you’ve heard of wordpress it was probably as the platform used by bloggers. But you own a business. You don’t want someone to visit your website and find a long list of posts. You want them to find information about your business. So why would you use wordpress? WordPress can Look Just Like a [...]

Read the full article →