Tag Archives: Wordpress

Some helpful information for writing a plugin

I am sharing 2 simple plugins I did, so it will be helpful for new plugin writers.

My client wanted to add an award section. That is, he wanted to assign an award for some post and display it in the front end under the title of the post. Each award has an image also.

So first he needs to create the awards. For that I wrote this plugin.

Now I have to display these awards when editing a post/ adding a new post.

For that I used the following plugin: Adding custom box

This plugin will save the award id as post meta. Later in the theme page we can call the post meta and display the award.

How to allow users to post only in some categories: wordpress

The following code will be useful if you want to allow users to post only in some categories.


function restrict_categories($categories) { 
// If we are in the new/edit post page and not an admin, then restrict the categories
$onPostPage = (strpos($_SERVER['PHP_SELF'], 'post.php') || strpos($_SERVER['PHP_SELF'], 'post-new.php'));
if (is_admin() && $onPostPage && !current_user_can('level_10')) {
$size = count($categories);
for ($i = 0; $i < $size; $i++) {			 			if ($categories[$i]->slug != 'user-created-articles' && $categories[$i]->slug != 'user-created-content' && $categories[$i]->slug != 'user-created-disagreements'&& $categories[$i]->slug != 'user-created-reviews')
unset($categories[$i]);
}
}
return $categories;
}
add_filter('get_terms', 'restrict_categories');

In the above code it checks the category slug. Admin can post in any categories, but other users can post only in the categories, where slug is either “user-created-articles” or “user-created-articles” or “user-created-disagreements” or “user-created-reviews”

Permit contributors to upload images in wordpress

By adding the following code to functions.php in your theme file you can permit contributors to upload images.

if ( current_user_can('contributor') && !current_user_can('upload_files') )
	add_action('admin_init', 'allow_contributor_uploads');

function allow_contributor_uploads() {
	$contributor = get_role('contributor');
	$contributor->add_cap('upload_files');
}

Blank page for wp-admin

Just now, I had been in a horrible situation that I can see all my blog pages but when I go to http://worth2read.org/blog/wp-admin/ it appears as blank. That is I am completly locked out of the admin area.

I made a Google search for the solution and confused with lots of solutions that appeared.

However atlast I manged to get my admin page back, the solution that worked for me is as follows…

I checked the functions.php in my theme folder. There were some spaces at the top and bottom of the page. i deleted it.

I got my admin page back so that I can post this for you now.

I am posting it here thinking if anyone else came through such situation, it will be helpful for them.

Adding new widgets in wordpress themes

We can add new widgets by using functions.php in the theme folder.

For example I can add “Services I offer and Google Adsense in 2 ways
to the sidebars.

1. Code both to the page sidebar.php
3. Create widgets using functions.php file in the theme folder

Following code shows how I added a simple Adsense widget using functions.php

if (function_exists(‘register_sidebar’))
{
register_sidebar_widget(__(‘Adsense’), ‘google_widget’);
}

function google_widget() {
echo $before_widget;
echo $before_title;

echo”<li><h2>Sponsors</h2></li>”;
echo $after_title; 
?>
 <ul>
 <li>

Here comes Adsense code

</li>
 </ul>
 <?php echo $after_widget; 
}

function init_adsense(){
    register_adsense_widget(“Sponsors”, “google_widget”);    
}

add_action(“plugins_loaded”, “init_adsense”);

The above code gives a simple widget named adsense in the widget panel. From the widget list I can add it to the sidebar without disturbing sidebar.php file.

Update:

Just now, I  noticed another method,  if the widget contains text or HTML only. In such cases you can use text widgets. Add the already existing “Text widget”.

In the example above give the title as “Sponsors” and write adsense code in the box. It is the easiest way if the widget you are making contains only text or HTML.

Import From B2evolution(2.4.6) to WordPress (2.7)

The outdated version of migrate.php is already available. I had just updated it so that we can use it for the latest version. Only some field names, table names etc are changed.

Step 1: Upload the wordpress files to the b2evolution directory (This is to keep permanent link as same)

Step2: Install wordpress, Use the same database as that of b2evolution.

Step3: Upload the script migrate.php to the folder wp-admin . Run migrate.php

Help with Permanent Link

The table wp_options contains a field, “permalink_structure” edit it and keep it in the way you want.

Some notes about the script : migrate.php

================================================
Step1 :  You have to connect to the database.

Step2:  Remove the present contents of wordpress tables. It is better not to delete details of user. Otherwise wordpress admin may not be able to access the admin area after running the script.

Step3:  Import users and user meta data.

The table evo_users contains users of your present blog. Select it from evo_users and insert it into wp_users.

Step4:  Import categories.

The table evo_categories contains the category name. Import them to wp_terms.

WordPress handles categories using wp_terms and wp_term_taxonomy tables

Step5: Import the posts

The table evo_items__item contains the posts . Import them to wp_posts.

Step6: Establish category-Post relation.

The table evo_postcats contains the relation between posts and categories. We can import them to wp_term_relationship

This is the difficult part of the job, since wordpress handles category using 3 tables wp_terms, wp_term_taxonomy and wp_term_relationships.

Step6: Process the category counts, ie here we are counting how many posts per category

Step7: Import coments

We have to  import comments from evo_comments to wp_comments

In this way we can import posts, comments, categories, category-post relationship. We can settle permanent link issue by editing wp_options table.

Update on May 17th 2009:
One of the clients asked whether I can fix tags and authors. I have updated the script to import tags and authors.
So now the script will import posts, comments, categories, category-post relationship, tags tag-post relationships and authors. We can settle permanent link issue by editing wp_options table and .htaccess file.

For the script, migrate.php , please contact gsh [dot] james [at] gmail [dot] com .  We will be happy to assist in importing your b2evolution blog to wordpress. [Paid Service]

Price for assistance : 50 – 75 USD ( depending on the data that need to be exported )

Linkedin profile: http://www.linkedin.com/in/gishajames

Thanks