Tag Archives: WordPress

Prevent users from adding new terms to custom taxonomy in WordPress

Recently I faced a scenario where I had to prevent certain users from adding new terms to some custom taxonomies that I created. After some research I found that there isn’t a straight forward way to do this using user capabilities or roles.

The only way is to use the pre_insert_term hook and black-list taxonomies based on user capabilities. I am sharing the code here so that it would be useful for people who want to something similar.

In the above code change the taxonomy name and capability to suit your usecase.

Posted in WordPress | Tagged , | 8 Comments

Merged my blogs using WordPress Multisite

Most of you know that I have another website in addition to this one where I talk about my experience with hardware programming. When I started the website in late 2012, I decided to host it using another single instance of WordPress.

Recently one of the users of my Bulk Delete WordPress plugin asked me if I can add the ability to delete posts in bulk from multiple sites at once in a WordPress multisite installation. I have played around with WordPress MU a bit before it got merged with WordPress, but haven’t played much with WordPress multisite after it got merged. I did a WordPress multisite installation in my dev server and was very impressed with it. I also realized that it will save me lot of time by just maintaining one WordPress installation instead of two.

After testing out plugin compatibility, I converted both the blogs into a single WordPress multisite installation. The migration process was very simple and now both the sites are powered by a single WordPress multisite installation.

If you manage more than one WordPress installation, then do consider doing it using WordPress multisite. It might simplify lot of things for you.

Posted in WordPress | Tagged , , | 2 Comments

Get the SQL query generated by WordPress

One of the good things about WordPress is that it provides a nice abstraction to DB and you can query pretty much anything from the DB, by using the appropriate functions instead of writing the query yourself.

All though it is good, there are times when you want to see what was the actual SQL query that got generated. I faced this senarious myself, when I was debugging an issue in my Bulk Delete WordPress plugin.

It turns out that it is quite easy to do it.

The WP_Query object has a method called request that returns the SQL query that was generated.

So if you want to get the query generated by the global $wpdb object then you can use the following code

<?php echo $GLOBALS['wp_query']->request; ?>

On the other hand, if you are using a custom WP_Query object then you can call the request method on your object.

<?php
$my_query = new WP_Query();
echo $my_Query->request;
?>

Posted in WordPress | Tagged , , | 1 Comment

Easy Retweet plugin now supports Google Analytics tracking

I recently released a couple of updates to my Easy Retweet WordPress Plugin.

For those who don’t know, Easy Retweet WordPress Plugin allows you to add Twitter tweet or bit.ly buttons to your WordPress posts.

You can choose to add these buttons using any one of the following ways

  • Automatic way – Just configure the button in the settings screen
  • Using shortcodes
  • Using template functions

Google Analytics tracking

One of the new feature that I have added to the Plugin is the ability to add Google Analytics tracking to links that are tweeted using the Tweet buttons.

You can add Google Analytics tracking by including the following in the url that gets tweeted.

  • utm-campaign
  • utm-source
  • utm-medium

Translations

In addition to the new features, I have also added translations for the following languages

  • Danish
  • Irish
  • Hindi
  • Romanian

Mandatory update

The current version of the Plugin is 3.0.1 and it includes both new features and bug fixes. So it is a mandatory update. You can install it from your WordPress admin or download it from the Plugin homepage.

Posted in Plugin Releases | Tagged , , , | Leave a comment

Creating new pages in WordPress through code

Recently, I had to create a new page automatically in WordPress, when a Plugin was created.

I thought there will be a simple core function like wp_insert_post which I can use. It turned out that create a new page from code is not that easy in WordPress. I ended up creating a new function myself wrapping the call to wp_insert_post.

I thought of posting it here so that I know where to look when I need it again 🙂

So here is the function.

It takes 5 parameters, out of which only one is mandatory.

  • $title – The title of the post (mandatory)
  • $content – The content of the post
  • $parent – The post id of the parent page. If this is the top level page, then pass 0
  • $page_template – The page template to use for this page
  • $menu_order – The menu order field.

While you are at it, you might also want to find out whether the page with that title already exists, before creating a new one.

Posted in WordPress | Tagged , | 1 Comment

Developing WordPress Plugins in Github

As most of you know, I actively develop and main more than 20 WordPress Plugins. All of them are hosted in the official WordPress Plugin repository.

Until recently I was using the official WordPress repository’s svn to do the development. All my non-WordPress pet projects are hosted in my github account.

I was thinking of moving all my WordPress Plugins to github for quite sometime now, but didn’t do it thinking that it might involve huge effort to keep both the repos in sync.

After reading about how a couple of people moved (and didn’t face much problem afterwards), I decided to try it out for a couple of my Plugins. The following is what I did and in the end, figured out that it was worth the effort.

Github – more than just code

To be fair to official WordPress Plugin repository, there is nothing anything wrong with the way it works or with svn.

It is just that github as a platform is more suited if you are interested in collaboration and receiving contributions from users. It reduces the barrier to entry for people to contribute to projects and also it provides enough tools for project owners to easily merge (or reject) contributions from users.

I was more interested in this aspect of github rather than the ‘git’ part.

Using github for hosting WordPress Plugins

When I decided to move from svn to github, I wanted to do the following things.

  • I wanted to maintain the entire check-in history from svn. Some of my Plugins are more than 4 years old and have gone through lot of revisions and I didn’t wanted to loose them.
  • I also wanted to do the development in github (read doing atomic check-ins) and then upload to WordPress repository only when I tag the changes for release after testing them.
  • I also didn’t wanted to manually check-in in both the places to do the sync.

For the first I found that git-svn can fetch all the check-in history.

For the second and third point, keeping pragmatism in mind, I ended up creating shell scripts so that I don’t have to do anything manual.

Automation scripts

You can get the scripts that I wrote from my github account. The project consists of two scripts.

  • clone-from-svn-to-git.sh – Use this script to clone your WordPress Plugins from SVN into git/github
  • deploy-plugin.sh – Use this script to push your WordPress Plugin updates to SVN from gi/github

Right now the documentation is pretty sparse. I will fix it when I get some free time 🙂

Things to improve

The scripts pretty much do the job. Going forward, I want to improve the following to make it more useful.

  • The readme.txt file used by WordPress repository is not fully in markdown. Because of which the readme file displayed in github is slightly broken.
  • Ability to auto update the .pot file before releasing the new version
  • WordPress repository allows you to add a banner using the assests/ directory. Need to add support for that.

Update: All the above features are added. Check out the latest code in github.

Is it worth the effort?

So far I have moved the following Plugins to github. Will be moving the other ones as well when even I get some free time.

So now the real question. Is it worth the effort? It is worth every second that I spent on it.

You should check the pull requests I have received so far. I have received more contributions to my Plugins in the last couple of weeks than the last 5 years 🙂

If you are a WordPress Plugin developer, then try out my script and let me know if you have any feedback.

Posted in WordPress | Tagged , , , , | 8 Comments

Completely rewrote WP-IRC Plugin

It’s been quite sometime since I released my WP-IRC WordPress Plugin. A couple of users complained about the increase in page load time and so I decided to fix it and ultimately ended up rewriting it.

By the way if you have not used it before, WP-IRC is a WordPress Plugin, that retrieves the number of users who are currently online in an irc channels and allows you to display this information in the sidebar using widgets.

Dynamic update using AJAX to decrease page load time

Since the Plugin has to connect to the irc server through sockets and do some protocol exchange to retrieve the user count in a channel, it was increasing the page load time drastically and was the major complaint that I was receiving about the Plugin.

I tired to decrease the page load time by lazily connecting to the irc server after the page is loaded using AJAX to retrieve the user count. This has drastically decreased the page load time and was one of the highlight of this rewrite.

You can check out this excellent article to find out how to use AJAX in WordPress.

Added caching to decrease page load time

In addition to using AJAX, I have also implemented caching so that the Plugin doesn’t have to connect to the irc server for every page load to retrieve the user count.

I implemented caching using transients in WordPress.

Support for multiple widgets

One of the other highlights of this release is that, I have enabled support for multiple instance of the widgets. This will allow users to use multiple widgets, each with its own configuration.

I implemented it using the Widgets API of WordPress.

Translation support

Finally I have also added support for internationalization in my Plugin. The .pot file is under the /languages folder and if you are willing to do translation for the Plugin, use the pot file to create the .po files for your language and let me know.

Download and source code

You can download the Plugin from its homepage and the source code is available at github.

Try out the Plugin and do let me know if you have any feedback or comments.

Posted in Plugin Releases | Tagged , , , | 1 Comment

WordPress Plugin to add Foss.in badge

I just wrote a WordPress Plugin, that allows you to display the Foss.in badge on your WordPress based website.

If you just want a easy setup, then you can just use the sidebar widget that is available when you install the Plugin. If you want more control over the display, then you can the template function yourself. Details about the template function is available in the Plugin’s homepage.

BTW, did I tell you that I am going to be speaking at foss.in? 🙂 Yes, my proposal got selected and I will be talking about building robots using Open hardware like Arduino.

Posted in Plugin Releases | Tagged , , | 1 Comment

Posts By Tag Plugin now supports excluding current post

I recently released a couple of updates to my Posts By Tag WordPress Plugin, which allows you to display a list of posts based on tags either in the sidebar as a widget or inside your posts using either shortcodes or a template tag.

Changes

The following are the changes that I have made to the Plugin

  • Ability to exclude current post/page
  • Moved caching logic to widget
  • Added Lithuanian translations

Ability to exclude current post/page

One of the most requested feature in the Plugins was to have the ability to exclude the current post in the listing. This could happen when ever you are including the tag of the current post also in the list of tags.

Now I have added an option in the widget to exclude the current post or page.

This option is also available even if you are using either the template tag or the shortcode

Moved caching logic to widget

I have cleaned up the logic which I was using to cache the DB queries used by the Plugin. If you are using the widget, then the output of the widget itself is cached. If for some reason, you don’t want to the widget to cache the queries you can disable it as well.

But if you are using the shortcode or the template tag, then you have to manually cache the output or the db queries.

Dev Time

This release took me about 3.5 hours of development time. You can find more details about the dev time tracking which I have recently started doing in a separate blog post.

Download

You can download the latest version of the Plugin from the Plugin’s home page.

Feedback

Try out the Plugin and if you have any comments or if you want to report any bugs, please leave a comment below.

Stay updated

I would be posting updates about this Plugin in my blog and in Twitter . If you want to be informed when new version of this Plugin is released, then you can either subscribe to this blog’s RSS feed or follow me in Twitter .

 

Posted in Plugin Releases | Tagged , , | 1 Comment

Started tracking time spend on my pet projects

Regular readers of my blog will know that I have lot of pet projects. WordPress Plugins, Android Apps, Arduino projects, jQuery Plugins and a whole variety of other hobbies as well.

Some of them are really popular. For instance my Bulk Delete WordPress Plugin has been downloaded for nearly 100,000 times. The flip side of it is that these pet projects have some additional baggage as well. Support questions, regular updates, feature requests etc can be sometimes very time consuming.

I always wanted to know the projects, which takes up most of my free time, but didn’t had any metrics or logs. So I have started to track the amount of time I am spending on each of these projects.

The advantage of this is that, now I know which project are real time hogs and can have some metrics to decide on whether it is worth spending time on those projects or not. I am also going to add this data to the project readme files, so that people who use it might also know the amount of time and effort I have put it on those projects 🙂

I have already started this and the recent release of my Bulk Delete WordPress Plugin had this metrics. I have spent close to 11 hours for releasing the update to the Plugin.

Let’s see how this new experiment turns out to be.

BTW do you guys also track your time spent on pet projects? If yes, then share your experiences as well.

 

Posted in Random/Personal, WordPress | Tagged , , | 3 Comments