Category: Themes

  • Change the “Enter title here” placeholder text to make it fit your content

    Change the “Enter title here” placeholder text to make it fit your content

    When you’re working with custom post types, sometimes the post title isn’t a title. It might be a person’s name, a building number, or a course code (just to take a few examples from universities). So it’s great that WordPress has a simple filter that makes it easy to customize the “Enter title here” placeholder text to make it fit your content:

    https://gist.github.com/patilswapnilv/5cffbf8396e3d2e86b4f77a1f3f1232f

    How to use this?

    Place this code in your functions.php or in a plugin or even better if you can add it to a must-have plugin.

  • How to change WordPress admin icons

    How to change WordPress admin icons

    One of my favorite things about the admin theme with WordPress is the new Dashicons icon font. It brings a sense of standard and uniformity to dashboard icons with its distinctive look and vast array of icons for theme and plugin authors to choose from and use with ease.

    However, it seems that not all plugin and theme authors have jumped on the Dashicons bandwagon. They insist on using their own custom image icons that do not fit in with the new design at all. This includes things such as colored icons which do not adapt to the different color schemes included with WordPress. Other authors do not set an icon for their menu or post type, resulting in many of that same thumb tack post icon or generic gear icon. This leads to a messy dashboard, which I do not like.

    Fortunately, it is easier than ever to override the author’s choice for the icon with a Dashicon of your choice – all with only a few lines of CSS.

    Dashicons is the icon font used officially in WordPress admin. What’s great about it is that you can add some visual cues on your website without bloating it up. Visuals like images increase load times and don’t scale well, while SVGs require you to read up a bit to actually put it to use Today we’re going to show you how you can get these font icons installed on your WordPress project.

    Step 1: Enqueuing the script

    To get your WordPress site ready, you’ll need to open up your child theme’s functions.php file and insert the following lines of code at the end.

    add_action( 'wp_enqueue_scripts', 'load_dashicons_front_end' );
    function load_dashicons_front_end() {
    }

    Step 2: Using dashicons

    Head over to this site and pick an icon that you’d like to use. Click on ‘Copy HTML’ and use that snippet of code and paste it into your HTML, and with that, you are done! Simple isn’t it.

    copy-code-prompt
    Copy the code and insert it into your HTML.

    Extra Step: CSS Method

    Instead of inserting it via HTML, you can also insert dashicons using CSS.  Return to this site and pick an icon. This time, click ‘Copy CSS’ and insert that code into your own CSS file. Make sure to paste the code in the CSS before:selector.

    Here’s a sample code of how this will look like:

    .web-link:before {
    font-family: "dashicons";
    content: "\f319";
    }

    Menu icons?

    Head over to the Dashicons website and pick out an icon you want. Select it and click “Copy CSS” to copy the necessary code for using that icon in CSS.

    The next is to obtain the ID of that menu. To do so, use your browser’s Inspect Element tool. You’re looking for something like toplevel_page_{menu-slug} or menu-posts-{post_type}.

    To add the Dashicon to the menu, use this CSS. Remember to substitute toplevel_page_{menu-slug} for the menu ID and content: '\f174'; for the Dashicon CSS code.

    #adminmenu #toplevel_page_{menu-slug} div.wp-menu-image::before {
        content: '\f174';
    }
    

    If you’re just overriding another Dashicon, then the above is all that is needed. If the menu already has an image icon embedded in the HTML, you will need to add this CSS:

    #adminmenu #toplevel_page_{menu-slug} div.wp-menu-image img 
        display: none;
    }
    

    If the menu already has an image icon applied through CSS background images, use this CSS instead of the above:

    #adminmenu #toplevel_page_{menu-slug} div.wp-menu-image {
        background: none !important;
    }
    

    You can include the CSS code in a plugin, theme’s functions.php or code snippet by wrapping it with this PHP code:

    function replace_admin_menu_icons_css() {
        ?>
        <style>
            /* CSS code goes here */
        </style>
        <?php
    }
    
    add_action( 'admin_head', 'replace_admin_menu_icons_css' );

    And with that, you are now on the road to using WordPress dashicons! Do you use Dashicons or do you prefer SVGs? Let us know which method you use and why down in the comments below.

  • Call a navigation menu using a shortcode

    Today I came across a weird situation: I needed to place a navigation menu in the content of a page. A shortcode was the obvious solution, but there doesn’t appear to be one built in for menus. I created this one very quickly:

    function print_menu_shortcode($atts, $content = null) {
    extract(shortcode_atts(array( 'name' =&gt; null, ), $atts));
    return wp_nav_menu( array( 'menu' =&gt; $name, 'echo' =&gt; false ) );
    }
    add_shortcode('menu', 'print_menu_shortcode');
    

    Place this in functions.php, then use [menu name="main-menu"] to call the menu in your content (replacing "main-menu" with your menu’s slug, of course).

    You could adapt this to accept any of the other arguments available for wp_nav_menu(), but this served my purposes.

  • Installing Theme Unit Test Data with wp-cli

    Installing Theme Unit Test Data with wp-cli

    When building custom themes client sites it is important to test as many edge case as possible. Theme Unit Test Data is the way to go here and a minimum requirement for any professionally built WordPress theme (in my opinion anyway) If building a theme for the wordpress.org theme repository this will almost certainly be tested. For those using WP-CLI there is a quick and easy way to install and then remove this test data. Naturally, you will need to install and setup WP-CLI before proceeding.

    First you will want to backup your current database

    wp db export mydatabase.sql

    Then reset your WordPress installation. This step is not obligatory as you can import the test data on top of your own data. The --y flag stands for “yes” in response to the question “Are you sure you want to reset your database?” So make sure you have the old data backed up in case.

    wp db reset --yes

    Next you will want to import

     curl -O https://wpcom-themes.svn.automattic.com/demo/theme-unit-test-data.xml
    wp plugin install wordpress-importer --activate
    wp import ./theme-unit-test-data.xml --authors=create
    rm theme-unit-test-data.xml

    Here is where you will test you theme. Ideally you should go through and systematically check each of the links which have all sorts of edge cases. Correct any abnormalities and problems with layout. When you are finished you can change back to your old data.

    wp db reset --y
    wp db import mydatabase.sql

     

    Please note that you will have to provide the --url flag for some of those wp commands when using multisite. I am happy to answer any questions in the comments.