Archive

Posts Tagged ‘wordpress’

Customize Admin Area for WordPress

May 22, 2012 Leave a comment

Hellow Guys,
Wordpress provides a great power to customize most of the things via wordpress, like curd operations on admin menu’s, customizing admin bar (which will be covered in my coming posts).

So today we would work with one of the awesome functionality by wordpress itself, like removing the unwanted menus from admin area. You might be thinking that we could simply hide it why to use the function, correct that’s also the way but remember always do the right way where ever you can.

We just need to call the following function to remove any menu you want to

remove_menu_page( $menu_slug );

$menu_slug is the file name which is called on clicking of the menu, like for user operation wordpress call’s users.php. So if you pass users.php as a “menu slug” then it will remove the entire “Users” menu from admin area. Now you may be wondering that how will i remove the sub menu from the admin area. Simple, just use the following function with the php file name and the parent file name as specified below.

remove_submenu_page( $menu_slug, $submenu_slug )

So say you need to remove some menu’s for author then you may just use the above function’s on admin init. That will make your life more simpler 🙂 … Also do not forget to check the user rights…  For details you may visit remove menuremove sub menu

Hope this helps.. Stay tuned for more ….

Advertisements

WordPress Logout Redirect not working (Solved)

May 17, 2012 4 comments

I know you all be so happy to see the title of the post. Yes there is a solution for adding a redirect after being logout.

Problem:

I was using

 echo wp_logout_url( home_url() );

It seems so simple. When we look at the url it works properly with a parameter “redirect_to”. But whenever you try to use that it will always go to default wordpress login page. I researched and found out that domain mapping plugin is stopping it from redirecting. I tried fixing it in domain mapping itself but it was a waste of time .

Solution:

But then wordpress offers amazing power with hooks. Then i found out there is one hook where i could call a function after doing redirect.

 add_filter('wp_logout','redirect_me');

This calls the function after deleting the cookie and signing out . So i wrote a function that will check if it has a redirect_to parameter in url then could redirect to destination else will redirect to where it came from. If you don’t want to redirect to HTTP_REFERER then you can always use

home_url()

function redirect_me(){

//if the user has rights of editor then return don't do anything
 if(current_user_can('editor')) return;

//get the reffer or you may user home_url() if the "request_to" is not set
 $logout_redirect_url = $_SERVER['HTTP_REFERER'];
 if(!empty($_REQUEST['redirect_to'])) wp_safe_redirect($_REQUEST['redirect_to']);
 else wp_redirect($logout_redirect_url);
 exit();
}

Hope this solution helps .

Add menu into wordpress page

May 11, 2011 Leave a comment

Hello Folks,

Since many days i was wondering to add a dynamic menus that i could insert via plugin. I wanted to insert login and logout buttons into menu bar without editing the template. Then i found out one hook for the wordpress that i could inject my content into menu.

      add_filter('wp_nav_menu_items',add_menu,10,2);

The above hook will call add_menu which contain 2 parameters.

      function add_menu($items,$args)
      {
            items .= "<li>Another Menu</li>";
            return items;
      }

it will look as following


As you can see another menu is created beside. As css is not applied it looks like that apply css would
look alike.

If there is any problem give a reply.

Adding template page from plugin

April 18, 2011 Leave a comment

Hello readers,

I have found a very simple solutions to assign a template page to specific or all pages . According to my previous tutorial for creating the template page and placing into the wordpress current template.

For creating the template you need to add comment on the top of the page as known from my blog

/*
Template Name: new template
*/

Now what ever you want add the functionality to the page and then add header/footer as on your preference.
Now your template file is ready to run or attach to page. But now no need to copy that php file to the template folder of wordpress.
all you need to do is call the following hook

add_filter( 'page_template', 'catch_plugin_template' );

Now this will be called on every page/post load.

add_filter( 'page_template', 'catch_plugin_template' );
function catch_plugin_template($template)
{
if(is_page())
{
$template = WP_PLUGIN_DIR . '/yourpluginname/template-file.php';
return $template;
}
}

Now the above could will execute on every page and the template would be assigned to all the pages you have created.

Thank You for reading.

Auto save problem for custom variables

April 14, 2011 2 comments

Readers while developing with wordpress i found a problem with the post . I had custom variable into my metabox.

So there is a default hook to be called to save that custom variable into the database.

add_action('save_post','function_save_var');

So by default when u click publish post or update post then ‘function_save_var’ would be called .
So when u update the post and then close the page then this would work perfectly fine but then i din know
auto save would call this hook too .

So when my page would be open for a while all my values would reset and my functionality would fail. So
after a little search i got the solution.

function function_save_var()
{
       // Function without avoiding auto save.
}

The above function would fail as it would reset everything .

but then to avoid; you need to add the following code in the start of the function

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return $post_id;

if the post will autosave this would directly return n do not update anything . Thus this is all solved.

Creating Template page

March 2, 2011 3 comments

Hello Readers, I am back after a long time today will give you a short tutorial for the wordpress custom template page.
Firstly remember the changes which would be done are template specific. So if the template changed ,changes are gone.
firstly go into the template folder ie

yoursiteaddres/wp-content/themes

So for now i m considering the default template for wordpress 3.0 is twentyten. Now create a empty file inside the the twenty ten folder .

new_template.php

Now we have the custom template which do not contain any code. but now you might be thinking where to access this template file . But wait this file wont show up into the wordpress admin area unless u write the following code.

/*
Template Name: new template
*/

now we could say we have a template page installed on to the wordpress current theme . You could write any html code or could copy the content from the page.php and delete the content which you don want to display . So now will add the following code.

<?php
/*
Template Name: new_template
*/
get_header();?>
 <div id="container">
 <div id="content" role="main">

      This is a  custom Template Page
      <!-- Any HTML Content Here -->
 </div>
 </div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

As you can see above will give the same look as the other but here you have freedom to write any static or call any php function or any functionality you want to extend to this template or wordpress. So now we have a complete page where we could have our custom page.So now its time to access this new template page. Step1: Got the Pages->Add New Step 2: Select the new template created . Template page As you can see you will get the template page that we created into the combobox. So now do not enter any content into the page just leave blank and select the template page and publish the page. Then from the main menu you could see the menu is created and also the content you have added . Thank you all for reading hope you it could have been a useful article.

Shortcode with wordpress

February 16, 2011 Leave a comment

Hello readers here is one more article for creating a shortcode into the wordpress . Like if you want to have custom tags to be written into a post and should replace it with another function then could be achieved it in minutes ..
Say for example you want to add a tag [google] which will replace with http://www.google.com,so here we could do.
First decide what shortcode you want to name as for now we have named it [google].
Step 1:
Not time for calling the wordpress function. Yes wordpress has provided inbuilt function creating shortcode.


add_shortcode(name-of-shortcode,function-name);

This above function would call the specified function whenever wp will find the shortcode. In our case [google].


add_shortcode('google',get_google_url);

So now the get_google_url would echo the http://www.google.com url.


add_shortcode('google',get_google_url);

function get_google_url()
{
      return "http://www.google.com";
}

Oh thats done !! basic of the shortcode is done .. Thats easy now will go ahead to get parameters from the shortcode. Like if we want to add search url like http://www.google.com?q=search+value So with shortcode [google supercar] will give output like http://www.google.com?q=supercar . Could achieve by the following way

add_shortcode('google',get_google_url);

function get_google_url($atts)
{
      return "http://www.google.com/search?q=".$atts[0];
}

So now where ever you will put [google value] then it will output google search url.

but remember if there are two parameters like [google value1 value2] they would be treated as seperate parameters not one .

Hope its easy to learn wordpress .

Thanx Readers.
Ketan Mujumdar

%d bloggers like this: