background preloader

Dev

Facebook Twitter

Create WordPress Shortcodes. WordPress shortcodes are super handy, especially when handing off a WordPress-based website to a client. The alternative to using shortcodes is creating complicated templates, and even then, you cannot adequately replace what shortcodes can do. I recently needed to implement a new shortcode for the Mozilla Hacks blog and was happy to learn how simple the WordPress API makes creating new shortcodes! Let me show you how to create your own WordPress shortcodes! First you start by creating a function within your theme's functions.php file. The function should return a string which replaces the shortcode within the rendered content.

Function my_shortcode_routine($args) { $return = ''; return $return;} Remember that your function shouldn't echo; a string must be returned. After your function has been created, you can use WordPress' add_shortcode function to register the shortcode. Add_shortcode('shortcode_name', 'my_shortcode_routine'); [shortcode_name key1="value1" key2="value2"] Older Newer.

Widgets

Taxonomy and custom post. Do-It-Yourself Caching Methods With WordPress. Advertisement There are different ways to make your website faster: specialized plugins to cache entire rendered HTML pages, plugins to cache all SQL queries and data objects, plugins to minimize JavaScript and CSS files and even some server-side solutions. But even if you use such plugins, using internal caching methods for objects and database results is a good development practice, so that your plugin doesn’t depend on which cache plugins the end user has. Your plugin needs to be fast on its own, not depending on other plugins to do the dirty work. And if you think you need to write your own cache handling code, you are wrong. WordPress comes with everything you need to quickly implement varying degrees of data caching.

Just identify the parts of your code to benefit from optimization, and choose a type of caching. WordPress implements two different caching methods: Non-persistent The data remains in the cache during the loading of the page. Non-Persistent Cache Example Important Notes. WordPress Essentials: The Definitive Guide To WordPress Hooks. How To Create Custom Post Meta Boxes In WordPress. WordPress Essentials: How To Create A WordPress Plugin - Smashing WordPress. Developing WordPress Locally With MAMP - Smashing WordPress. WordPress Essentials: Interacting With The WordPress Database - Smashing WordPress. Advertisement While you already use many functions in WordPress to communicate with the database, there is an easy and safe way to do this directly, using the $wpdb class. Built on the great ezSQL class1 by Justin Vincent, $wpdb enables you to address queries to any table in your database, and it also helps you handle the returned data.

Because this functionality is built into WordPress, there is no need to open a separate database connection (in which case, you would be duplicating code), and there is no need to perform hacks such as modifying a result set after it has been queried. The $wpdb class modularizes and automates a lot of database-related tasks. In this article, I will show you how to get started with the $wpdb class, how to retrieve data from your WordPress database and how to run more advanced queries that update or delete something in the database. Getting Started <? As you can see, this is a basic SQL query, with some PHP wrapped around it.

Get_results() <? Get_row <? Get_col <?