• Home
  • About
  • Job Profile
  • Portfolio
  • Training
Blue Orange Green Pink Purple

Archive for the ‘Wordpress’ Category

You can use the search form below to go through the content and find a specific post or page:

Aug 19

SEO meta techniques for a wordpress blog

SEO became the important part of websites. WordPress comes with the great concept of blog, CMS and with nice SEO support. Although I’m sharing here the some techniques for basic meta for different pages in wordpress to be handled in a common header section upon your page type, content. Here goes my thoughts…

Basically 3 most common part/page used in a wordpress blog/site, where we goes for visit are

single.php for single post
category.php for any type category/term
index.php for blog home/default template for other types

At first we have common meta tags which will be used if not a single post or category page requested. For category page, we taken category title as a meta keyword and titles of posts under the category as a meta description. For single post page, post title as as meta description and post tags are as meta keywords. Finally a common meta key/description for index/home or other requested page. Here goes the code snippet:

<?php
if(is_category()){
    $cat = get_query_var('cat');
    $cmeta_desc = '';
    $cposts = get_posts(array('numberposts' => -1, 'post_type' => 'post', 'category' => $cat));
    if($cposts){
        foreach ($cposts as $ck => $cp) {
            $cmeta_desc .= $cp->post_title.",";
        }
    }
    $cmeta_desc = rtrim($cmeta_desc, ",");
    ?>
    <meta name="description" content="<?php echo $cmeta_desc;?>">
    <meta name="keywords" content="<?php single_cat_title();?>">
<?php
} else if(is_single()) {
    $tags = '';
    $tag_list = wp_get_post_tags($post->ID);
    foreach ($tag_list as $tk => $tag) {
        $tags .= $tag->name.",";
    }
    $tags = rtrim($tags, ",");
?>
    <meta name="description" content="<?php echo $post->post_title;?>">
    <meta name="keywords" content="<?php echo $tags;?>">    
<?php
} else {
?>
    <meta name="description" content="PHP developer, tech savvy, tech blogger, freelancer">
    <meta name="keywords" content="php, mysql, wordpress, codeigniter, web developer, freelancer, web consultant">  
<?php
}
?>

Hope this will give you guys some idea as well as some help in wordpress.

Happy blogging!

Aug 19

WordPress custom URL rewrites and tips

WordPress has its own URL rewrite support for custom development. Before starting code, it would be good to visit its codex page here. Though there are many things to know and will take time to understand the whole process, i’m describing here a common scenario for rewriting a URL.

htaccess can be used to rewrite URL’s and good technical knowledge required for that to do that on server, at this point we can use wp rewrite to fullfill our need without editing site’s htaccess file.

Here goes the things: suppose we have a page for news listing like http://example.com/news, and we added a custom field in back-end to choose a news reporter. Now we want a page where news will be listed by reporter. we also need SEO friendly URL like this: http://example.com/news/reporter. In this case if we can take reporter slug from URL like \index.php?pagename=catalog&reporter=john’,we can the get reporter slug and can use for further query.

Normally all rewrite rules map to a file handler (almost always index.php), not another URL.

            add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
            add_filter('query_vars','wp_insertMyRewriteQueryVars');

            // Adding a new rule
            function wp_insertMyRewriteRules($rules)
            {
                $newrules = array();
                $newrules['news/(.*)/?'] = 'index.php?pagename=news&reporter=$matches[1]';
                return $newrules + $rules;
            }

            // Adding the 'reporter' var so that WP recognizes it
            function wp_insertMyRewriteQueryVars($vars)
            {
                array_push($vars, 'reporter');
                return $vars;
            }

This would map news/whatever to the WordPress page ‘news’, and pass along the query var ‘reporter’. You could create a page template for ‘news’, then pick up the value of ‘reporter’ using getqueryvar(‘reporter’).

In above example, first we wrote the rules and added the function, then added function to get it by query var.

Tips:

1. Avoid using query vars like id - use something more unique (like 'reporter') to avoid clashes with WordPress.
2. Don't flush rules on every init! It's bad practice, and will write to .htaccess and call database updates on every page load!
3. WordPress will always flush permalinks whenever you update a post or your permalink structure (simply update your permalinks when you make changes to your code).

Will be back soon with another WP issue :)

Jun 12

Usefull function for custom post type in wordpress and more

I’m gonna share someuseful function here which is really handy for custom post type. Some guys really don’t know about those functions, as i got those useful after digging some solutions for real life problem.

1. is_post_type_archive(‘POST-TYPE-NAME’), this is needed when you are checking something for post type archive/listing page like in common header/footer. we frequently use for add some js/css or some class in common places.

2. get_query_var(‘var_name’), we frequently use this to get category name, its retrieve corresponding value of given variable name. I have got this most useful when i need the slug/post_name of a post of any post type. For the last couple of month, i used below code to get last/second segment of URL of a post :

$subpath = $_SERVER['REQUEST_URI'];
$tokens = explode('/', $subpath);
$scn_seg = $tokens[sizeof($tokens)-3];

But that was the extra effort to get URL segment for a post, we can do it by get_query_var(), we need to pass post type name as a parameter to this function like for ‘news’ post type we will use get_query_var(‘news’).

$news_post_slug = get_query_var('news');

So for the URL : http://example.com/news/lorem-ipsum-dolor/, we can get the post slug(post_name used in DB) by get_query_var(‘news’).

3. wp_deregister_script() sometimes we use plugins for some extra facilities like custom post type/twitter feed, plugins use wp_head() to inject its necessary js/css or others function to hook, same as for wp_footer(). wp_deregister_script() is needed when we use our own js like jquery library. So there will be 2 jquery file when using own with wp’s native 1. and sometimes for new bee, its hard to find out why js error occurred and js functions are not working …. wp_head() renders some others links also. to avoid this type of situation, we need to remove that, we can use just wp_deregister_script('jquery'); in theme’s functions.php file.

This is useful to give a condition for admin section like below, otherwise admin panel’s may create problem:

if( !is_admin()){
   wp_deregister_script('jquery');
}

Its also better to add your own library just after de-register wp’s native jquery like below :

if( !is_admin()){
	wp_deregister_script('jquery');
	wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"), false, '1.3.2');
	wp_enqueue_script('jquery');
}

Finally its handy to remove the jQuery from your wordPress font-end/theme files, add wp_deregister_script(‘jquery’) just before wp_head() on your theme’s header.php file like below.

wp_deregister_script('jquery');
wp_head();

Will add some other WP function soon which we need frequently in development.

 

Apr 23

Show user login form in sidebar/template in wordpress

Recently, i was looking for a solution to show a simple user login form in site sidebar, googled but it referred for using plugin like sidebar-login. Suddenly i got wordpress have already its native function to use for login form.

wp_login_form() allows to add the form in any place you wish. some parameters exist there to control the form attributes. i can just put your attention to the following :

  • ‘redirect‘ — where to redirect after login
  • ‘remember‘ — whether to remember the values.

Let us see an example:

if (is_user_logged_in()) {
   echo 'Hello, ', $user_login, '. <a href="', wp_logout_url(), '" title="Logout">Logout</a>';
} else {
   wp_login_form();
}

here, we just checked for logged in user to show different message.
If you want you can customize the look of the form by defining extra css putting it within a div as following screen.
login
So instead of using a plugin with extra feature,using native function is as easy as better!

Apr 17

WordPress ‘Featured Image’, how to use…

WordPress stands for most common and popular CMS now a days. Sometimes we need just a media field for a image to be added in a post. WordPress latest 3+ version has this facility, so you don’t need to use any ‘custom field type’.

In default theme, this feature enabled by default, but when you are using a custom made theme or a theme of your choice which you downloaded, but not supported ‘featured Image’ section, then what to do.

Its very simple for wp developer to add that support to your theme. Add below code to your theme’s functions.php file :

add_theme_support('post-thumbnails');

Now you will see that section in your admin sidebar when adding/editing any page/post. And also you will find option at your ‘Screen Options’ section as below, so from there you can enable/disable that for future need.

In right sidebar, ‘Featured Image’ section after adding a image:

Now, You knew how to add theme support and how to add an image on admin panel. So to show this image on front-end, in any single post/page or in any loop, just add below code to show it between wp post loop:

<?php the_post_thumbnail();?>

This will show that image as you added in admin panel with that image ratio. Note that this code will execute when you are using wordpress’s default loop code like <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?> and <?php endwhile; ?>

If you use custom loop code using other function like get_posts() or custom query like $wpdb->get_results(), then it depends on that way and you have to pull data with correspondence format.

Use, modify and share your thoughts and happy blogging :)

Apr 11

Add Page Excerpt Option in WordPress Admin Panel

WordPress is not a blogging platform now, has many other features, options, resources, plugins to fulfill your requirements.

In some wp version(like 3.1), for ‘pages’, no ‘excerpt’ option, even not option in ‘Screen Options’ to display it . So an easy way to add this to your wp admin panel, follow below steps:

add the following code to your functions.php file.

 //Add Excerpts to Pages 
function themeprefix_add_page_excerpt_support() {
 add_post_type_support( 'page', 'excerpt' ); 
} 
add_action('init', 'themeprefix_add_page_excerpt_support');

We have done this excerpt option by a wordpress hook. At first we used add_post_type_support function to use a specific post type option then we packed within a function. And this function called by a worpress hook/filter to make it available for admin panel.

Functions.php is found in “wp-content\themes\[your-currently-active-theme]\”.

Cheers :)

Saif The Green

  • View Saif's profile on LinkedIn
  • Recent Posts
    • RESTful API – The HTTPish …
    • SUSE – another user-friendly desktop Linux distributions
    • SEO meta techniques for a wordpress blog
    • Set favicon in cross browser and more…
    • WordPress custom URL rewrites and tips
  • Archives
    • October 2016
    • August 2016
    • August 2014
    • July 2014
    • April 2014
    • June 2013
    • April 2013
    • March 2013
  • Categories
    • CodeIgniter
    • Javascript
    • Linux
    • MySQL
    • opencart
    • php
    • SEO
    • Software Development
    • Web Development
    • Web Services
    • Wordpress
  • Meta
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Archives
    • October 2016
    • August 2016
    • August 2014
    • July 2014
    • April 2014
    • June 2013
    • April 2013
    • March 2013
  • Search







Saif The Green is proudly powered by WordPress
Entries (RSS) and Comments (RSS).