• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

wordpress

Hide WordPress Admin Bar by Default

July 21, 2011 by Webhead

Problem:

When users register for a site, they are usually just registering to comment on a page.  This means they DO NOT need the admin bar at the top hiding a portion of your page.

 

Solution:

To get rid of this  you can add this to your functions page:

 

// show admin bar only for admins and editors
if (!current_user_can('edit_posts')) {
	add_filter('show_admin_bar', '__return_false');
}

 

More solutions can be found here:

http://digwp.com/2011/04/admin-bar-tricks/

Filed Under: Coding Tagged With: php, wordpress

Restrict Comments on WordPress Page

July 9, 2011 by Webhead

Problem:

A client needed just one page to have comments where the user must log in.  The client wanted the rest of the pages to be open to anyone to comment.  I found no such plugin.

Solution:

At first I was going to write a plugin, but being that I have never wrote an official plugin before I decided to go the simpler route.  I ended up creating 3 template files and adding a function to the functions.php file.   Since almost all pages will allow anonymous comments, the wordpress setting will allow comments.  The following changes will prevent users from posting a comment to specific page-templates.

page-comment-restrict.php

This is a copy of page.php.  the only difference is the loop part will call ‘page-comments-restrict’ instead of the ‘page’ template.  Use this template for each page that needs the comments restricted.

loop-page-comment-restrict.php

This is a copy of loop-page.php.  The only difference is the comment template.  Instead of calling the default comment template it will call ‘/comments-restrict.php’.

comments_template( '/comments-restrict.php', true );

comments-restrict.php

This is a copy of the comments.php.   The difference is you need to copy some login code from “/wp-includes/comment-template.php”.  Specifically the part where it checks for comment_registration and is_user_logged_in.  After making a copy of comments.php, replace comment_form() with the code below.

		<?php if ( comments_open() ) : ?>
			<?php do_action( 'comment_form_before' ); ?>
			<div id="respond">
				<h3 id="reply-title"><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?> <small><?php cancel_comment_reply_link( 'Cancel reply' ); ?></small></h3>
				<?php if ( !is_user_logged_in() ) : ?>
					<?php echo '<p>' .  sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>'; ?>
					<?php do_action( 'comment_form_must_log_in_after' ); ?>
                <?php else :
                    comment_form();
                endif; ?>
        <?php endif; ?>

 

functions.php

All the changes up until now are to hide the comments form on the page.  However, a malicious user could still try to post a comment to your page because the wordpress setting is to allow anonymous comments.  To prevent this, we need to catch all comments before it is posted by adding an action hook at ‘pre_comment_on_post’.   Add the following code to the functions.php file.

 

 

if ( ! function_exists( 'comments_restrict' ) ) :
/**
 * Restricts comments to logged in users only for the specific pages determined by page template.
 *
 */
function comments_restrict($post_ID) {
    $meta_value = get_post_meta($post_ID, '_wp_page_template', true);
    if ($meta_value == 'page-comments-restrict.php') {
        $user = wp_get_current_user();
        if (!$user->ID) wp_die( __('Sorry, you must be logged in to post a comment.') );
    }
}
endif;

add_action('pre_comment_on_post', 'comments_restrict');

 

That’s it.  It should restrict users who are not logged in from commenting.  Test it out with the following steps:

  • Replace the code in comments-restrict.php with the normal comments_form(); method.   Try to post a comment.  The result should be a page that says you need to be logged in.
  • View the page with and without the wordpress setting to allow anonymous comments.  You should see no difference.

Filed Under: Coding Tagged With: php, wordpress

wordpress mod_rewrite exception url

June 28, 2011 by Webhead

Problem:

I added a wordpress blog to a website, but I still needed to get to a url that was on the same website and not part of wordpress.

Search terms:

wordpress mod_rewrite exception url

Solution Found:

http://stackoverflow.com/questions/6428448/rewriterule-exception-for-static-page-outside-wordpress

Use:

RewriteRule ^test.htm$ - [PT,L]

 

Filed Under: Server Stuff Tagged With: apache, htaccess, wordpress

Custom WordPress Comments

June 27, 2011 by Webhead

This guy explains how to customize WordPress comments pretty well:

http://devpress.com/blog/using-the-wordpress-comment-form/

 

Disable HTML in Comments

You can try this plugin found on :  http://www.theblog.ca/literal-comments

 

// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {

	// convert everything in a comment to display literally
	$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);

	// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
	$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );

	return( $incoming_comment );
}

// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {

	// Put the single quotes back in
	$comment_to_display = str_replace( ''', "'", $comment_to_display );

	return $comment_to_display;
}

add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);
add_filter( 'comment_text', 'plc_comment_display', '', 1);
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);

 

Filed Under: Coding Tagged With: wordpress

WordPress CMS Plugins

June 15, 2011 by Webhead

WordPress has tons of features, but most clients just need to edit pages and want a content management system.  I needed to get rid of all the fat.

Complicated Menus

To get rid of the menus on the dashboard for all roles except the admin place the following code in the functions.php of your theme.

function remove_menus () {
    global $menu;
    if (!(current_user_can('install_themes'))) {
        $restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
    }
    else {
        return;
    }
	end ($menu);
	while (prev($menu)){
		$value = explode(' ',$menu[key($menu)][0]);
		if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
	}
}
add_action('admin_menu', 'remove_menus');

Thanks to http://www.wprecipes.com/how-to-remove-menus-in-wordpress-dashboard

Redirected Login

A problem that arises from this is the dashboard is pretty much useless.  You can use this plugin to redirect a user to a specific page based on their role:  http://wordpress.org/extend/plugins/peters-login-redirect/

Easy TinyMCE adjustments

One plugin that makes this very simple:  http://wordpress.org/extend/plugins/tinymce-advanced/

 

Filed Under: Coding Tagged With: php, wordpress

Which CMS to Use?

May 14, 2011 by Webhead

I recently was asked the age old question “what CMS is the best?”.  So I asked back “What will the CMS be used for?”, and of course that wasn’t specifically known.  This post is not a complete post.  It is here to jot down findings and notes about CMS’s.  Wordpress and Exponent CMS are the only two CMS’ I have tried for more than 5 minutes.  I tried Exponent and WordPress in 2007 and have not given Exponent another chance.  Since then Joomla and Drupal have become the clear competition (in terms of popularity) for WordPress.  So these three will be reviewed.  At the time of this post creation, WordPress is beating both in terms of Google Searches.

In the following lists, “Users” are people who use the CMS to put out content.  “Developers” are people who set up and/or customize the CMS for someone else to use.   And if you haven’t noticed, these are only comparing PHP open source CMS’.

WordPress

Pros

  • Very easy to pick up and learn for new users.

Cons

  • Forums have many complaints from developers.

Drupal

Pros

  • Easy for developers to pick up and learn

Cons

  • Not user friendly.  Difficult for new users to pick up and learn.
  • No WYSIWYG editor.

Joomla

Pros

  • Easy to use for Developers, Designers, and Users

Cons

  • Not as User-Friendly as WordPress.
  • Not as powerful as Drupal.

Conclusion

In conclusion from the web, it seems that WordPress is best for clients you want to impress, Drupal is best if you have a website with many features, Joomla is best if … you need something easier to use than Drupal, but more customizable than WordPress? In my own narrow opinion, WordPress has a large community creating plugins to keep you from straying to Drupal.   These plugins offer calendars, embedded video, columns, galleries, etc.  One thing that I think I’ve seen in Drupal that WordPress does not natively support (and did not find a plugin yet) is to edit different parts of a page.  For example if you wanted to have a template where you needed to edit some text on the left sidebar, the top section, and the main content, you would have trouble doing this in WordPress. I’m still doing research on how easy it would be in Drupal or Joomla.  The problem for me is that the user interface of Joomla and Drupal looks like it’s meant for IT personnel.  However, the people I’m creating a CMS for are business and secretarial personnel.  Decide for yourself and try a demo of each one and more at opensourcecms.com.

Update

Below are the up-to-date trends… Joomla and Drupal?  what’s that?

Filed Under: Off the Shelf Tagged With: cms, drupal, joomla, wordpress

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 10
  • Go to page 11
  • Go to page 12

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization mac os x ms sql mysql open source optimize php php 5.3 responsive rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools