16 April 2012

Edit BuddyPress Admin Bar - Log In

A site I've been working on is using a WordPress multisite install, with BuddyPress.  As a multisite install, the idea is users will be able to create their own blog, which works fine.  However, say a user who is not logged in, visits one of the sub-blogs and clicks the 'Log In' button on the BuddyPress admin bar, they're redirected to the main blog home page. Not optimal behaviour for me.
There was no option to edit this in the BuddyPress admin menu, no plugin to alter this, and trying to search for a solution turned up results that either asked how to fix it, or were about a slightly different problem (e.g. disabling the BuddyPress admin bar and creating one's own).

I have created a plugin to fix this- 'BuddyPress Adminbar Log In Fix' or 'BP-adminbar-c' for short.

To fix the log in behaviour of the button is not too difficult, we create a function for the button, active only when the user is not logged in:


function custom_adminbar_li_button() {
if (!is_user_logged_in()) {
echo '<li><a href="/wp-login.php/">Log In</a></li>';
}
}


Then, we create a function to remove the original log in button, and replace it with our new button, appending it to the WordPress footer which calls the BuddyPress admin bar:

function remv_bp_adminbar_li(){

remove_action('bp_adminbar_menus', 'bp_adminbar_login_menu', 2);
add_action('bp_adminbar_menus', 'custom_adminbar_li_button', 2);
}
add_action('wp_footer','remv_bp_adminbar_li',1);


This plug in is available on GitHub: http://coopey.me/3xci7

21 February 2012

WordPress and Instapaper

I noticed that when I clipped my blog posts, some useless extra info got clipped too.
This was easy to fix with a few lines of code and the Instapaper publisher guidelines.

I have created a plug-in containing this code, available here: http://coopey.ml/m44u

If you want the code yourself it's:

<?php
function instapaper_ce($content = ''){
  if(is_single()){
    $content = '<div class="instapaper_body">' . "\n" . $content;
    $content .= "</div><!-- instapaper_body -->\n";
  }
  return $content;
}
add_filter('the_content', 'instapaper_ce');
?>

16 January 2012

Blank Wordpress homepage and blank admin page / dashboard

Activated a new Wordpress theme, but the home page is blank? Worse still the admin page is blank? Uh-oh.  I did this today on a live site- a stupid mistake.

Luckily, I figured out how to fix it quite quickly, so all is well.

You've uploaded your new/test theme to your Wordpress install and activated it. You visit the homepage and it's blank, so you visit /wp-admin/ but that's blank too.
Simply go back to your FTP client and delete or rename the folder with your new theme in.
Give it a few seconds and try and visit /wp-admin/ again and you'll see the dashboard again.  If you go to the theme selector you'll see a message saying 'theme not found, reverting to default' or something similar.

Rules of Web Development

I'm a fool.
Rules of web development:
1. Always back up.
2. Do not test on a live site.

Today I forgot rule 2.

11 December 2011

not an rpm package (or package manifest):


Got this?

not an rpm package (or package manifest):

You’ve probably downloaded a .html file and not the actual package. Don’t waste 20 minutes wondering why the RPM didn’t work. Like I just did.

10 December 2011

How do I get the copyright notice to update year automatically?


I have a few little websites scattered around, and I noticed one of them had an outdated copyright notice. “Why doesn’t this update automatically?” I thought, then realised it’s easy with PHP- simply pop this in to your footer (or wherever you want it to appear):

&copy; Copyright My Website 2010 - <?php echo date("Y") ?>

Easy, huh?

26 November 2011

WordPress Multisite Domain Mapping - ISPConfig 3

I'm going to assume you're familiar with setting up and installing WordPress and installing plugins and have an intermediate knowledge of hosting and domains.
This isn't a tutorial, more of a checklist, but if you have any questions then please ask in the comments below.

If you're setting up a WordPress multisite network on your server with ISPConfig 3 control panel, the workflow is mostly the same as the tutorials you can find by searching with your favourite search engine. There are, however, two important things to get right (highlighted red) that if not correct will result in endless redirect loops.  The 'Aliasdomain' section isn't covered in any tutorials I could find, but this slots in where the official tutorial talks about 'parking' names in cPanel (a more popular control panel solution).

Example Site: example.com, blog2.example.com, example.org
Example IP: 217.145.318.277
(Replace with your own details- IP address is impossible anyway!)

Set up domain in ISPConfig:
Set up DNS details making sure you have A records for 'www', '*' and ' ' (i.e. blank) all pointing to the server's primary IP address
-- So www.example.com, example.com and everythingelse.example.com at the moment all point to your index.html page on example.com
Set up a site for your primary domain (where you'll install WordPress).

Installing WordPress & Plugins:
Install WordPress (I'm using 3.2.1). Set up your primary blog on example.com
Follow instructions for setting up a multisite network.
Install and activate the plugin Wordpress MU Domain Mapping.
Install and activate the plugin Better HTTP redirects.

Mapping Domains:
Create your secondary blog e.g. 'Blog2' which is now at the address blog2.example.com.
From the Settings menu, choose 'Domain Mapping'.  If you were hosting your WordPress install on your server's default domain, you could put the IP in and omit CNAME, but as we're installing on a server that we're sharing with many domains, ignore IP and in CNAME enter the main blog domain i.e. example.com
In 'Domain Options' on the mapping page, select options 1, 2, and 4.
Next choose the option 'Domains' from the Settings menu.  In 'Site ID' enter '2' (assuming this is a fresh install and the first sub-blog you've set up, else find your ID from the 'Sites' menu) and in domain enter example.org. IT IS VERY IMPORTANT THAT DOMAIN IS ALL IN LOWERCASE! Tick/check the 'Primary Domain' option and click save.

Setting Up Secondary Domain:
At your registrar, find where you can edit your DNS settings.
Remove any A records for ' ' (i.e. blank) - sometimes represented as '@', '*' and 'www'.
In their place create CNAME records for each of the above, pointing at 'example.com'.

Alternatively, if your name servers already point at your ISPConfig server, you can create the domain name under 'DNS' and change the settings on the ISPConfig server as outlined above.

Alias To Main Site
In your ISPConfig control panel, select the 'Sites' menu option, and then the 'Aliasdomain for website' from the left-hand menu.
Choose to 'Add new alias domain'.
In 'Domain' enter example.org
For 'Parent Website' select example.com from the drop-down menu.
For 'Redirect Type' select 'No Flag' from the drop-down menu.
Leave 'Redirect Path' blank, and for 'Auto Subdomain' select '*.' from the drop-down menu and make sure 'Active' is ticked/checked.

Sometimes, DNS settings take a while to work, so I would suggest taking this opportunity to make some tea/coffee.

Now, visiting example.com should take you to your primary blog, example.org should take you to Blog2 and if anyone entered blog2.example.com, they should get redirected to example.org (which should display in the address bar).  All sites can be managed from the 'Network Admin' page which you can reach from the WordPress dashboard (at 3.2.1 this is where it says 'Howdy, Yourname", but this will be slightly different in 3.3).

Update [Feb 2014]: +Gwyneth Llewelyn has posted a helpful comment containing a method for nginx users to utilise the benefits of W3 Total Cache: https://plus.google.com/117667414309209066991/posts/SsWV8EXds5c
She also points out that the 'Better HTTP redirects' plugin is no longer maintained (I've just realised this post is over two years old!) and may not be required. I deactivated on one of the few WP Multisite installs I have remaining and there seems to be no adverse effects. (YMMV).

23 November 2011

Change hostname on CentOS server

If your hosting provider has given you a rubbish or branded hostname for your server, you'll be pleased to know there is a simple way to see and change the hostname on a CentOS server.
Use:
sysctl kernel.hostname
to read the current hostname, and:
sysctl kernel.hostname=YOUR_SERVER_NAME
to change it.

13 November 2011

coopey@gmail.com

Some spam bot (80.86.81.22) is signing up to lots of blogs and attempting to post spammy comments and is using *my* GMail address. Posting it here is probably going to make it worse (luckily it is a secondary account and not my main account).
I'd like to assure anyone who has decided to search for this address that it is not me.
And, because the address is being posted everywhere, it is now getting scraped by other spam bots and I'm getting hundred of spam mails every day. Credit to Google, only 3 have got through the filter so far.

10 November 2011

Hide WordPress Admin Link

If you're editing or building a WordPress theme, and want to hide the 'Admin' link, the solution is very simple:

<?php if (current_user_can("manage_options")) : ?>
       <a href="<?php echo bloginfo("siteurl") ?>/wp-admin/">Admin</a>
<?php endif; ?>
This will show a link saying 'Admin' if an admin is logged in, for all other users and logged-out users nothing is displayed.