Tag Archives: WordPress

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

Working With wp_register_script()

WordPress lets you register your own script files with your theme or plugin instead of hardcoding a <script> tag into your header or footer. The traditional approach loads scripts unconditionally on every page, whether they’re needed or not, and gives WordPress no way to manage load order or avoid duplicate copies of the same library. Registering scripts with wp_register_script() (and loading them with wp_enqueue_script()) lets WordPress handle dependencies, versioning, and placement for you, which keeps your site faster and your script loading predictable.

So how do you register a script?

Use the following syntax to register a script. This is typically called inside a function hooked to wp_enqueue_scripts (more on that below).


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. Both methods below are still valid in current WordPress: the show_admin_bar filter hides the bar sitewide for every user, while the show_admin_bar_front user meta lets you hide it for a specific user only.

Option 1: Hide for all users (sitewide)

Add this to your theme’s functions.php or a site-specific plugin:
add_filter( 'show_admin_bar', '__return_false' );

Option 2: Hide for the current user only

Call this once (e.g. on a settings save or a one-time migration) to set the preference for the currently logged-in user:
// Hide admin bar for the current user only
update_user_meta( get_current_user_id(), 'show_admin_bar_front', false );
Now the admin bar will be hidden from all users (Option 1) or just the chosen user (Option 2). 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’t 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
include( 'wp-load.php' );

// 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