WordPress Plugin Development Quickies – Useful Code Snippets

This post is a collection of short but useful WordPress snippets.

Become a professional WordPress Plugin developer

Take our online course WordPress Plugin Development – Learn by Building 8 Plugins to gain all the skills you need to start creating your own WordPress Plugins with PHP.

Get the blog name in WordPress:

bloginfo( 'name' );

Get a media URL in WordPress:


Get the value of a setting in WordPress:


Get homepage URL in WordPress:

home_url( '/' )

Include a file in a plugin that is in the plugin directory:

include( plugin_dir_path( __FILE__ ) . 'some_file.php');

Create a settings page:

1. Needed actions:

//add admin settings
add_action('admin_init', 'xyz_register_settings');
add_action('admin_menu', 'xyz_add_menu_entry' );

2. Register settings:

 * Add plugin admin settings
function xyz_register_settings() {
    register_setting('xyz_group', 'xyz_setting1');
    register_setting('xyz_group', 'xyz_setting2');

3. Add menu entry:

 * add menu to admin
function xyz_add_menu_entry() {
    add_options_page( 'My Settings Page', 'My Settings Page', 'manage_options', 'xyz', 'xyz_plugin_options' );

4. Plugin settings page:

 * show admin settings page
function xyz_plugin_options() {
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2>My Settings</h2>
        <form action="options.php" method="post">
            <?php settings_fields('xyz_group'); ?>
            <?php @do_settings_fields('xyz_group'); ?> 
            <table class="form-table"> 
                <tr valign="top"> 
                    <th scope="row"><label>Setting 1</label></th> 
                        <input type="text" name="xyz_setting1" id="xyz_setting1" value="<?php echo get_option('xyz_setting1'); ?>" />
                <tr valign="top"> 
                    <th scope="row"><label>Setting 2</label></th> 
                        <input type="text" name="xyz_setting2" id="xyz_setting2" value="<?php echo get_option('xyz_setting2'); ?>" />
            </table> <?php @submit_button(); ?> 

Create a shortcode with arguments and content in WordPress:

add_action('init', 'xyz_register_shortcodes');

function xyz_register_shortcodes() {
    add_shortcode( 'your-shorty', 'xyz_shortcode' );

function xyz_shortcode($args, $content) {
    $result = '<a href="'.$args['url'].'">'.esc_attr($content).'</a>';
    return $result;