Tag: WordPress

  • Licence key constants in wp-config.php cheatsheet

    I use a few premium WordPress plugins regularly as part of my work. Rather than rely on keeping the licence key in the database (via the relevant plugin’s WordPress admin settings screen), I generally prefer to add the licence key as a constant to a (non git committed) wp-config.php file.

    This way I don’t have to worry about if the licence key got lost when I pushed or pulled from a different development environment, when I use my preferred tool for doing this WP Migrate DB.

    Right now there are four plugins that I use on a regular basis that support this method of defining a constant:

    To use this method simply add the following to your wp-config.php file (you’ll need to do this on each environment – developer, staging or production – you plan to use this on) and you’re good to go. (If you use version control, I don’t recommend you commit you licence key however).

    https://gist.github.com/nickdavis/331912a00252bced587c2ab7b4e669fe

    If you know of any other good premium plugins that support this method me know and I’ll add them here.

  • add_filter shortcuts in WordPress

    Once you start developing more with WordPress you’ll probably find yourself using add_filter to add, modify or remove data.

    When modifying some text you might do something like the following simple example.

    https://gist.github.com/nickdavis/6403c81b4ec05ba32afecc97634a589e

    Sometimes you’ll want to remove text too.

    https://gist.github.com/nickdavis/fd8a1c75c6f68389f6f087a9f63357df

    However, you might not be aware (or might forget!) that there’s a shorter way of doing this.

    https://gist.github.com/nickdavis/71c2f1f06287c6f4347b7a8e8ef67c6b

    In the above example, there’s no need to write out a separate function to achieve the result that you want. Instead you can use one of the following (self explanatory) ‘shortcut’ functions, baked into WordPress, to pass the type of data that you want.

    https://gist.github.com/nickdavis/c4afa54d97ab6a69e1d6eecdf3268fd7

    Writing it out this way is not only shorter, but much easier to read too.

  • Get WordPress menu items only (not the whole menu)

    You might find yourself in the situation where you need to get the menu items for a particular WordPress menu without grabbing the whole menu markup (for example, the nav part) itself or maybe you just want get the menu items data to output in your loop elsewhere.

    Render the menu items without the nav

    In the first scenario, we can render the menu without the container (usually nav) by using wp_nav_menu() and setting container to be blank.

    https://gist.github.com/nickdavis/aa2cf126384a3550940767688b8f8959

    In this example we just need to know the slug for the menu location we’re targeting.

    Get the menu items data and render in a custom loop

    In the second scenario, we can grab all the menu items data by passing the menu’s slug (rather than the menu location slug) to wp_get_nav_menu_items() like the following example.

    https://gist.github.com/nickdavis/9947bc968903549afc3950d80db796b7

    (For this technique you might need to get a WordPress menu from a specific menu location in order to discover the correct menu slug).

  • Get a WordPress menu from a specific menu location

    Sometimes you know that you want a WordPress menu associated with a particular menu location, but you don’t know for sure which menu is associated with that location.

    To find out which menu it is (I typically want to know the slug) you can do something like the following.

    https://gist.github.com/nickdavis/25d4a733b7c2754ca1e34db31a735bb0

  • Get Post Slug in WordPress

    Get Post Slug in WordPress