Tag Archives: WordPress

WordPress is web software you can use to create a beautiful website or blog.

Working With wp_register_script()

WordPress allows you to register your own script files with theme. This allows you to link the script file to WordPress generated page according to the dependencies and at right time. The traditional way of including scripts, i.e. by adding <script> tag in head or footer have some drawbacks. In traditional way, we hardcode the script and load it every time with page, even though it is not needed. But once you register the script with theme, it can be loaded dynamically. This allows you to improve your performance and make your website more faster.

So how to register a script?

Quite simple. Just use following syntax for registering your scripts.

<?php wp_register_script( $handle, $src, $deps, $ver, $in_footer ); ?>
Continue reading Working With wp_register_script()

Hide the Admin Bar in WordPress

As you know whenever an user login to WordPress system, who-ever it may be admin or just a subscriber, WordPress automatically shows an admin bar at top of the screen. If the user is administrator then this admin bar provides some useful functionality such as links to navigate to various admin panels of websites, moderating comments, shows available updates etc. The options may be vary depending upon the plugins you have installed.

You might know that this admin bar can be hidden form Profile > Toolbar > Uncheck “Show Toolbar when viewing site”. This is something depends on user’s will. If they like that admin bar they will not hide it.

But it may happen that, you want to hide admin bar for all users maybe due to it interferes with your design. Then you can achieve this using simple WordPress hook:

add_filter('show_admin_bar', '__return_false');

Now the admin bar will be hidden from all user. That’s all. Thank you WordPress hooks!

Getting Recent Posts Outside Of WordPress

Many sites do use WordPress for blogging but they do not use it as back-end for whole site. (Your site or app may be one of them.) There may be a scenario in which you have to display recent posts from WordPress blog, on a page which is outside of WordPress. No problem. You might know that WordPress is quite flexible, hence you can handle such scenario with a small code. There are two ways to achieve this. You can either take advantage of WordPress loop itself or if you don’t want to deal with WordPress loop then you can use other way also.

If you don’t want WordPress loop

So you don’to want to take advantage of WordPress loop? Then you can use wp_get_recent_posts(). According to WordPress codex:

wp_get_recent_posts() will return a list of posts. Different from get_posts which returns an array of post objects.

How to use it?

Just copy paste following code in page where you want to list the Recent Posts


// Get the last 10 posts
$recent_posts = wp_get_recent_posts(array(
  'numberposts' => 10,
  'post_type' => 'post',
  'post_status' => 'publish'
// Display them as list
echo '<ul>';
foreach($recent_posts as $post) {
  echo '<li><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></li>';
echo '</ul>';

You can simply change numberposts parameters value to change the number of posts you want to retrieve.

Continue reading Getting Recent Posts Outside Of WordPress